Completed
Push — master ( e6f9e9...ade0a3 )
by Peter
03:33
created

Keeper::getModifiedResponse()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 21
Code Lines 12

Duplication

Lines 6
Ratio 28.57 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 6
loc 21
ccs 13
cts 13
cp 1
rs 9.0534
cc 4
eloc 12
nc 8
nop 4
crap 4
1
<?php
2
/**
3
 * AnimeDb package.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2014, Peter Gribanov
7
 * @license   http://opensource.org/licenses/MIT
8
 */
9
namespace AnimeDb\Bundle\CacheTimeKeeperBundle\Service;
10
11
use AnimeDb\Bundle\CacheTimeKeeperBundle\Exception\NotModifiedException;
12
use AnimeDb\Bundle\CacheTimeKeeperBundle\Service\Driver\DriverInterface;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
16
class Keeper
17
{
18
    /**
19
     * Key for last update of the project.
20
     *
21
     * @var string
22
     */
23
    const LAST_UPDATE_KEY = 'last-update';
24
25
    /**
26
     * @var DriverInterface
27
     */
28
    protected $driver;
29
30
    /**
31
     * @param DriverInterface $driver
32
     */
33 19
    public function __construct(DriverInterface $driver)
34
    {
35 19
        $this->driver = $driver;
36 19
    }
37
38
    /**
39
     * Get time for key.
40
     *
41
     * @param string $key
42
     *
43
     * @return \DateTime
44
     */
45 3
    public function get($key)
46
    {
47 3
        if (!($time = $this->driver->get($key))) {
48 2
            if ($key == self::LAST_UPDATE_KEY) {
49 1
                $time = $this->reset();
50 1
            } else {
51 1
                $time = $this->get(self::LAST_UPDATE_KEY);
52
            }
53 2
        }
54
55 3
        return $time;
56
    }
57
58
    /**
59
     * Set time for key.
60
     *
61
     * @param string $key
62
     * @param \DateTime $time
63
     *
64
     * @return bool
65
     */
66 1
    public function set($key, \DateTime $time)
67
    {
68 1
        return $this->driver->set($key, $time);
69
    }
70
71
    /**
72
     * Remove time for key.
73
     *
74
     * @param string $key
75
     *
76
     * @return bool
77
     */
78 2
    public function remove($key)
79
    {
80 2
        return $this->driver->remove($key);
81
    }
82
83
    /**
84
     * Get a list of keys or dates and chooses the max date.
85
     *
86
     * @param mixed $params
87
     *
88
     * @return \DateTime
89
     */
90 13
    public function getMax($params = [])
91
    {
92 13
        $params = (array) $params;
93
        // always check the date of the last update of the project
94 13
        if (!in_array(self::LAST_UPDATE_KEY, $params)) {
95 11
            $params[] = self::LAST_UPDATE_KEY;
96 11
        }
97
98 13
        if (!($time = $this->driver->getMax($params))) {
99 4
            $time = $this->reset();
100 4
        }
101
102 13
        return $time;
103
    }
104
105
    /**
106
     * Get cache response.
107
     *
108
     * Set $lifetime as < 0 for not set max-age
109
     *
110
     * @param mixed $params
111
     * @param int $lifetime
112
     * @param Response|null $response
113
     *
114
     * @return Response
115
     */
116 2
    public function getResponse($params = [], $lifetime = -1, Response $response = null)
117
    {
118 2
        if (!$response) {
119 1
            $response = new Response();
120 1
        }
121
122 2 View Code Duplication
        if ($lifetime > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
123
            $response
124 1
                ->setMaxAge($lifetime)
125 1
                ->setSharedMaxAge($lifetime)
126 1
                ->setExpires((new \DateTime())->modify('+'.$lifetime.' seconds'));
127 1
        }
128
129 2
        return $response->setLastModified($this->getMax($params));
130
    }
131
132
    /**
133
     * Get only modified response.
134
     *
135
     * Throw exception if response was not modified for this request
136
     *
137
     * Set $lifetime as < 0 for not set max-age
138
     *
139
     * @throws NotModifiedException
140
     *
141
     * @param Request $request
142
     * @param mixed $params
143
     * @param int $lifetime
144
     * @param Response|null $response
145
     *
146
     * @return Response
147
     */
148 3
    public function getModifiedResponse(Request $request, $params = [], $lifetime = -1, Response $response = null)
149
    {
150 3
        if (!$response) {
151 1
            $response = new Response();
152 1
        }
153
154 3 View Code Duplication
        if ($lifetime > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
155
            $response
156 1
                ->setMaxAge($lifetime)
157 1
                ->setSharedMaxAge($lifetime)
158 1
                ->setExpires((new \DateTime())->modify('+'.$lifetime.' seconds'));
159 1
        }
160
161 3
        $response->setLastModified($this->getMax($params));
162
163 3
        if ($response->isNotModified($request)) {
164 1
            throw new NotModifiedException($response);
165
        }
166
167 2
        return $response;
168
    }
169
170
    /**
171
     * Reset last update date.
172
     *
173
     * @return \DateTime
174
     */
175 5
    private function reset()
176
    {
177 5
        $time = new \DateTime();
178 5
        $this->driver->set(self::LAST_UPDATE_KEY, $time);
179
180 5
        return $time;
181
    }
182
}
183