Passed
Pull Request — 1.x (#118)
by Akihito
03:24 queued 59s
created

ResourceStorageCacheableTrait::__unserialize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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