DoctrineCacheAdapter::saveDeferred()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
/*
3
 * This file is part of the Egils\Component\Cache package.
4
 *
5
 * (c) Egidijus Lukauskas <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Egils\Component\Cache\Adapter;
12
13
use Doctrine\Common\Cache\CacheProvider;
14
use Egils\Component\Cache\CacheItem;
15
use Psr\Cache\CacheItemInterface;
16
use Psr\Cache\CacheItemPoolInterface;
17
use DateTime;
18
19
class DoctrineCacheAdapter implements CacheItemPoolInterface
20
{
21
    /** @var CacheProvider */
22
    private $provider;
23
24
    /** @var array|CacheItemInterface[] */
25
    private $deferred = [];
26
27 12
    public function __construct(CacheProvider $provider)
28
    {
29 12
        $this->provider = $provider;
30 12
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 5
    public function getItem($key)
36
    {
37 5
        if (isset($this->deferred[$key])) {
38 1
            return $this->deferred[$key];
39
        }
40
41 4
        if (false === $this->provider->contains($key)) {
42 2
            return new CacheItem($key);
43
        }
44
45 3
        $item = $this->provider->fetch($key);
46 3
        $item->setHit(true);
47
48 3
        return $item;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 3
    public function getItems(array $keys = [])
55
    {
56 3
        if (true === empty($keys)) {
57 1
            return [];
58
        }
59
60 2
        $items = [];
61 2
        foreach ($keys as $key) {
62 2
            $items[$key] = $this->getItem($key);
63 2
        }
64
65 2
        return $items;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 1
    public function clear()
72
    {
73 1
        return $this->provider->flushAll();
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 1
    public function deleteItems(array $keys)
80
    {
81 1
        foreach ($keys as $key) {
82 1
            if (true === $this->provider->contains($key)) {
83 1
                $this->provider->delete($key);
84 1
            }
85 1
        }
86
87 1
        return $this;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 2
    public function save(CacheItemInterface $item)
94
    {
95 2
        $this->doSave($item);
96
97 2
        return $this;
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103 3
    public function saveDeferred(CacheItemInterface $item)
104
    {
105 3
        $this->deferred[$item->getKey()] = $item;
106
107 3
        return $this;
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113 2
    public function commit()
114
    {
115 2
        $result = true;
116 2
        foreach ($this->deferred as $key => $deferred) {
117 2
            $saveResult = $this->doSave($deferred);
118 2
            if (true === $saveResult) {
119 2
                unset($this->deferred[$key]);
120 2
            }
121 2
            $result = $result && $saveResult;
122 2
        }
123
124 2
        return $result;
125
    }
126
127 4
    private function doSave(CacheItemInterface $item)
128
    {
129 4
        $now = new DateTime();
130 4
        $ttl = $item->getExpiration()->format('U') - $now->format('U');
131
132 4
        if ($ttl < 0) {
133 1
            return false;
134
        }
135
136 3
        return $this->provider->save($item->getKey(), $item, $ttl);
137
    }
138
}
139