Completed
Push — master ( e7f482...a802c2 )
by Peter
09:28
created

Keeper::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * AnimeDb package
4
 *
5
 * @package   AnimeDb
6
 * @author    Peter Gribanov <[email protected]>
7
 * @copyright Copyright (c) 2014, Peter Gribanov
8
 * @license   http://opensource.org/licenses/MIT
9
 */
10
11
namespace AnimeDb\Bundle\CacheTimeKeeperBundle\Service;
12
13
use AnimeDb\Bundle\CacheTimeKeeperBundle\Service\Driver\DriverInterface;
14
use Symfony\Component\HttpFoundation\Response;
15
16
/**
17
 * Keeper
18
 *
19
 * @package AnimeDb\Bundle\CacheTimeKeeperBundle\Service
20
 * @author  Peter Gribanov <[email protected]>
21
 */
22
class Keeper
23
{
24
    /**
25
     * Key for last update of the project
26
     *
27
     * @var string
28
     */
29
    const LAST_UPDATE_KEY = 'last-update';
30
31
    /**
32
     * @var DriverInterface
33
     */
34
    protected $driver;
35
36
    /**
37
     * @param DriverInterface $driver
38
     */
39 16
    public function __construct(DriverInterface $driver)
40
    {
41 16
        $this->driver = $driver;
42 16
    }
43
44
    /**
45
     * Get time for key
46
     *
47
     * @param string $key
48
     *
49
     * @return \DateTime
50
     */
51 3
    public function get($key)
52
    {
53 3
        if (!($time = $this->driver->get($key))) {
54 2
            if ($key == self::LAST_UPDATE_KEY) {
55 1
                $time = $this->reset();
56
            } else {
57 1
                $time = $this->get(self::LAST_UPDATE_KEY);
58
            }
59
        }
60
61 3
        return $time;
62
    }
63
64
    /**
65
     * Set time for key
66
     *
67
     * @param string $key
68
     * @param \DateTime $time
69
     *
70
     * @return boolean
71
     */
72 1
    public function set($key, \DateTime $time)
73
    {
74 1
        return $this->driver->set($key, $time);
75
    }
76
77
    /**
78
     * Remove time for key
79
     *
80
     * @param string $key
81
     *
82
     * @return boolean
83
     */
84 2
    public function remove($key)
85
    {
86 2
        return $this->driver->remove($key);
87
    }
88
89
    /**
90
     * Get a list of keys or dates and chooses the max date
91
     *
92
     * @param mixed $params
93
     *
94
     * @return \DateTime
95
     */
96 10
    public function getMax($params = [])
97
    {
98 10
        $params = (array)$params;
99
        // always check the date of the last update of the project
100 10
        if (!in_array(self::LAST_UPDATE_KEY, $params)) {
101 8
            $params[] = self::LAST_UPDATE_KEY;
102
        }
103
104 10
        if (!($time = $this->driver->getMax($params))) {
105 4
            $time = $this->reset();
106
        }
107
108 10
        return $time;
109
    }
110
111
    /**
112
     * Get cache response
113
     *
114
     * Set $lifetime as < 0 for not set max-age
115
     *
116
     * @param mixed $params
117
     * @param integer $lifetime
118
     * @param Response|null $response
119
     *
120
     * @return Response
121
     */
122 2
    public function getResponse($params = [], $lifetime = -1, Response $response = null)
123
    {
124 2
        if (!$response) {
125 1
            $response = new Response();
126
        }
127
128 2
        if ($lifetime > 0) {
129
            $response
130 1
                ->setMaxAge($lifetime)
131 1
                ->setSharedMaxAge($lifetime)
132 1
                ->setExpires((new \DateTime())->modify('+'.$lifetime.' seconds'));
133
        }
134
135
        return $response
136 2
            ->setPublic()
137 2
            ->setLastModified($this->getMax($params));
138
    }
139
140
    /**
141
     * Reset last update date
142
     *
143
     * @return \DateTime
144
     */
145 5
    private function reset()
146
    {
147 5
        $time = new \DateTime();
148 5
        $this->driver->set(self::LAST_UPDATE_KEY, $time);
149 5
        return $time;
150
    }
151
}
152