Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare update + change dockerfile #27

Merged
merged 32 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ jobs:

steps:
- uses: actions/checkout@v3
- name: Set up php 8.1
- name: Set up php 8.2
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
php-version: '8.2'
- name: Validate composer.json and composer.lock
run: composer validate --strict

Expand All @@ -37,6 +37,8 @@ jobs:

# Add a test script to composer.json, for instance: "test": "vendor/bin/phpunit"
# Docs: https://getcomposer.org/doc/articles/scripts.md
- name: RectorPHP Refactoring
run: vendor/bin/rector process src

- name: Run test suite
env:
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
dist: trusty
language: php
php:
- '8.1'
- '8.2'
- nightly
before_script: composer install
script:
Expand Down
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
* [Authors](#authors)
* [Licence](#licence)

Version 2.5.2
Version 2.6.0

## Requirements
* PHP 8.1 min or superior (oldest versions are no longer supported)
* PHP 8.2 min or superior (oldest versions are no longer supported)
* API Key and API Secret from [Astrobin](https://www.astrobin.com/api/request-key/)

## Introduction
Expand All @@ -53,17 +53,20 @@ You can install this package in 2 different ways.
Update to the newest version :
> `composer update hamhamfonfon/astrobin-ws`

* If you just want to make some issues, make some simple tests etc, juste clone the repository

> `git clone [email protected]:HamHamFonFon/Astrobin-Webservices.git`

If you're using old PHP versions:
- PHP 8.1
> `composer require hamhamfonfon/astrobin-ws:2.5`
- PHP 7.4 | 8.0
> `composer require hamhamfonfon/astrobin-ws:2.4`
- PHP 7.3
> `composer require hamhamfonfon/astrobin-ws:2.3`

Caution, these versions are not maintained anymore. Only 2.5.* will be maintained and will have new features.
Caution, these versions are not maintained anymore. Only 2.6.* will be maintained and will have new features.

* If you just want to make some issues, make some simple tests etc, juste clone the repository

> `git clone [email protected]:HamHamFonFon/Astrobin-Webservices.git`

### Usage

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
},
"config": {
"platform": {
"php": "8.1"
"php": "8.2"
}
},
"require" : {
"php": ">=8.1.0",
"php": ">=8.2.0",
"guzzlehttp/guzzle": "^7.5",
"ext-ctype": "*",
"ext-json": "*"
Expand Down
2 changes: 1 addition & 1 deletion docker/php/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM php:8.1.0-fpm
FROM php:8.2.0-fpm

ARG USERNAME=site
ARG USER_UID
Expand Down
16 changes: 12 additions & 4 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
use Rector\CodeQuality\Rector\ClassMethod\InlineArrayReturnAssignRector;
use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;
Expand All @@ -14,9 +15,16 @@
]);

// register a single rule
$rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class);
$rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class);
$rectorConfig->rule(InlineArrayReturnAssignRector::class);

$rectorConfig->sets([LevelSetList::UP_TO_PHP_81]);
$rectorConfig->sets([SetList::CODE_QUALITY]);
// $rectorConfig->sets([SetList::CODING_STYLE]);

$rectorConfig->sets([
LevelSetList::UP_TO_PHP_82,
SetList::CODE_QUALITY,
SetList::CODING_STYLE,
SetList::TYPE_DECLARATION,
// SetList::DEAD_CODE,
SetList::EARLY_RETURN,
]);
};
33 changes: 20 additions & 13 deletions src/AbstractWebService.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@
*/
abstract class AbstractWebService
{
/**
* @var int
*/
final public const LIMIT_MAX = 20;

/**
* @var int
*/
final public const TIMEOUT = 30;

protected int $timeout = self::TIMEOUT;
Expand All @@ -32,9 +39,6 @@
'Content-Type' => GuzzleSingleton::APPLICATION_JSON
];

/**
* @var Client
*/
private Client $client;

