Skip to content

Commit

Permalink
chore: upgrade for scraper v3 (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
rem42 authored Jun 14, 2023
1 parent 93ad867 commit 72e9a38
Show file tree
Hide file tree
Showing 41 changed files with 232 additions and 359 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
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Scraper Colissimo
=================

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

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

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

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

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

- PHP >= 8.1

Usage
-----

TODO
34 changes: 11 additions & 23 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,29 @@
}
],
"require": {
"php": "^7.4|^8.0",
"doctrine/collections": "^1.6",
"rem42/scraper": "^2.0",
"symfony/serializer-pack": "^1.0"
"php": "^8.1",
"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.0"
"rem42/php-cs-fixer-config": "^3.4",
"phpstan/phpstan": "^1.8",
"phpunit/phpunit": "^9.5"
},
"autoload": {
"psr-0": {
"Scraper\\ScraperColissimo\\": "src/"
},
"psr-4": {
"Scraper\\ScraperColissimo\\": "src/"
}
},
"autoload-dev": {
"psr-0": {
"Scraper\\ScraperColissimo\\Tests\\": "tests/"
},
"psr-4": {
"Scraper\\ScraperColissimo\\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>
24 changes: 0 additions & 24 deletions phpunit.xml.dist

This file was deleted.

35 changes: 18 additions & 17 deletions src/Adapter/ColissimoAdapter.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace Scraper\ScraperColissimo\Adapter;

Expand All @@ -14,8 +14,8 @@ public static function execute(ResponseInterface $response): ColissimoJSON
if ($response->getStatusCode() >= 300 || $response->getStatusCode() < 200) {
self::handleException($response);
}
$content = $response->getContent();
$colissimoXML = new ColissimoJSON();
$content = $response->getContent();
$colissimoJSON = new ColissimoJSON();

$result = self::extractData($content);

Expand All @@ -25,24 +25,24 @@ public static function execute(ResponseInterface $response): ColissimoJSON
$header = self::extractHeader($item);

if (\count($header) > 0) {
if (false !== strpos((string) $header['Content-Type'], 'application/json')) {
$colissimoXML->setJson(trim(substr($item, (int) $header['offsetEnd'])));
if (str_contains((string) $header['Content-Type'], 'application/json')) {
$colissimoJSON->setJson(trim(substr($item, (int) $header['offsetEnd'])));
} elseif (3 === $i) {
$colissimoXML->setCn23(trim(substr($item, (int) $header['offsetEnd'])));
$colissimoJSON->setCn23(trim(substr($item, (int) $header['offsetEnd'])));
} else {
$colissimoXML->setFile(trim(substr($item, (int) $header['offsetEnd'])));
$colissimoJSON->setFile(trim(substr($item, (int) $header['offsetEnd'])));
}
}
$i++;
}

return $colissimoXML;
return $colissimoJSON;
}

private static function handleException(ResponseInterface $response): void
{
$content = $response->getContent(false);
$data = self::extractData($content);
$data = self::extractData($content);

if (1 !== \count($data)) {
throw new ColissimoResponseUnknownException('Colissimo error, more than one value expected');
Expand All @@ -51,9 +51,10 @@ private static function handleException(ResponseInterface $response): void

$header = self::extractHeader($data);

$messages = json_decode(trim(substr($data, (int) $header['offsetEnd'])))->messages;
/** @var \stdClass $exception */
$exception = json_decode(trim(substr($data, (int) $header['offsetEnd'])), false, 512, \JSON_THROW_ON_ERROR);

throw new ColissimoResponseException('Colissimo request error: ', $messages);
throw new ColissimoResponseException('Colissimo request error: ', $exception->messages ?? []);
}

/**
Expand Down Expand Up @@ -89,20 +90,20 @@ private static function extractData(string $data): array
*/
private static function extractHeader(string $part): array
{
$content = 'Content-';
$header = [];
$content = 'Content-';
$header = [];
$headerLineStart = strpos($part, $content, 0);
$endLine = 0;
$endLine = 0;
while (false !== $headerLineStart) {
$header['offsetStart'] = $headerLineStart;
$endLine = strpos($part, "\r\n", $headerLineStart);
$headerLine = explode(': ', substr(
$endLine = strpos($part, "\r\n", $headerLineStart);
$headerLine = explode(': ', substr(
$part,
$headerLineStart,
$endLine - $headerLineStart
));
$header[$headerLine[0]] = $headerLine[1];
$headerLineStart = strpos($part, $content, (int) $endLine);
$headerLineStart = strpos($part, $content, (int) $endLine);
}
$header['offsetEnd'] = $endLine;

Expand Down
2 changes: 1 addition & 1 deletion src/Api/ColissimoApi.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace Scraper\ScraperColissimo\Api;

Expand Down
13 changes: 7 additions & 6 deletions src/Api/ColissimoGenerateLabelApi.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace Scraper\ScraperColissimo\Api;

Expand All @@ -8,17 +8,18 @@

final class ColissimoGenerateLabelApi extends ColissimoApi
{
public function execute()
public function execute(): ColissimoGenerateLabel
{
$colissimoAdapter = ColissimoAdapter::execute($this->response);
$response = $this->serializer->deserialize($colissimoAdapter->getJson(), Response::class, 'json');
/** @var Response $response */
$response = $this->serializer->deserialize($colissimoAdapter->getJson(), Response::class, 'json');

$colissimoGenerateLabel = new ColissimoGenerateLabel();
$colissimoGenerateLabel->setResponse($response);
$colissimoGenerateLabel->setFile($colissimoAdapter->getFile());
$colissimoGenerateLabel->response = $response;
$colissimoGenerateLabel->file = $colissimoAdapter->getFile();

if (!empty($colissimoAdapter->getCn23())) {
$colissimoGenerateLabel->setCn23($colissimoAdapter->getCn23());
$colissimoGenerateLabel->cn23 = $colissimoAdapter->getCn23();
}

return $colissimoGenerateLabel;
Expand Down
44 changes: 4 additions & 40 deletions src/Entity/ColissimoGenerateLabel.php
Original file line number Diff line number Diff line change
@@ -1,46 +1,10 @@
<?php
<?php declare(strict_types=1);

namespace Scraper\ScraperColissimo\Entity;

final class ColissimoGenerateLabel
{
private Response $response;
private ?string $file = null;
private ?string $cn23 = null;

public function getResponse(): Response
{
return $this->response;
}

public function setResponse(Response $response): self
{
$this->response = $response;

return $this;
}

public function getFile(): ?string
{
return $this->file;
}

public function setFile(?string $file = null): self
{
$this->file = $file;

return $this;
}

public function getCn23(): ?string
{
return $this->cn23;
}

public function setCn23(?string $cn23 = null): self
{
$this->cn23 = $cn23;

return $this;
}
public Response $response;
public ?string $file = null;
public ?string $cn23 = null;
}
4 changes: 2 additions & 2 deletions src/Entity/ColissimoJSON.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace Scraper\ScraperColissimo\Entity;

Expand All @@ -13,7 +13,7 @@ public function getCn23(): ?string
return $this->cn23;
}

public function setCn23(?string $cn23 = null): self
public function setCn23(string $cn23 = null): self
{
$this->cn23 = $cn23;
return $this;
Expand Down
Loading

0 comments on commit 72e9a38

Please sign in to comment.