Passed
Pull Request — 1.x (#118)
by Akihito
02:50
created

ResourceStorageCacheableTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 7
eloc 30
c 5
b 0
f 0
dl 0
loc 69
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __unserialize() 0 6 2
A __serialize() 0 11 1
A unserialize() 0 20 3
A setInjector() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\QueryRepository;
6
7
use BEAR\QueryRepository\Exception\ResourceStorageUnserializeException;
8
use BEAR\RepositoryModule\Annotation\EtagPool;
9
use BEAR\Resource\LoggerInterface;
10
use Error;
11
use Psr\Cache\CacheItemPoolInterface;
12
use Ray\Di\Di\Inject;
13
use Ray\Di\Exception\Unbound;
14
use Ray\Di\InjectorInterface;
15
use Ray\PsrCacheModule\Annotation\Shared;
16
use Symfony\Component\Cache\Adapter\AdapterInterface;
17
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
18
19
use function assert;
20
21
trait ResourceStorageCacheableTrait
22
{
23
    /**
24
     * @var ?InjectorInterface
25
     * @psalm-suppress PropertyNotSetInConstructor
26
     */
27
    protected $injector;
28
29
    /**
30
     * @Inject
31
     */
32
    #[Inject]
33
    final public function setInjector(InjectorInterface $injector): void
34
    {
35
        $this->injector = $injector;
36
    }
37
38
    /**
39
     * @return array{logger: RepositoryLoggerInterface, purger: PurgerInterface, uriTag: UriTagInterface, saver: ResourceStorageSaver, knownTagTtl: float, injector: InjectorInterface}
40
     */
41
    final public function __serialize(): array
42
    {
43
        assert($this->injector instanceof InjectorInterface);
44
45
        return [
46
            'logger' => $this->logger,
47
            'purger' => $this->purger,
48
            'uriTag' => $this->uriTag,
49
            'saver' => $this->saver,
50
            'knownTagTtl' => $this->knownTagTtl,
51
            'injector' => $this->injector,
52
        ];
53
    }
54
55
    /**
56
     * @param array{logger: RepositoryLoggerInterface, purger: PurgerInterface, uriTag: UriTagInterface, saver: ResourceStorageSaver, knownTagTtl: float, injector: InjectorInterface} $data
57
     */
58
    final public function __unserialize(array $data): void
59
    {
60
        try {
61
            $this->unserialize($data);
62
        } catch (Error $e) {
63
            throw new ResourceStorageUnserializeException($e->getMessage());
64
        }
65
    }
66
67
    /**
68
     * @param array{logger: RepositoryLoggerInterface, purger: PurgerInterface, uriTag: UriTagInterface, saver: ResourceStorageSaver, knownTagTtl: float, injector: InjectorInterface} $data
69
     */
70
    private function unserialize(array $data): void
71
    {
72
        $this->logger = $data['logger'];
0 ignored issues
show
Bug Best Practice introduced by
The property logger does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
73
        $this->purger = $data['purger'];
0 ignored issues
show
Bug Best Practice introduced by
The property purger does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
74
        $this->uriTag = $data['uriTag'];
0 ignored issues
show
Bug Best Practice introduced by
The property uriTag does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
75
        $this->saver = $data['saver'];
0 ignored issues
show
Bug Best Practice introduced by
The property saver does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
76
        $pool = $data['injector']->getInstance(CacheItemPoolInterface::class, Shared::class);
77
        try {
78
            $maybeEtagPool = $data['injector']->getInstance(CacheItemPoolInterface::class, EtagPool::class);
79
        } catch (Unbound $e) {
80
            $maybeEtagPool = null;
81
        }
82
83
        assert($pool instanceof AdapterInterface);
84
        /** @psalm-suppress all */
85
        $etagPool = $maybeEtagPool instanceof AdapterInterface ? $maybeEtagPool : $pool;
86
        $this->roPool = new TagAwareAdapter($pool, $etagPool, $data['knownTagTtl']);
0 ignored issues
show
Bug Best Practice introduced by
The property roPool does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
87
        $this->etagPool = new TagAwareAdapter($etagPool, $etagPool, $data['knownTagTtl']);
0 ignored issues
show
Bug Best Practice introduced by
The property etagPool does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
88
        $this->knownTagTtl = $data['knownTagTtl'];
0 ignored issues
show
Bug Best Practice introduced by
The property knownTagTtl does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
89
        $this->injector = $data['injector'];
90
    }
91
}
92