Completed
Push — 1.x ( d9db55...434f1d )
by Akihito
21s queued 12s
created

ResourceStorageCacheableTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __serialize() 0 11 1
A setInjector() 0 4 1
A __unserialize() 0 7 2
A unserialize() 0 22 3
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 Error;
10
use Psr\Cache\CacheItemPoolInterface;
11
use Ray\Di\Di\Inject;
12
use Ray\Di\Exception\Unbound;
13
use Ray\Di\InjectorInterface;
14
use Ray\PsrCacheModule\Annotation\Shared;
15
use Symfony\Component\Cache\Adapter\AdapterInterface;
16
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
17
18
use function assert;
19
20
trait ResourceStorageCacheableTrait
21
{
22
    /**
23
     * @var ?InjectorInterface
24
     * @psalm-suppress PropertyNotSetInConstructor
25
     */
26
    protected $injector;
27
28
    /**
29
     * @Inject
30
     */
31
    #[Inject]
32
    final public function setInjector(InjectorInterface $injector): void
33
    {
34
        $this->injector = $injector;
35
    }
36
37
    /**
38
     * @return array{logger: RepositoryLoggerInterface, purger: PurgerInterface, uriTag: UriTagInterface, saver: ResourceStorageSaver, knownTagTtl: float, injector: InjectorInterface}
39
     */
40
    final public function __serialize(): array
41
    {
42
        assert($this->injector instanceof InjectorInterface);
43
44
        return [
45
            'logger' => $this->logger,
46
            'purger' => $this->purger,
47
            'uriTag' => $this->uriTag,
48
            'saver' => $this->saver,
49
            'knownTagTtl' => $this->knownTagTtl,
50
            'injector' => $this->injector,
51
        ];
52
    }
53
54
    /**
55
     * @param array{logger: RepositoryLoggerInterface, purger: PurgerInterface, uriTag: UriTagInterface, saver: ResourceStorageSaver, knownTagTtl: float, injector: InjectorInterface} $data
56
     */
57
    final public function __unserialize(array $data): void
58
    {
59
        try {
60
            $this->unserialize($data);
61
            // @codeCoverageIgnoreStart
62
        } catch (Error $e) {
63
            throw new ResourceStorageUnserializeException($e->getMessage());
64
            // @codeCoverageIgnoreEnd
65
        }
66
    }
67
68
    /**
69
     * @param array{logger: RepositoryLoggerInterface, purger: PurgerInterface, uriTag: UriTagInterface, saver: ResourceStorageSaver, knownTagTtl: float, injector: InjectorInterface} $data
70
     */
71
    private function unserialize(array $data): void
72
    {
73
        $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...
74
        $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...
75
        $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...
76
        $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...
77
        $pool = $data['injector']->getInstance(CacheItemPoolInterface::class, Shared::class);
78
        try {
79
            $maybeEtagPool = $data['injector']->getInstance(CacheItemPoolInterface::class, EtagPool::class);
80
            // @codeCoverageIgnoreStart
81
        } catch (Unbound $e) {
82
            $maybeEtagPool = null;
83
            // @codeCoverageIgnoreEnd
84
        }
85
86
        assert($pool instanceof AdapterInterface);
87
        /** @psalm-suppress all */
88
        $etagPool = $maybeEtagPool instanceof AdapterInterface ? $maybeEtagPool : $pool;
89
        $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...
90
        $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...
91
        $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...
92
        $this->injector = $data['injector'];
93
    }
94
}
95