Skip to content

Commit 0bb07b3

Browse files
authored
Merge pull request #21 from OpenClassrooms/remove_openclassrooms_cache
Remove openclassrooms/cache deps
2 parents 5c31950 + b8863ff commit 0bb07b3

File tree

40 files changed

+299
-220
lines changed

40 files changed

+299
-220
lines changed

.github/workflows/tests.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Tests
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- master
8+
9+
concurrency:
10+
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
11+
cancel-in-progress: ${{ github.ref != 'refs/heads/master' }}
12+
13+
jobs:
14+
15+
tests:
16+
runs-on: ubuntu-24.04
17+
strategy:
18+
matrix:
19+
php-versions: [ '8.2', '8.3', '8.4' ]
20+
fail-fast: false
21+
steps:
22+
- uses: actions/checkout@v4
23+
24+
- name: Setup PHP, with composer and extensions
25+
uses: shivammathur/setup-php@v2
26+
with:
27+
php-version: ${{ matrix.php-versions }}
28+
coverage: none
29+
30+
- name: Composer install
31+
run: composer install --no-scripts --no-interaction --prefer-dist -oa
32+
33+
- name: Run Phpunit
34+
run: ./vendor/bin/phpunit
35+
36+
- name: Run PHPStan
37+
run: ./vendor/bin/phpstan analyse src

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
composer.lock
33
composer.phar
44
vendor/
5+
.phpunit.result.cache

