CacheAttributesReader   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 84
rs 10
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A read() 0 20 2
A normalizeToCache() 0 9 2
A cacheKey() 0 9 2
A normalizeFromCache() 0 10 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Kafkiansky\SymfonyMiddleware\Attribute\Reader;
6
7
use Kafkiansky\SymfonyMiddleware\Attribute\Middleware;
8
use Psr\Cache\CacheItemPoolInterface;
9
use Psr\Http\Server\MiddlewareInterface;
10
11
final class CacheAttributesReader implements AttributeReader
12
{
13
    private CacheItemPoolInterface $cache;
14
    private AttributeReader $delegate;
15
16
    public function __construct(CacheItemPoolInterface $cache, AttributeReader $delegate)
17
    {
18
        $this->cache = $cache;
19
        $this->delegate = $delegate;
20
    }
21
22
    /**
23
     * @param object $class
24
     * @param string|null $method
25
     *
26
     * @throws \Psr\Cache\InvalidArgumentException
27
     * @throws \ReflectionException
28
     *
29
     * @return Middleware[]
30
     */
31
    public function read(object $class, ?string $method = null): array
32
    {
33
        $cacheKey = self::cacheKey($class, $method);
34
35
        $item = $this->cache->getItem($cacheKey);
36
37
        if ($item->isHit()) {
38
            /** @var array $cachedMiddlewares */
39
            $cachedMiddlewares = $item->get();
40
41
            $middlewares = $this->normalizeFromCache($cachedMiddlewares);
42
        } else {
43
            $middlewares = $this->delegate->read($class, $method);
44
45
            $item->set($this->normalizeToCache($middlewares));
46
47
            $this->cache->save($item);
48
        }
49
50
        return $middlewares;
51
    }
52
53
    /**
54
     * @param array $cachedMiddlewares
55
     *
56
     * @return Middleware[]
57
     */
58
    private function normalizeFromCache(array $cachedMiddlewares): array
59
    {
60
        $middlewares = [];
61
62
        /** @var class-string<MiddlewareInterface>[]|string[] $item */
63
        foreach ($cachedMiddlewares as $item) {
64
            $middlewares[] = Middleware::fromArray($item);
65
        }
66
67
        return $middlewares;
68
    }
69
70
    /**
71
     * @param Middleware[] $middlewares
72
     *
73
     * @return array
74
     */
75
    private function normalizeToCache(array $middlewares): array
76
    {
77
        $normalized = [];
78
79
        foreach ($middlewares as $middleware) {
80
            $normalized[] = $middleware->toArray();
81
        }
82
83
        return $normalized;
84
    }
85
86
    private static function cacheKey(object $class, ?string $method = null): string
87
    {
88
        $key = 'symfony.middleware.'.str_replace('\\', '', get_class($class));
89
90
        if ($method !== null) {
91
            $key .= '.'.$method;
92
        }
93
94
        return $key;
95
    }
96
}
97