Psr6Storage   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 99
ccs 0
cts 41
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A remember() 0 10 2
A restore() 0 10 2
A touch() 0 4 1
A store() 0 13 2
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\Storage\Drivers;
11
12
use Psr\Cache\CacheException;
13
use Psr\Cache\CacheItemInterface;
14
use Psr\Cache\CacheItemPoolInterface;
15
use Psr\Cache\InvalidArgumentException;
16
use Railt\Io\Readable;
17
use Railt\Storage\Storage;
18
19
/**
20
 * Class Psr6Storage
21
 */
22
class Psr6Storage implements Storage
23
{
24
    private const DEFAULT_REMEMBER_TIME = 60 * 5;
25
26
    /**
27
     * @var int
28
     */
29
    private $timeout;
30
31
    /**
32
     * @var CacheItemPoolInterface
33
     */
34
    private $pool;
35
36
    /**
37
     * @var \Closure
38
     */
39
    private $persist;
40
41
    /**
42
     * Psr6Storage constructor.
43
     * @param CacheItemPoolInterface $pool
44
     * @param \Closure $persist
45
     * @param int $timeout
46
     */
47
    public function __construct(
48
        CacheItemPoolInterface $pool,
49
        \Closure $persist,
50
        int $timeout = self::DEFAULT_REMEMBER_TIME
51
    ) {
52
        $this->pool = $pool;
53
        $this->persist = $persist;
54
        $this->timeout = $timeout;
55
    }
56
57
    /**
58
     * @param Readable $readable
59
     * @param \Closure $then
60
     * @return object|mixed
61
     * @throws \InvalidArgumentException
62
     * @throws InvalidArgumentException
63
     * @throws \Exception
64
     */
65
    public function remember(Readable $readable, \Closure $then)
66
    {
67
        $exists = $this->pool->hasItem($readable->getHash());
68
69
        if ($exists) {
70
            return $this->restore($readable);
71
        }
72
73
        return $this->store($readable, $then($readable));
74
    }
75
76
    /**
77
     * @param Readable $readable
78
     * @return object|mixed
79
     * @throws \InvalidArgumentException
80
     */
81
    private function restore(Readable $readable)
82
    {
83
        try {
84
            $result = $this->pool->getItem($readable->getHash());
85
86
            return $this->touch($result)->get();
87
        } catch (InvalidArgumentException | \Throwable $fatal) {
88
            throw new \InvalidArgumentException($fatal->getMessage(), 0, $fatal);
89
        }
90
    }
91
92
    /**
93
     * @param CacheItemInterface $item
94
     * @return CacheItemInterface
95
     */
96
    private function touch(CacheItemInterface $item): CacheItemInterface
97
    {
98
        return $item->expiresAfter(\time() + $this->timeout);
99
    }
100
101
    /**
102
     * @param Readable $readable
103
     * @param mixed $data
104
     * @return mixed
105
     * @throws \Exception
106
     */
107
    private function store(Readable $readable, $data)
108
    {
109
        try {
110
            /** @var CacheItemInterface $item */
111
            $item = ($this->persist)($readable, $data);
112
            $this->touch($item);
113
            $this->pool->save($item);
114
        } catch (CacheException $error) {
115
            throw $error->getPrevious();
0 ignored issues
show
Bug introduced by
The method getPrevious() does not seem to exist on object<Psr\Cache\CacheException>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
116
        }
117
118
        return $data;
119
    }
120
}
121