composer.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,14 @@
2626
},
2727
"require": {
2828
"php": ">=8.2",
29-
"doctrine/annotations": "~2.0",
30-
"openclassrooms/cache": "^v1.1.0"
29+
"doctrine/annotations": "^1.0|~2.0",
30+
"psr/log": "^2.0|^3.0",
31+
"psr/cache": "^2.0|^3.0"
3132
},
3233
"require-dev": {
33-
"phpunit/phpunit": "^9.6.20"
34+
"phpunit/phpunit": "^11.0",
35+
"phpstan/phpstan": "^2.1",
36+
"symfony/cache": "^5.4|^6.4|^7.0"
3437
},
3538
"extra": {
3639
"branch-alias": {

phpunit.xml.dist

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,18 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
3-
<phpunit
4-
backupGlobals = "false"
5-
backupStaticAttributes = "false"
6-
colors = "true"
7-
convertErrorsToExceptions = "true"
8-
convertNoticesToExceptions = "true"
9-
convertWarningsToExceptions = "true"
10-
processIsolation = "false"
11-
stopOnFailure = "false"
12-
syntaxCheck = "false"
13-
bootstrap = "./tests/bootstrap.php">
14-
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.5/phpunit.xsd"
4+
bootstrap="tests/bootstrap.php">
155
<testsuites>
16-
<testsuite name="Use Case Test Suite">
17-
<directory>./tests/</directory>
6+
<testsuite name="unit">
7+
<directory>tests/</directory>
188
</testsuite>
199
</testsuites>
20-
<filter>
21-
<whitelist>
22-
<directory suffix=".php">./src</directory>
23-
</whitelist>
24-
</filter>
25-
</phpunit>
2610

11+
<source>
12+
<include>
13+
<directory>src/</directory>
14+
</include>
15+
</source>
16+
17+
<coverage pathCoverage="true" />
18+
</phpunit>

src/OpenClassrooms/UseCase/Application/Services/Event/Exceptions/InvalidEventNameException.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace OpenClassrooms\UseCase\Application\Services\Event\Exceptions;
44

5-
use Exception;
6-
75
/**
86
* @author Romain Kuzniak <romain.kuzniak@turn-it-up.org>
97
*/

src/OpenClassrooms/UseCase/Application/Services/Proxy/Strategies/Impl/Cache/CacheProxyStrategy.php

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,30 @@
22

33
namespace OpenClassrooms\UseCase\Application\Services\Proxy\Strategies\Impl\Cache;
44

5-
use OpenClassrooms\Cache\Cache\Cache;
65
use OpenClassrooms\UseCase\Application\Services\Proxy\Strategies\Impl\DTO\ProxyStrategyResponseDTO;
76
use OpenClassrooms\UseCase\Application\Services\Proxy\Strategies\Requestors\Cache\CacheProxyStrategyRequest;
87
use OpenClassrooms\UseCase\Application\Services\Proxy\Strategies\Requestors\PostExecuteProxyStrategy;
98
use OpenClassrooms\UseCase\Application\Services\Proxy\Strategies\Requestors\PreExecuteProxyStrategy;
109
use OpenClassrooms\UseCase\Application\Services\Proxy\Strategies\Requestors\ProxyStrategy;
1110
use OpenClassrooms\UseCase\Application\Services\Proxy\Strategies\Requestors\ProxyStrategyRequest;
1211
use OpenClassrooms\UseCase\Application\Services\Proxy\Strategies\Responders\ProxyStrategyResponse;
12+
use Psr\Cache\CacheItemInterface;
13+
use Psr\Cache\CacheItemPoolInterface;
1314

1415
/**
1516
* @author Romain Kuzniak <romain.kuzniak@turn-it-up.org>
1617
*/
1718
class CacheProxyStrategy implements PreExecuteProxyStrategy, PostExecuteProxyStrategy
1819
{
19-
20-
/**
21-
* @var mixed
22-
*/
23-
private $data;
24-
2520
/**
26-
* @var Cache
21+
* @var CacheItemPoolInterface
2722
*/
2823
private $cache;
2924

3025
/**
3126
* @var bool
3227
*/
33-
private $postExecute = true;
28+
private $postExecute;
3429

3530
/**
3631
* @return string
@@ -46,28 +41,14 @@ public function getType()
4641
public function preExecute(ProxyStrategyRequest $proxyStrategyRequest)
4742
{
4843
/** @var CacheProxyStrategyRequest $proxyStrategyRequest */
49-
$this->data = $this->cache->fetchWithNamespace(
44+
$item = $this->fetchWithNamespace(
5045
$proxyStrategyRequest->getId(),
5146
$proxyStrategyRequest->getNamespaceId()
5247
);
5348

54-
if ($this->responseIsInCache()) {
55-
$stopExecution = true;
56-
$this->postExecute = false;
57-
} else {
58-
$stopExecution = false;
59-
}
60-
$response = new ProxyStrategyResponseDTO($this->data, $stopExecution);
61-
62-
return $response;
63-
}
49+
$this->postExecute = !$item->isHit();
6450

65-
/**
66-
* @return bool
67-
*/
68-
private function responseIsInCache()
69-
{
70-
return $this->data;
51+
return new ProxyStrategyResponseDTO($item->get(), $item->isHit());
7152
}
7253

7354
/**
@@ -76,17 +57,51 @@ private function responseIsInCache()
7657
public function postExecute(ProxyStrategyRequest $proxyStrategyRequest)
7758
{
7859
/** @var CacheProxyStrategyRequest $proxyStrategyRequest */
79-
$saved = $this->cache->saveWithNamespace(
60+
$item = $this->saveWithNamespace(
8061
$proxyStrategyRequest->getId(),
8162
$proxyStrategyRequest->getData(),
8263
$proxyStrategyRequest->getNamespaceId(),
8364
$proxyStrategyRequest->getLifeTime()
8465
);
85-
$response = new ProxyStrategyResponseDTO($saved, false);
8666

87-
return $response;
67+
return new ProxyStrategyResponseDTO($item->get(), false);
8868
}
8969

70+
private function fetchWithNamespace(string $id, string $namespace = null): CacheItemInterface
71+
{
72+
if ($namespace !== null) {
73+
$namespaceId = $this->cache->getItem($namespace);
74+
75+
$id = ((string) $namespaceId->get()) . $id;
76+
}
77+
78+
return $this->cache->getItem($id);
79+
}
80+
81+
private function saveWithNamespace(string $id, mixed $data, string $namespace = null, int $lifetime = null): CacheItemInterface
82+
{
83+
if ($namespace !== null) {
84+
$namespaceId = $this->cache->getItem($namespace);
85+
86+
if (!$namespaceId->isHit()) {
87+
$namespaceId->set($namespace . '_' . random_int(0, 10000));
88+
$namespaceId->expiresAfter(604800); // 7 days
89+
90+
$this->cache->save($namespaceId);
91+
}
92+
93+
$id = ((string) $namespaceId->get()) . $id;
94+
}
95+
96+
$item = $this->cache->getItem($id);
97+
$item->set($data)->expiresAfter($lifetime);
98+
99+
$this->cache->save($item);
100+
101+
return $item;
102+
}
103+
104+
90105
/**
91106
* @return boolean
92107
*/
@@ -95,7 +110,7 @@ public function isPostExecute()
95110
return $this->postExecute;
96111
}
97112

98-
public function setCache(Cache $cache)
113+
public function setCache(CacheItemPoolInterface $cache)
99114
{
100115
$this->cache = $cache;
101116
}

src/OpenClassrooms/UseCase/Application/Services/Proxy/Strategies/Impl/CacheProxyStrategyBagImpl.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ class CacheProxyStrategyBagImpl extends ProxyStrategyBag
2828

2929
public function isPostExecute()
3030
{
31-
/** @var $this ->proxyStrategy */
32-
3331
return $this->proxyStrategy->isPostExecute();
3432
}
3533
}

src/OpenClassrooms/UseCase/Application/Services/Proxy/Strategies/Impl/Transaction/TransactionProxyStrategy.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ public function preExecute(ProxyStrategyRequest $proxyStrategyRequest)
4545
*/
4646
public function postExecute(ProxyStrategyRequest $proxyStrategyRequest)
4747
{
48-
return $this->transaction->commit();
48+
$this->transaction->commit();
49+
50+
return new ProxyStrategyResponseDTO();
4951
}
5052

5153
/**
@@ -54,8 +56,10 @@ public function postExecute(ProxyStrategyRequest $proxyStrategyRequest)
5456
public function onException(ProxyStrategyRequest $proxyStrategyRequest)
5557
{
5658
if ($this->transaction->isTransactionActive()) {
57-
return $this->transaction->rollBack();
59+
$this->transaction->rollBack();
5860
}
61+
62+
return new ProxyStrategyResponseDTO();
5963
}
6064

6165
public function setTransaction(Transaction $transaction)

src/OpenClassrooms/UseCase/Application/Services/Proxy/Strategies/Requestors/OnExceptionProxyStrategy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
interface OnExceptionProxyStrategy extends ProxyStrategy
1111
{
1212
/**
13-
* @return ProxyStrategyResponse
13+
* @return ?bool
1414
*/
1515
public function onException(ProxyStrategyRequest $proxyStrategyRequest);
1616
}

