Completed
Push — master ( be9b77...ef7016 )
by Limon
02:29
created

AdblockParser::clearCache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 3
cts 4
cp 0.75
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
crap 2.0625
1
<?php
2
namespace Limonte;
3
4
class AdblockParser
5
{
6
    private $rules;
7
8
    private $cacheExpire = 1; // 1 day
9
10
    const CACHE_FOLDER = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
11
12 10
    public function __construct($rules = [])
13
    {
14 10
        $this->rules = [];
15 10
        $this->addRules($rules);
16 10
    }
17
18
    /**
19
     * @param  string[]  $rules
20
     */
21 10
    public function addRules($rules)
22
    {
23 10
        foreach ($rules as $rule) {
24
            try {
25 9
                $this->rules[] = new AdblockRule($rule);
26 9
            } catch (InvalidRuleException $e) {
27
                // Skip invalid rules
28
            }
29
        }
30
31
        // Sort rules, eceptions first
32 10
        usort($this->rules, function ($a, $b) {
33 4
            return (int)$a->isException() < (int)$b->isException();
34 10
        });
35 10
    }
36
37
    /**
38
     * @param  string|array  $path
39
     */
40 3
    public function loadRules($path)
41
    {
42
        // single resource
43 3
        if (is_string($path)) {
44 3
            if (filter_var($path, FILTER_VALIDATE_URL)) {
45 2
                $content = $this->getCachedResource($path);
46
            } else {
47 1
                $content = @file_get_contents($path);
48
            }
49 3
            if ($content) {
50 3
                $rules = preg_split("/(\r\n|\n|\r)/", $content);
51 3
                $this->addRules($rules);
52
            }
53
        // array of resources
54 2
        } elseif (is_array($path)) {
55 2
            foreach ($path as $item) {
56 2
                $this->loadRules($item);
57
            }
58
        }
59 3
    }
60
61
    /**
62
     * @return  array
63
     */
64 1
    public function getRules()
65
    {
66 1
        return $this->rules;
67
    }
68
69
    /**
70
     * @param  string  $url
71
     *
72
     * @return integer
73
     */
74 10
    public function shouldBlock($url)
75
    {
76 10
        $url = trim($url);
77
78 10
        if (filter_var($url, FILTER_VALIDATE_URL) === false) {
79 1
            throw new \Exception("Invalid URL");
80
        }
81
82 9
        foreach ($this->rules as $rule) {
83 9
            if ($rule->isComment() || $rule->isHtml()) {
84 4
                continue;
85
            }
86
87 9
            if ($rule->matchUrl($url)) {
88 9
                if ($rule->isException()) {
89 2
                    return false;
90
                }
91 9
                return true;
92
            }
93
        }
94
95 6
        return false;
96
    }
97
98
    /**
99
     * @param  string  $url
100
     *
101
     * @return boolean
102
     */
103 7
    public function shouldNotBlock($url)
104
    {
105 7
        return !$this->shouldBlock($url);
106
    }
107
108
    /**
109
     * Get external resources cache expire (in days)
110
     *
111
     * @return integer
112
     */
113 1
    public function getCacheExpire()
114
    {
115 1
        return $this->cacheExpire;
116
    }
117
118
    /**
119
     * Set external resources cache expire (in days)
120
     *
121
     * @param  integer  $expire
0 ignored issues
show
Documentation introduced by
There is no parameter named $expire. Did you maybe mean $expireInDays?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
122
     */
123
    public function setCacheExpire($expireInDays)
124
    {
125
        $this->cacheExpire = $expireInDays;
126
    }
127
128
    /**
129
     * Clear external resources cache
130
     */
131 1
    public function clearCache()
132
    {
133 1
        foreach (glob(self::CACHE_FOLDER . '*') as $file) {
134
            unlink($file);
135
        }
136 1
    }
137
138
    /**
139
     * @param  string  $url
140
     *
141
     * @return string
142
     */
143 2
    private function getCachedResource($url)
144
    {
145 2
        $cacheFile = self::CACHE_FOLDER . basename($url) . md5($url);
146
147 2
        if (file_exists($cacheFile) && (filemtime($cacheFile) > (time() - 60 * 24 * $this->cacheExpire))) {
148
            // Cache file is less than five minutes old.
149
            // Don't bother refreshing, just use the file as-is.
150 1
            $content = @file_get_contents($cacheFile);
151
        } else {
152
            // Our cache is out-of-date, so load the data from our remote server,
153
            // and also save it over our cache for next time.
154 1
            $content = @file_get_contents($url);
155 1
            file_put_contents($cacheFile, $content, LOCK_EX);
156
        }
157
158 2
        return $content;
159
    }
160
}
161