Skip to content

Commit ba10e24

Browse files
committed
feat: added cache trait
1 parent 8f45323 commit ba10e24

File tree

6 files changed

+24
-24
lines changed

6 files changed

+24
-24
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
],
1414
"require": {
1515
"php": ">=8.1",
16+
"myclabs/deep-copy": "^1.11",
1617
"programmatordev/php-api-sdk": "^0.2.0",
1718
"programmatordev/yet-another-php-validator": "^1.1"
1819
},

docs/04-error-handling.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ try {
2323
$coordinate->getLongitude()
2424
);
2525
}
26-
// bad request to the api
26+
// bad request to the API
2727
catch (BadRequestException $exception) {
2828
echo $exception->getCode(); // 400
2929
echo $exception->getMessage();
@@ -39,7 +39,7 @@ catch (NotFoundException $exception) {
3939
echo $exception->getCode(); // 404
4040
echo $exception->getMessage();
4141
}
42-
// api key requests quota exceeded
42+
// API key requests quota exceeded
4343
catch (TooManyRequestsException $exception) {
4444
echo $exception->getCode(); // 429
4545
echo $exception->getMessage();
@@ -65,7 +65,7 @@ try {
6565
$coordinate->getLongitude()
6666
);
6767
}
68-
// catches all api response errors
68+
// catches all API response errors
6969
catch (ApiErrorException $exception) {
7070
echo $exception->getCode();
7171
echo $exception->getMessage();

src/Endpoint/Util/CacheTtlTrait.php

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/OpenWeatherMap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class OpenWeatherMap extends Api
2020
private array $options;
2121

2222
public function __construct(
23-
#[\SensitiveParameter] private readonly string $apiKey,
23+
#[\SensitiveParameter] private string $apiKey,
2424
array $options = []
2525
)
2626
{

src/Resource/GeocodingResource.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,21 @@
55
use ProgrammatorDev\Api\Method;
66
use ProgrammatorDev\OpenWeatherMap\Entity\Location;
77
use ProgrammatorDev\OpenWeatherMap\OpenWeatherMap;
8+
use ProgrammatorDev\OpenWeatherMap\Resource\Util\CacheTrait;
89
use ProgrammatorDev\OpenWeatherMap\Resource\Util\ValidationTrait;
910
use ProgrammatorDev\OpenWeatherMap\Util\EntityTrait;
1011
use ProgrammatorDev\Validator\Exception\ValidationException;
1112
use Psr\Http\Client\ClientExceptionInterface;
1213

1314
class GeocodingResource
1415
{
16+
use CacheTrait;
1517
use EntityTrait;
1618
use ValidationTrait;
1719

1820
private const NUM_RESULTS = 5;
1921

20-
public function __construct(private readonly OpenWeatherMap $api) {}
22+
public function __construct(private OpenWeatherMap $api) {}
2123

2224
/**
2325
* @return Location[]

src/Resource/Util/CacheTrait.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\OpenWeatherMap\Resource\Util;
4+
5+
use function DeepCopy\deep_copy;
6+
7+
trait CacheTrait
8+
{
9+
public function withCacheTtl(?int $ttl): static
10+
{
11+
$clone = deep_copy($this);
12+
$clone->api->getCacheBuilder()?->setTtl($ttl);
13+
14+
return $clone;
15+
}
16+
}

0 commit comments

Comments
 (0)