Completed
Push — master ( 38c1d8...ae312b )
by Joseph
03:32
created

Cache   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 94.74%

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 6
c 2
b 0
f 2
lcom 1
cbo 3
dl 0
loc 45
ccs 18
cts 19
cp 0.9474
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 0 10 2
A set() 0 10 1
A getExpireTime() 0 8 2
1
<?php
2
3
namespace Jclyons52\PagePreview\Cache;
4
5
use Jclyons52\PagePreview\Preview;
6
use Psr\Cache\CacheItemPoolInterface;
7
8
class Cache
9
{
10
    private $pool;
11
12 9
    public function __construct(CacheItemPoolInterface $pool)
13
    {
14 9
        $this->pool = $pool;
15 9
    }
16
17 9
    public function get($key)
18
    {
19 9
        $item = $this->pool->getItem(md5($key));
20
21 9
        $preview = unserialize($item->get());
22
23 9
        if ($preview instanceof Preview) {
24 6
            return $preview;
25
        }
26 3
    }
27
28 6
    public function set(Preview $preview, $expiresAt = null)
29
    {
30 6
        $item = $this->pool->getItem(md5($preview->url));
31
32 6
        $item->set(serialize($preview));
33
34 6
        $item->expiresAfter($this->getExpireTime($expiresAt));
35
36 6
        $this->pool->save($item);
37 6
    }
38
39
40
    /**
41
     * @param $expiresAt
42
     * @return \DateInterval
43
     */
44 6
    private function getExpireTime($expiresAt)
45
    {
46 6
        if ($expiresAt instanceof \DateInterval) {
47
            return $expiresAt;
48
        }
49
50 6
        return new \DateInterval('P10D');
51
    }
52
}
53