Passed
Push — main ( a145c5...a6206c )
by Jeroen
02:18
created

CacheRouteLoaderDecorator::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 16
ccs 9
cts 9
cp 1
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jerowork\RouteAttributeProvider\RouteLoader\Cache;
6
7
use Generator;
8
use Jerowork\RouteAttributeProvider\RouteLoader\RouteLoaderInterface;
9
use JsonException;
10
use Psr\SimpleCache\CacheInterface;
11
12
final class CacheRouteLoaderDecorator implements RouteLoaderInterface
13
{
14
    use CreateCacheKeyTrait;
15
    use MapCachePayloadToLoadedRoutesTrait;
0 ignored issues
show
Bug introduced by
The type Jerowork\RouteAttributeP...loadToLoadedRoutesTrait was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
    use MapLoadedRoutesToCachePayloadTrait;
0 ignored issues
show
Bug introduced by
The type Jerowork\RouteAttributeP...utesToCachePayloadTrait was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
18
    private RouteLoaderInterface $routeLoader;
19
    private CacheInterface $cache;
20
21 3
    public function __construct(RouteLoaderInterface $routeLoader, CacheInterface $cache)
22
    {
23 3
        $this->routeLoader = $routeLoader;
24 3
        $this->cache       = $cache;
25 3
    }
26
27
    /**
28
     * @throws JsonException
29
     */
30 2
    public function load(string $className): Generator
31
    {
32 2
        $cacheKey = $this->createCacheKey($className);
33 2
        $payload  = $this->cache->get($cacheKey);
34
35 2
        if ($payload !== null) {
36 1
            yield from $this->mapCachePayloadToLoadedRoutes($payload);
37
38 1
            return;
39
        }
40
41 1
        $loadedRoutes = iterator_to_array($this->routeLoader->load($className));
42
43 1
        $this->cache->set($cacheKey, $this->mapLoadedRoutesToCachePayload(...$loadedRoutes));
44
45 1
        yield from $loadedRoutes;
46 1
    }
47
}
48