Skip to content

Commit 68177e3

Browse files
committed
fix #5 network exception issue
1 parent 1a16670 commit 68177e3

9 files changed

+630
-483
lines changed

Makefile

+3-10
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,11 @@ help: ## affiche cet aide
88
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
99

1010
.PHONY: lint
11-
lint: vendor/autoload.php ## affiche les erreurs de formatage de code
11+
lint: ## affiche les erreurs de formatage de code
1212
php vendor/bin/ecs
1313
php vendor/bin/phpstan
14+
php vendor/bin/rector --dry-run
1415

1516
.PHONY: test
16-
test: vendor/autoload.php ## lance les tests
17+
test: ## lance les tests
1718
php vendor/bin/phpunit
18-
19-
.PHONY: lint-fix
20-
lint-fix: vendor/autoload.php ## corrige les erreurs de formatage de code
21-
php vendor/bin/ecs --fix
22-
23-
vendor/autoload.php: composer.lock # installe les dépendances PHP
24-
composer update
25-
composer dump-autoload

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"squizlabs/php_codesniffer": "^3.7",
3232
"symplify/easy-coding-standard": "^11.0",
3333
"vimeo/psalm": "^4.24",
34-
"symfony/var-dumper": "^6.1|^7.0"
34+
"symfony/var-dumper": "^6.1|^7.0",
35+
"rector/rector": "^1.2"
3536
},
3637
"require": {
3738
"php": ">=8.2",

composer.lock

+576-433
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docker-compose.yaml

-10
This file was deleted.

phpstan.neon

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ parameters:
33
paths:
44
- src
55
- tests
6-
checkMissingIterableValueType: false
7-
checkGenericClassInNonGenericObjectType: false
6+
ignoreErrors:
7+
- identifier: missingType.generics
8+
- identifier: missingType.iterableValue

rector.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
7+
return RectorConfig::configure()
8+
->withPaths([
9+
__DIR__ . '/src',
10+
__DIR__ . '/tests',
11+
])
12+
->withPhpSets(php82: true)
13+
->withTypeCoverageLevel(0);

src/Client.php

+5-10
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
*
3131
* @author bernard-ng <[email protected]>
3232
*/
33-
class Client
33+
readonly class Client
3434
{
3535
private HttpClientInterface $http;
3636

@@ -266,17 +266,14 @@ public function collection(string $id, CollectionParameters $parameters = new Co
266266

267267
/**
268268
* @throws NetworkException
269-
* @noinspection PhpReturnValueOfMethodIsNeverUsedInspection
270269
*/
271270
private function createExceptionFromResponse(\Throwable $exception): never
272271
{
273272
if ($exception instanceof HttpExceptionInterface) {
274273
try {
275274
$response = $exception->getResponse();
276-
$body = $response->toArray(throw: false);
277275
throw NetworkException::create(
278-
message: $body['message'] ?? '',
279-
type: $body['error'],
276+
message: $response->getContent(false),
280277
status: $response->getStatusCode()
281278
);
282279
} catch (\Throwable $exception) {
@@ -309,11 +306,9 @@ private function getMappedData(string $type, array $data): Photos|Videos|Collect
309306
fn ($m) => $this->serializer->denormalize($m, type: Collection::class),
310307
$data['collections']
311308
),
312-
$mapped instanceof CollectionMedia => $mapped->media = array_map(function ($m) {
313-
return $m['type'] === 'Photo' ?
314-
$this->serializer->denormalize($m, type: Photo::class) :
315-
$this->serializer->denormalize($m, type: Video::class);
316-
}, $data['media'])
309+
$mapped instanceof CollectionMedia => $mapped->media = array_map(fn($m) => $m['type'] === 'Photo' ?
310+
$this->serializer->denormalize($m, type: Photo::class) :
311+
$this->serializer->denormalize($m, type: Video::class), $data['media'])
317312
};
318313

319314
return $mapped;

src/Exception/NetworkException.php

+8-12
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,20 @@ class NetworkException extends \Exception
1515
{
1616
public function __construct(
1717
string $message,
18-
?string $type = null,
19-
public ?int $status = null
18+
public ?int $status = null,
19+
public ?\Throwable $previous = null
2020
) {
21-
if ($this->status !== null) {
22-
parent::__construct($message . ' (HTTP ' . $status . '/' . $type . ')');
23-
} else {
24-
parent::__construct($message);
25-
}
21+
parent::__construct($message, previous: $this->previous);
2622
}
2723

28-
public static function create(string $message, string $type, int $status): self
24+
public static function create(string $message, int $status, ?\Throwable $previous = null): self
2925
{
3026
$message = empty($message) ? 'No message was provided' : $message;
3127
return match (true) {
32-
$status === 401 || $status === 429 => new AccountException($message, $type, $status),
33-
$status >= 400 && $status <= 499 => new ClientException($message, $type, $status),
34-
$status >= 500 && $status <= 599 => new ServerException($message, $type, $status),
35-
default => new self($message, $type, $status)
28+
$status === 401 || $status === 429 => new AccountException($message, $status, $previous),
29+
$status >= 400 && $status <= 499 => new ClientException($message, $status, $previous),
30+
$status >= 500 && $status <= 599 => new ServerException($message, $status, $previous),
31+
default => new self($message, $status, $previous)
3632
};
3733
}
3834
}

tests/ClientTest.php

+20-5
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,7 @@ final class ClientTest extends TestCase
2727
private function getPexels(callable|MockResponse $mock): Client
2828
{
2929
$pexels = new Client('your_token');
30-
$reflection = new ReflectionClass($pexels);
31-
32-
$http = $reflection->getProperty('http');
33-
$http->setAccessible(true);
34-
$http->setValue($pexels, new MockHttpClient($mock));
30+
$this->setValue($pexels, 'http', new MockHttpClient($mock));
3531

3632
return $pexels;
3733
}
@@ -138,4 +134,23 @@ public function testCollection(): void
138134
}
139135
}
140136
}
137+
138+
private function setValue(object &$object, string $propertyName, mixed $value): void
139+
{
140+
$reflectionClass = new ReflectionClass($object);
141+
142+
if ($reflectionClass->getProperty($propertyName)->isReadOnly()) {
143+
$mutable = $reflectionClass->newInstanceWithoutConstructor();
144+
145+
foreach ($reflectionClass->getProperties() as $property) {
146+
if ($property->isInitialized($object) && $property->name != $propertyName) {
147+
$reflectionClass->getProperty($property->name)->setValue($mutable, $property->getValue($object));
148+
}
149+
}
150+
151+
$object = $mutable;
152+
}
153+
154+
$reflectionClass->getProperty($propertyName)->setValue($object, $value);
155+
}
141156
}

0 commit comments

Comments
 (0)