Completed
Push — master ( 185853...b7deb5 )
by Marcel
01:56
created

Psr6Cache::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace BotMan\BotMan\Cache;
4
5
use Psr\Cache\CacheItemPoolInterface;
6
use BotMan\BotMan\Interfaces\CacheInterface;
7
8
class Psr6Cache implements CacheInterface
9
{
10
    /**
11
     * @var CacheItemPoolInterface
12
     */
13
    protected $adapter;
14
15
    /**
16
     * @param CacheItemPoolInterface $adapter
17
     */
18
    public function __construct(CacheItemPoolInterface $adapter)
19
    {
20
        $this->adapter = $adapter;
21
    }
22
23
    /**
24
     * @param string $key
25
     * @return bool
26
     */
27
    public function has($key)
28
    {
29
        return $this->adapter->hasItem($key);
30
    }
31
32
    /**
33
     * @param string $key
34
     * @param null $default
35
     * @return mixed|null
36
     */
37 View Code Duplication
    public function get($key, $default = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
38
    {
39
        $item = $this->adapter->getItem($key);
40
        if ($item->isHit()) {
41
            return $item->get();
42
        }
43
44
        return $default;
45
    }
46
47
    /**
48
     * @param string $key
49
     * @param null $default
50
     * @return null
51
     */
52 View Code Duplication
    public function pull($key, $default = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
53
    {
54
        $item = $this->adapter->getItem($key);
55
        if ($item->isHit()) {
56
            $this->adapter->deleteItem($key);
57
58
            return $item->get();
59
        }
60
61
        return $default;
62
    }
63
64
    /**
65
     * @param string $key
66
     * @param mixed $value
67
     * @param \DateTime|int $minutes
68
     */
69
    public function put($key, $value, $minutes)
70
    {
71
        $item = $this->adapter->getItem($key);
72
        $item->set($value);
73
74
        if ($minutes instanceof \DateTimeInterface) {
75
            $item->expiresAt($minutes);
76
        } else {
77
            $item->expiresAfter(new \DateInterval(sprintf('PT%dM', $minutes)));
78
        }
79
80
        $this->adapter->save($item);
81
    }
82
}
83