Passed
Push — develop ( 7cbbb4...22c94d )
by Stephen
02:36
created

Filter::parseFilterString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 2
c 5
b 0
f 0
dl 0
loc 7
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Spinen\BrowserFilter\Stack;
4
5
use Illuminate\Http\Request;
6
use Spinen\BrowserFilter\Exceptions\InvalidFilterTypeException;
7
use Spinen\BrowserFilter\Filter as CoreFilter;
8
9
/**
10
 * Class Filter
11
 *
12
 * @package Spinen\BrowserFilter\Stack
13
 */
14
class Filter extends CoreFilter
15
{
16
    /**
17
     * @inheritDoc
18
     */
19
    protected $block_filter = true;
20
21
    /**
22
     * @inheritDoc
23
     */
24 1
    public function generateCacheKey(Request $request)
25
    {
26
        // Append the rules with the version of the browser to allow new rules to bust the cache
27 1
        return md5(json_encode($this->config->get($this->config_path . 'rules', []))) .
28 1
            ':' .
29 1
            parent::generateCacheKey($request);
30
    }
31
32
    /**
33
     * @inheritDoc
34
     */
35 4
    public function parseFilterString($filter_string)
36
    {
37
        // NOTE: $filter_string is unused, but needed to match signature of the method.
38
39 4
        $this->setFilterType($this->config->get($this->config_path . 'type'));
40
41 3
        $this->rules = $this->config->get($this->config_path . 'rules', []);
42 3
    }
43
44
    /**
45
     * Set the filter type.
46
     *
47
     * @param string $type
48
     *
49
     * @return void
50
     *
51
     * @throws InvalidFilterTypeException
52
     */
53 4
    protected function setFilterType(string $type)
54
    {
55 4
        if ('allow' === $type) {
56 2
            $this->block_filter = false;
57
58 2
            return;
59
        }
60
61 2
        if ('block' === $type) {
62 1
            $this->block_filter = true;
63
64 1
            return;
65
        }
66
67 1
        throw new InvalidFilterTypeException(
68 1
            sprintf(
69 1
                "Invalid filter type [%s] was given. Only allow or block are permitted.",
70
                $type
71
            )
72
        );
73
    }
74
}
75