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

ResourceStorageCacheableTrait::unserialize()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 15
c 2
b 0
f 0
nc 4
nop 1
dl 0
loc 22
rs 9.7666
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
            // @codeCoverageIgnoreStart
63
        } catch (Error $e) {
64
            throw new ResourceStorageUnserializeException($e->getMessage());
65
            // @codeCoverageIgnoreEnd
66
        }
67
    }
68
69
    /**
70
     * @param array{logger: RepositoryLoggerInterface, purger: PurgerInterface, uriTag: UriTagInterface, saver: ResourceStorageSaver, knownTagTtl: float, injector: InjectorInterface} $data
71
     */
72
    private function unserialize(array $data): void
73
    {
74
        $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...
75
        $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...
76
        $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...
77
        $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...
78
        $pool = $data['injector']->getInstance(CacheItemPoolInterface::class, Shared::class);
79
        try {
80
            $maybeEtagPool = $data['injector']->getInstance(CacheItemPoolInterface::class, EtagPool::class);
81
            // @codeCoverageIgnoreStart
82
        } catch (Unbound $e) {
83
            $maybeEtagPool = null;
84
            // @codeCoverageIgnoreEnd
85
        }
86
87
        assert($pool instanceof AdapterInterface);
88
        /** @psalm-suppress all */
89
        $etagPool = $maybeEtagPool instanceof AdapterInterface ? $maybeEtagPool : $pool;
90
        $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...
91
        $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...
92
        $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...
93
        $this->injector = $data['injector'];
94
    }
95
}
96