Test Failed
Push — master ( e8bb53...a68ccf )
by Dev
03:23 queued 42s
created

CacheTrait::previousRequestUsedCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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

80
        $url = $url ?: $this->/** @scrutinizer ignore-call */ generateGoogleSearchUrl();
Loading history...
81
        $url = preg_replace('/&(gbv=1|sei=([a-z0-9]+)(&|$))/i', '&', $url);
82
        $url = trim($url, '&');
83
84
        return sha1($this->page.(int) $this->mobile.':'.$url);
85
    }
86
87
    protected function getCache($url): string
88
    {
89
        if ($this->cacheFolder) {
90
            $source = $this->getCacheManager()->get($this->getCacheKey($url), $this->cacheExpireTime);
91
            $this->previousRequestUsedCache = true;
92
93
            return $source;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $source could return the type false which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
94
        }
95
96
        return '';
97
    }
98
99
    public function setCache($url, $source): void
100
    {
101
        if ($this->cacheFolder) {
102
            $this->getCacheManager()->set($this->getCacheKey($url), $source);
103
            $this->previousRequestUsedCache = false;
104
        }
105
    }
106
}
107