CacheManager   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
c 2
b 0
f 0
lcom 1
cbo 3
dl 0
loc 60
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getInstance() 0 8 2
A save() 0 7 1
A load() 0 13 2
A urlToKey() 0 4 1
1
<?php
2
3
namespace Perry\Cache;
4
5
use Perry\Setup;
6
7
class CacheManager
8
{
9
    /**
10
     * @var CacheManager
11
     */
12
    private static $myInstance = null;
13
14
    /**
15
     * @return CacheManager
16
     */
17
    public static function getInstance()
18
    {
19
        if (is_null(self::$myInstance)) {
20
            self::$myInstance = new CacheManager();
21
        }
22
23
        return self::$myInstance;
24
    }
25
26
    /**
27
     * @param string $url
28
     * @param array $data
29
     * @return bool
30
     */
31
    public function save($url, $data)
32
    {
33
        return Setup::getInstance()
34
            ->cacheImplementation
35
            ->getItem($this->urlToKey($url))
36
            ->set($data, Setup::$cacheTTL);
37
    }
38
39
    /**
40
     * @param string $url
41
     * @return array|false
42
     */
43
    public function load($url)
44
    {
45
        $data = Setup::getInstance()
46
            ->cacheImplementation
47
            ->getItem($this->urlToKey($url));
48
49
50
        if ($data->isHit()) {
51
            return $data->get();
52
        }
53
54
        return false;
55
    }
56
57
    /**
58
     * make sure the key is in a format that complies with PSR-6
59
     * @param string $url
60
     * @return string
61
     */
62
    private function urlToKey($url)
63
    {
64
        return sha1($url);
65
    }
66
}
67