Skip to content

Commit

Permalink
chore: upgrade for scraper v3 (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
rem42 authored Jun 14, 2023
1 parent 9476148 commit 81915ae
Show file tree
Hide file tree
Showing 111 changed files with 1,372 additions and 6,627 deletions.
6 changes: 1 addition & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ jobs:
fail-fast: false
matrix:
include:
- operating-system: 'ubuntu-20.04'
php-version: '7.4'
- operating-system: 'ubuntu-20.04'
php-version: '8.0'
- operating-system: 'ubuntu-20.04'
- operating-system: 'ubuntu-latest'
php-version: '8.1'

name: PHP ${{ matrix.php-version }}
Expand Down
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
.idea
/bin/
/build/
/vendor/
/composer.lock
/.php-cs-fixer.cache
/.phpunit.result.cache
/.phpunit.cache/
6 changes: 5 additions & 1 deletion .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<?php

$config = new Rem42\CS\Config\Config;
$config->getFinder()
$config
->addMoreRules([
'declare_strict_types' => true,
])
->getFinder()
->in(
[
__DIR__.'/src',
Expand Down
99 changes: 99 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
Scraper Prestashop
==================

![Packagist version](https://flat.badgen.net/packagist/v/rem42/scraper-prestashop)
![Packagist download](https://flat.badgen.net/packagist/dt/rem42/scraper-prestashop)
![Packagist name](https://flat.badgen.net/packagist/name/rem42/scraper-prestashop)
![Packagist php version](https://flat.badgen.net/packagist/php/rem42/scraper-prestashop)
![Github licence](https://flat.badgen.net/github/license/rem42/scraper-prestashop)
![Depenabot](https://flat.badgen.net/github/dependabot/rem42/scraper-prestashop)
![Codeclimate lines of code](https://flat.badgen.net/codeclimate/loc/rem42/scraper-prestashop)
![Codeclimate maintainability](https://flat.badgen.net/codeclimate/maintainability/rem42/scraper-prestashop)

Scraper can handle multiple request type and transform them into object in order to create some API.

Installation
------------

````bash
composer require rem42/scraper-prestashop
````

Requirement
-----------

- PHP >= 8.1

Usage
-----

Initiate the client with the http client of your choice that implement the `Symfony\Contracts\HttpClient\HttpClientInterface` interface.


`````php
<?php

use Scraper\Scraper\Client;
use Symfony\Component\HttpClient\CurlHttpClient;
use Symfony\Component\HttpClient\NativeHttpClient;

$client = new Client(
new CurlHttpClient()
// OR
new NativeHttpClient()
);
`````

Then you can use the client to make request to the API.

Product list
------------

`````php
<?php

use Scraper\ScraperPrestashop\Request\PrestashopGetRequest;
use Scraper\ScraperPrestashop\Entity\PrestashopProducts;

$request = new PrestashopGetRequest(
'host.com', // Your prestashop host
'key', // Your prestashop webservice key
'products', // The resource you want to get, like 'products'
);

// optional for list request
$request
->addFilter('limit', 1)
->addFilter('offset', 1)
;

/** @var PrestashopProducts $products */
$products = $client->execute($request);

$products->getProducts(); // Return an array of PrestashopProduct
`````

Product detail
--------------

`````php
<?php

use Scraper\ScraperPrestashop\Request\PrestashopGetRequest;
use Scraper\ScraperPrestashop\Entity\PrestashopProduct;

$request = new PrestashopGetRequest(
'host.com', // Your prestashop host
'key', // Your prestashop webservice key
'products', // The resource you want to get, like 'products'
);

$request->setId(42);

/** @var PrestashopProduct $product */
$product = $client->execute($request);

$product->name; // Return the product name
$product->dateAdd; // Return the product creation date
`````

36 changes: 12 additions & 24 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,30 @@
"homepage": "https://remy.ovh"
}],
"require" : {
"php": "^7.4|^8.0",
"doctrine/collections": "^1.6",
"symfony/serializer-pack": "^1.0",
"rem42/scraper": "^2.0",
"ext-json": "*"
"php": "^8.1",
"ext-json": "*",
"rem42/scraper": "^3.0",
"symfony/serializer-pack": "^1.1"
},
"require-dev": {
"rem42/php-cs-fixer-config": "^3.1",
"phpstan/phpstan": "^0.12",
"phpunit/phpunit": "^9.5",
"php-coveralls/php-coveralls": "^2.5"
"rem42/php-cs-fixer-config": "^3.4",
"phpstan/phpstan": "^1.8",
"phpunit/phpunit": "^9.5"
},
"autoload": {
"psr-0": {
"Scraper\\ScraperPrestashop\\": "src/"
},
"psr-4": {
"Scraper\\ScraperPrestashop\\": "src/"
}
},
"autoload-dev": {
"psr-0": {
"Scraper\\ScraperPrestashop\\Tests\\": "tests/"
},
"psr-4": {
"Scraper\\ScraperPrestashop\\Tests\\": "tests/"
}
},
"config": {
"bin-dir": "bin/"
},
"scripts": {
"static-analysis": "./bin/phpstan analyse src --level=max --no-progress -vvv",
"code-style-check": "./bin/php-cs-fixer fix --dry-run --verbose",
"code-style-fix": "./bin/php-cs-fixer fix",
"unit-test": "./bin/phpunit"
},
"sort-packages": true
"static-analysis": "./vendor/bin/phpstan analyse src --level=max --no-progress -vvv",
"code-style-check": "./vendor/bin/php-cs-fixer fix --dry-run --verbose",
"code-style-fix": "./vendor/bin/php-cs-fixer fix --using-cache=no",
"unit-test": "./vendor/bin/phpunit"
}
}
27 changes: 27 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheResultFile=".phpunit.cache/test-results"
executionOrder="depends,defects"
forceCoversAnnotation="false"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
convertDeprecationsToExceptions="true"
failOnRisky="true"
failOnWarning="true"
verbose="true">
<testsuites>
<testsuite name="default">
<directory>tests</directory>
</testsuite>
</testsuites>

<coverage cacheDirectory=".phpunit.cache/code-coverage"
processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
</phpunit>
32 changes: 0 additions & 32 deletions phpunit.xml.dist

This file was deleted.

11 changes: 10 additions & 1 deletion src/Api/PrestashopApi.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
<?php
<?php declare(strict_types=1);

namespace Scraper\ScraperPrestashop\Api;

use Scraper\Scraper\Api\AbstractApi;
use Scraper\Scraper\Attribute\Scraper;
use Scraper\Scraper\Request\ScraperRequest;
use Scraper\ScraperPrestashop\Factory\SerializerFactory;
use Symfony\Contracts\HttpClient\ResponseInterface;

abstract class PrestashopApi extends AbstractApi
{
public function __construct(ScraperRequest $request, Scraper $scraper, ResponseInterface $response)
{
parent::__construct($request, $scraper, $response);
$this->serializer = SerializerFactory::create();
}
}
26 changes: 13 additions & 13 deletions src/Api/PrestashopGetApi.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
<?php
<?php declare(strict_types=1);

namespace Scraper\ScraperPrestashop\Api;

use Scraper\ScraperPrestashop\Exception\PrestashopUnexpectedException;
use Scraper\ScraperPrestashop\Factory\SerializerFactory;
use Scraper\ScraperPrestashop\Request\PrestashopRequest;
use Scraper\ScraperPrestashop\Tools\ResourceMapping;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;

class PrestashopGetApi extends PrestashopApi
{
/**
* @return object|object[]|string
* @return object|array<object>|string
*/
public function execute()
public function execute(): object|array|string
{
$content = $this->response->getContent();

Expand All @@ -29,19 +28,20 @@ public function execute()

if ($this->request->getId()) {
/** @var array<string, mixed> $data */
$data = json_decode($content, true, 512, \JSON_THROW_ON_ERROR);
$data = json_decode($content, true, 512, \JSON_THROW_ON_ERROR);
$content = json_encode($data[ResourceMapping::singularize($this->request)], \JSON_THROW_ON_ERROR);
}

return SerializerFactory::create()
/* @phpstan-ignore-next-line */
return $this->serializer
->deserialize(
$content,
$className,
'json',
[
AbstractObjectNormalizer::DISABLE_TYPE_ENFORCEMENT => true,
]
)
$content,
$className,
'json',
[
AbstractObjectNormalizer::DISABLE_TYPE_ENFORCEMENT => true,
]
)
;
}
}
34 changes: 19 additions & 15 deletions src/Api/PrestashopPostApi.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
<?php
<?php declare(strict_types=1);

namespace Scraper\ScraperPrestashop\Api;

use Scraper\ScraperPrestashop\Exception\PrestashopUnexpectedException;
use Scraper\ScraperPrestashop\Factory\SerializerFactory;
use Scraper\ScraperPrestashop\Request\PrestashopRequest;
use Scraper\ScraperPrestashop\Tools\ResourceMapping;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;

class PrestashopPostApi extends PrestashopApi
{
public function execute()
/**
* @return object|array<object>
*/
public function execute(): object|array
{
$content = $this->response->getContent();

Expand All @@ -19,18 +21,20 @@ public function execute()
}

$className = ResourceMapping::find($this->request);
$data = json_decode($content, true, 512, \JSON_THROW_ON_ERROR);
$content = json_encode($data[ResourceMapping::singularize($this->request)], \JSON_THROW_ON_ERROR);
/** @var array<string, mixed> $data */
$data = json_decode($content, true, 512, \JSON_THROW_ON_ERROR);
$content = json_encode($data[ResourceMapping::singularize($this->request)], \JSON_THROW_ON_ERROR);

$serializer = SerializerFactory::create();

return $serializer->deserialize(
$content,
$className,
'json',
[
AbstractObjectNormalizer::DISABLE_TYPE_ENFORCEMENT => true,
]
);
/* @phpstan-ignore-next-line */
return $this->serializer
->deserialize(
$content,
$className,
'json',
[
AbstractObjectNormalizer::DISABLE_TYPE_ENFORCEMENT => true,
]
)
;
}
}
Loading

0 comments on commit 81915ae

Please sign in to comment.