Passed
Push — master ( c6992b...62cc34 )
by Kanstantsin
02:33
created

ExternalCacheStatefulFile::saveCachedState()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
ccs 0
cts 6
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace tkanstantsin\fileupload\model;
5
6
class ExternalCacheStatefulFile extends ExternalFile implements ICacheStateful
7
{
8
    /**
9
     * @var array
10
     */
11
    private $cachedState = [];
12
    /**
13
     * @var \Closure
14
     */
15
    private $saveCachedStateCallback;
16
17
    /**
18
     * @param array $cachedState
19
     */
20
    public function setCachedStateArray(array $cachedState): void
21
    {
22
        $this->cachedState = $cachedState;
23
    }
24
25
    /**
26
     * @return array
27
     */
28
    public function getCachedStateArray(): array
29
    {
30
        return $this->cachedState;
31
    }
32
33
    /**
34
     * @param string $format
35
     *
36
     * @see Factory::$formatterConfigArray
37
     * @return int
38
     */
39
    public function getCachedAt(string $format): ?int
40
    {
41
        $cachedAt = $this->cachedState[$format] ?? null;
42
        if ($cachedAt <= 0) {
43
            $cachedAt = null;
44
        }
45
46
        return $cachedAt;
47
    }
48
49
    /**
50
     * @param string $format
51
     * @param int $cachedAt
52
     */
53
    public function setCachedAt(string $format, int $cachedAt): void
54
    {
55
        $this->cachedState[$format] = $cachedAt;
56
        if ($cachedAt <= 0) {
57
            unset($this->cachedState[$format]);
58
        }
59
    }
60
61
    /**
62
     * @param string $format
63
     *
64
     * @return bool
65
     */
66
    public function getIsCached(string $format): bool
67
    {
68
        return $this->getCachedAt($format) !== null;
69
    }
70
71
    /**
72
     * @param \Closure $callback
73
     */
74
    public function setSaveCacheCallback(\Closure $callback): void
75
    {
76
        $this->saveCachedStateCallback = $callback;
77
    }
78
79
    /**
80
     * @return bool
81
     */
82
    public function saveCachedState(): bool
83
    {
84
        if (!($this->saveCachedStateCallback instanceof \Closure)) {
0 ignored issues
show
introduced by
$this->saveCachedStateCallback is always a sub-type of Closure.
Loading history...
85
            return true;
86
        }
87
88
        return (bool)\call_user_func($this->saveCachedStateCallback, $this);
89
    }
90
}