Completed
Push — 1.x ( 3a06bd...cc30df )
by Akihito
24s queued 12s
created

ResourceStorageCacheableTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 51
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __unserialize() 0 12 1
A __serialize() 0 11 1
A setInjector() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\QueryRepository;
6
7
use BEAR\RepositoryModule\Annotation\EtagPool;
8
use Psr\Cache\CacheItemPoolInterface;
9
use Ray\Di\Di\Inject;
10
use Ray\Di\InjectorInterface;
11
use Ray\PsrCacheModule\Annotation\Shared;
12
use Symfony\Component\Cache\Adapter\AdapterInterface;
13
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
14
15
use function assert;
16
17
trait ResourceStorageCacheableTrait
18
{
19
    use Php73BcSerializableTrait;
20
21
    /**
22
     * @var ?InjectorInterface
23
     * @psalm-suppress PropertyNotSetInConstructor
24
     */
25
    protected $injector;
26
27
    /**
28
     * @Inject
29
     */
30
    #[Inject]
31
    final public function setInjector(InjectorInterface $injector): void
32
    {
33
        $this->injector = $injector;
34
    }
35
36
    /**
37
     * @return array{logger: RepositoryLoggerInterface, purger: PurgerInterface, uriTag: UriTagInterface, saver: ResourceStorageSaver, knownTagTtl: float, injector: InjectorInterface}
38
     */
39
    final public function __serialize(): array
40
    {
41
        assert($this->injector instanceof InjectorInterface);
42
43
        return [
44
            'logger' => $this->logger,
45
            'purger' => $this->purger,
46
            'uriTag' => $this->uriTag,
47
            'saver' => $this->saver,
48
            'knownTagTtl' => $this->knownTagTtl,
49
            'injector' => $this->injector,
50
        ];
51
    }
52
53
    /**
54
     * @param array{logger: RepositoryLoggerInterface, purger: PurgerInterface, uriTag: UriTagInterface, saver: ResourceStorageSaver, knownTagTtl: float, injector: InjectorInterface} $data
55
     */
56
    final public function __unserialize(array $data): void
57
    {
58
        $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...
59
        $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...
60
        $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...
61
        $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...
62
        $pool = $data['injector']->getInstance(CacheItemPoolInterface::class, Shared::class);
63
        $etagPool = $data['injector']->getInstance(CacheItemPoolInterface::class, EtagPool::class);
64
        assert($pool instanceof AdapterInterface);
65
        assert($etagPool instanceof AdapterInterface);
66
        $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...
67
        $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...
68
    }
69
}
70