Completed
Push — master ( c97b8a...48b53c )
by Kanstantsin
03:13
created

ExternalCacheStatefulFile::saveState()   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
/**
7
 * Class ExternalCacheStatefulFile
8
 */
9
class ExternalCacheStatefulFile extends ExternalFile implements ICacheStateful
10
{
11
    /**
12
     * @todo: formalize cache config.
13
     * @xample (current)
14
     * ```php
15
     *  [
16
     *      'formatter-name' => 123456789,
17
     *      ...
18
     *  ],
19
     * ```
20
     * @example (for todo)
21
     * ```php
22
     *  [
23
     *      'formatter-name' => [
24
     *          'is_cached' => true|false,
25
     *          'cached_at' => 123456789,
26
     *          'empty' => true|false,
27
     *          'error' => true|false,
28
     *      ],
29
     *      ...
30
     *  ],
31
     * ```
32
     * @var array
33
     */
34
    protected $cachedState = [];
35
    /**
36
     * @var \Closure
37
     */
38
    protected $saveStateCallback;
39
40
    /**
41
     * @param array $cachedState
42
     */
43
    public function setCachedState(array $cachedState): void
44
    {
45
        $this->cachedState = $cachedState;
46
    }
47
48
    /**
49
     * @return array
50
     */
51
    public function getCachedState(): array
52
    {
53
        return $this->cachedState;
54
    }
55
56
    /**
57
     * @param string $format
58
     *
59
     * @see Factory::$formatterConfigArray
60
     * @return int
61
     */
62
    public function getCachedAt(string $format): ?int
63
    {
64
        $cachedAt = $this->cachedState[$format] ?? null;
65
        if ($cachedAt <= 0) {
66
            $cachedAt = null;
67
        }
68
69
        return $cachedAt;
70
    }
71
72
    /**
73
     * @param string $format
74
     * @param int|null $cachedAt
75
     */
76
    public function setCachedAt(string $format, ?int $cachedAt): void
77
    {
78
        $this->cachedState[$format] = $cachedAt;
79
        if ($cachedAt === null || $cachedAt <= 0) {
80
            unset($this->cachedState[$format]);
81
        }
82
    }
83
84
    /**
85
     * @param string $format
86
     *
87
     * @return bool
88
     */
89
    public function getIsCached(string $format): bool
90
    {
91
        return $this->getCachedAt($format) !== null;
92
    }
93
94
    /**
95
     * @param \Closure $callback
96
     */
97
    public function setSaveStateCallback(\Closure $callback): void
98
    {
99
        $this->saveStateCallback = $callback;
100
    }
101
102
    /**
103
     * @return bool
104
     */
105
    public function saveState(): bool
106
    {
107
        if (!($this->saveStateCallback instanceof \Closure)) {
0 ignored issues
show
introduced by
$this->saveStateCallback is always a sub-type of Closure.
Loading history...
108
            return true;
109
        }
110
111
        return (bool) \call_user_func($this->saveStateCallback, $this);
112
    }
113
}