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

Cache::getExpireTime()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2.0625
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