These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace BotMan\BotMan\Cache; |
||
4 | |||
5 | use Doctrine\Common\Cache\Cache; |
||
6 | use BotMan\BotMan\Interfaces\CacheInterface; |
||
7 | |||
8 | class DoctrineCache implements CacheInterface |
||
9 | { |
||
10 | /** |
||
11 | * @var Cache |
||
12 | */ |
||
13 | private $driver; |
||
14 | |||
15 | /** |
||
16 | * @param Cache $driver |
||
17 | */ |
||
18 | public function __construct(Cache $driver) |
||
19 | { |
||
20 | $this->driver = $driver; |
||
21 | } |
||
22 | |||
23 | /** |
||
24 | * Determine if an item exists in the cache. |
||
25 | * |
||
26 | * @param string $key |
||
27 | * @return bool |
||
28 | */ |
||
29 | public function has($key) |
||
30 | { |
||
31 | return $this->driver->contains($key); |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * Retrieve an item from the cache by key. |
||
36 | * |
||
37 | * @param string $key |
||
38 | * @param mixed $default |
||
39 | * @return mixed |
||
40 | */ |
||
41 | public function get($key, $default = null) |
||
42 | { |
||
43 | $value = $this->driver->fetch($key); |
||
44 | if ($value !== false) { |
||
45 | return $value; |
||
46 | } |
||
47 | |||
48 | return $default; |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * Retrieve an item from the cache and delete it. |
||
53 | * |
||
54 | * @param string $key |
||
55 | * @param mixed $default |
||
56 | * @return mixed |
||
57 | */ |
||
58 | public function pull($key, $default = null) |
||
59 | { |
||
60 | if ($this->has($key)) { |
||
61 | $cached = $this->get($key, $default); |
||
62 | $this->driver->delete($key); |
||
63 | |||
64 | return $cached; |
||
65 | } |
||
66 | |||
67 | return $default; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Store an item in the cache. |
||
72 | * |
||
73 | * @param string $key |
||
74 | * @param mixed $value |
||
75 | * @param \DateTime|int $minutes |
||
76 | * @return void |
||
77 | */ |
||
78 | View Code Duplication | public function put($key, $value, $minutes) |
|
0 ignored issues
–
show
|
|||
79 | { |
||
80 | if ($minutes instanceof \Datetime) { |
||
81 | $seconds = $minutes->getTimestamp() - time(); |
||
82 | } else { |
||
83 | $seconds = $minutes * 60; |
||
84 | } |
||
85 | |||
86 | $this->driver->save($key, $value, $seconds); |
||
87 | } |
||
88 | } |
||
89 |
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.