Passed
Push — spike ( 62103d )
by Akihito
03:02
created

ResourceStorageCacheableTrait::unserialize()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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