src/OpenClassrooms/UseCase/Application/Services/Proxy/UseCases/UseCaseProxyBuilder.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace OpenClassrooms\UseCase\Application\Services\Proxy\UseCases;
44

55
use Doctrine\Common\Annotations\Reader;
6-
use OpenClassrooms\Cache\Cache\Cache;
76
use OpenClassrooms\UseCase\Application\Services\Event\EventFactory;
87
use OpenClassrooms\UseCase\Application\Services\Event\EventSender;
98
use OpenClassrooms\UseCase\Application\Services\Proxy\Strategies\Impl\Cache\CacheProxyStrategy;
@@ -28,6 +27,7 @@
2827
use OpenClassrooms\UseCase\Application\Services\Security\Security;
2928
use OpenClassrooms\UseCase\Application\Services\Transaction\Transaction;
3029
use OpenClassrooms\UseCase\BusinessRules\Requestors\UseCase;
30+
use Psr\Cache\CacheItemPoolInterface;
3131
use Psr\Log\LoggerInterface;
3232

3333
/**
@@ -42,7 +42,7 @@ abstract class UseCaseProxyBuilder
4242
protected $useCaseProxy;
4343

4444
/**
45-
* @var Cache
45+
* @var CacheItemPoolInterface
4646
*/
4747
private $cache;
4848

@@ -85,7 +85,7 @@ abstract public function create(UseCase $useCase);
8585
/**
8686
* @return UseCaseProxyBuilder
8787
*/
88-
public function withCache(Cache $cache = null)
88+
public function withCache(CacheItemPoolInterface $cache = null)
8989
{
9090
$this->cache = $cache;
9191

0 commit comments

Comments
 (0)