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

Keeper   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 167
Duplicated Lines 7.19 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 11
Bugs 1 Features 4
Metric Value
wmc 17
c 11
b 1
f 4
lcom 1
cbo 3
dl 12
loc 167
ccs 51
cts 51
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 0 12 3
A set() 0 4 1
A remove() 0 4 1
A getMax() 0 14 3
A getResponse() 6 15 3
A getModifiedResponse() 6 21 4
A reset() 0 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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