/**
Expand Down Expand Up @@ -130,7 +134,8 @@
$options['body'] = $body;
}

$guzzleResponse = $msgErr = null;
$guzzleResponse = null;
$msgErr = null;
try {
$guzzleResponse = $this->client->request($method, $endPoint, $options);
} catch (GuzzleException $guzzleException) {
Expand All @@ -144,7 +149,6 @@
throw new WsException($msgErr, 500, null);
}


/**
* Check response and jsondecode object
* @throws WsException|JsonException
Expand Down Expand Up @@ -174,17 +178,20 @@
throw new WsResponseException(WsException::RESP_EMPTY, 500, null);
}

if (
property_exists($jsonContent, "objects")
&& property_exists($jsonContent, "meta")
&& 0 === $jsonContent->meta->total_count
) {
throw new WsResponseException(WsException::RESP_EMPTY, 500, null);
if (!property_exists($jsonContent, "objects")) {
return $contents;
}

return $contents;
}
if (!property_exists($jsonContent, "meta")) {
return $contents;

Check warning on line 186 in src/AbstractWebService.php

View check run for this annotation

Codecov / codecov/patch

src/AbstractWebService.php#L186

Added line #L186 was not covered by tests
}

if (0 !== $jsonContent->meta->total_count) {
return $contents;
}

throw new WsResponseException(WsException::RESP_EMPTY, 500, null);

Check warning on line 193 in src/AbstractWebService.php

View check run for this annotation

Codecov / codecov/patch

src/AbstractWebService.php#L193

Added line #L193 was not covered by tests
}

/**
* @throws JsonException
Expand Down
34 changes: 29 additions & 5 deletions src/Exceptions/WsException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,49 @@
*/
class WsException extends \Exception implements \Stringable
{
/**
* @var string
*/
public const KEYS_ERROR = 'API key or API secret are null';

/**
* @var string
*/
public const GUZZLE_RESPONSE = 'Error response Guzzle: %s';

/**
* @var string
*/
public const ERR_READABLE = 'Response not readable';

/**
* @var string
*/
public const ERR_EMPTY = 'Empty response from endPoint "%s"';

/**
* @var string
*/
public const ERR_JSON = 'Not a JSON valid format :\n %s';

public const RESP_EMPTY = 'Astrobin doesn\'t find any objects, check your params';
/**
* @var string
*/
public const RESP_EMPTY = "Astrobin doesn't find any objects, check your params";

/**
* @var string
*/
public const EMPTY_ID = '%s is not a correct value, alphanumeric expected';

/**
* @var string
*/
public const ERR_DATE_FORMAT = 'Format "%s" is not a correct format for a date, please use YYYY-mm-dd instead';

/**
* @var string
*/
public const ERR_DATE = 'Error date format : \n';

/**
Expand All @@ -37,11 +64,8 @@ public function __construct(string $message, int $code, ?\Exception $previous)
parent::__construct(...func_get_args());
}

/**
* @return string
*/
public function __toString(): string
{
return self::class . "[{$this->getCode()}]: {$this->getMessage()}\n";
return self::class . sprintf('[%d]: %s%s', $this->getCode(), $this->getMessage(), PHP_EOL);
}
}
2 changes: 1 addition & 1 deletion src/Filters/CollectionFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ enum CollectionFilters: string
{
use EnumToArray;

case USER_FILTER = 'user';
case USER_FILTER = 'username';
}
21 changes: 21 additions & 0 deletions src/GuzzleSingleton.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,34 @@
*/
class GuzzleSingleton
{
/**
* @var string
*/
final public const ASTROBIN_URL = 'https://www.astrobin.com';

/**
* @var string
*/
final public const TIMEOUT = '0';

/**
* @var string
*/
final public const APPLICATION_JSON = 'application/json';

/**
* @var string
*/
final public const METHOD_GET = 'GET';

/**
* @var string
*/
final public const METHOD_POST = 'POST';

/**
* @var string
*/
final public const METHOD_PUT = 'PUT';

private static ?Client $_instance = null;
Expand Down
7 changes: 2 additions & 5 deletions src/Response/DTO/AstrobinError.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@
* Class AstrobinError
* @package AstrobinWs\Response
*/
final class AstrobinError implements AstrobinResponse
final readonly class AstrobinError implements AstrobinResponse
{
/**
* AstrobinError constructor.
*/
public function __construct(private readonly ?string $message)
public function __construct(private ?string $message)
{
}

Expand Down
2 changes: 1 addition & 1 deletion src/Response/DTO/Collection/ListCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class ListCollection extends AbstractResponse implements \IteratorAggregat
public array $listCollection;

/**
* @return CollectionIterator|\Traversable
* @return CollectionIterator
*/
public function getIterator(): CollectionIterator
{
Expand Down
1 change: 1 addition & 0 deletions src/Response/DTO/Collection/ListImages.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
final class ListImages implements \IteratorAggregate, AstrobinResponse
{
public array $listImages = [];

public int $count = 0;

public function getIterator(): ImageIterator
Expand Down
1 change: 1 addition & 0 deletions src/Response/DTO/Collection/ListToday.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
class ListToday extends AbstractResponse implements \IteratorAggregate, AstrobinResponse
{
public array $listToday;

public int $count = 0;

/**
Expand Down
18 changes: 12 additions & 6 deletions src/Response/DTO/Item/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,29 @@
final class Collection extends AbstractResponse implements AstrobinResponse
{
public int $id;

public string $name;

public ?string $description = null;


public string $user;
public string|\DateTime $date_created;
public string|\DateTime $date_updated;

public string|null|\DateTime $date_created = null;

public string|null|\DateTime $date_updated = null;

public ListImages|array|null $images = null;

public function setDateCreated(string $dateCreated): self
public function setDateCreated(?string $dateCreated): self
{
$this->date_created = \DateTime::createFromFormat(QueryFilters::DATE_FORMAT->value, $dateCreated);
$this->date_created = ((is_null($dateCreated)) ? null : \DateTime::createFromFormat(QueryFilters::DATE_FORMAT->value, $dateCreated)) ?: null;
return $this;
}

public function setDateUpdated(string $dateUpdated): self
public function setDateUpdated(?string $dateUpdated): self
{
$this->date_updated = \DateTime::createFromFormat(QueryFilters::DATE_FORMAT->value, $dateUpdated);
$this->date_updated = ((is_null($dateUpdated)) ? null : \DateTime::createFromFormat(QueryFilters::DATE_FORMAT->value, $dateUpdated)) ?: null;
return $this;
}
}
Loading