Test Failed
Push — master ( 138499...684014 )
by Dev
02:21
created

CacheTrait::deleteCacheFiles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace rOpenDev\Google;
4
5
use rOpenDev\Cache\SimpleCacheFile as fileCache;
6
use \Exception;
7
8
trait CacheTrait
9
{
10
    /** @var mixed Contain the cache folder for SERP results * */
11
    protected $cacheFolder = '/tmp';
12
13
    /** @var int Contain in seconds, the time cache is valid. Default 1 Day (86400). * */
14
    protected $cacheExpireTime = 86400;
15
16
    public function setCacheExpireTime($seconds)
17
    {
18
        $this->cacheExpireTime = $seconds;
19
20
        return $this;
21
    }
22
23
24
    /**
25
     * @param string $cache
26
     */
27
    public function setCacheFolder(?string $cache)
28
    {
29
        $this->cacheFolder = $cache;
30
31
        return $this;
32
    }
33
34
    /**
35
     * Delete cache file for a query ($q).
36
     *
37
     * @throws \Exception Where self::$cache is not set
38
     *
39
     * @return int Number of files deleted
40
     */
41
    public function deleteCacheFiles()
42
    {
43
        if (!$this->cacheFolder) {
44
            throw new Exception('Cache Folder is not defined : you can\'t delete cache files');
45
        }
46
47
        return $this->getCacheManager()->getMaintener()->deleteCacheFilesByPrefix();
48
    }
49
50
    /**
51
     * Return cache instance.
52
     *
53
     * @throws \Exception if the cache (folder, self::$cache via self::setCache) is not set
54
     *
55
     * @return \rOpenDev\cache\SimpleCacheFile
56
     */
57
    protected function getCacheManager()
58
    {
59
        if (!$this->cacheFolder) {
60
            return null;
61
        }
62
63
        $cachePrefix = md5($this->q).'_';
64
65
        return fileCache::instance($this->cacheFolder, $cachePrefix);
66
    }
67
68
    /**
69
     * Return a cache key | A vérifier avec chrome.
70
     *
71
     * @return string
72
     */
73
    protected function getCacheKey($url = null)
74
    {
75
        return sha1($this->page.(int) $this->mobile.':'.($url ? $url : $this->generateGoogleSearchUrl()));
0 ignored issues
show
Bug introduced by
It seems like generateGoogleSearchUrl() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

75
        return sha1($this->page.(int) $this->mobile.':'.($url ? $url : $this->/** @scrutinizer ignore-call */ generateGoogleSearchUrl()));
Loading history...
76
    }
77
78
    protected function getCache($url)
79
    {
80
        if ($this->cacheFolder) {
81
            $source = $this->getCacheManager()->get($this->getCacheKey($url), $this->cacheExpireTime);
82
            //$this->previousResultCacheKey = $cacheKey;
83
            return $source;
84
        }
85
86
        return false;
87
    }
88
89
    public function setCache($url, $source)
90
    {
91
        if ($this->cacheFolder) {
92
            $this->getCacheManager()->set($this->getCacheKey($url), $source);
93
        }
94
    }
95
}
96