Completed
Pull Request — master (#1)
by Indra
02:41
created

AbstractFilter   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 72
ccs 31
cts 31
cp 1
rs 10
c 0
b 0
f 0
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getNames() 0 3 1
A getExcept() 0 3 1
A isSupportsMethod() 0 15 6
A __construct() 0 23 3
A getOnly() 0 3 1
1
<?php
2
3
/*
4
 * This file is part of the MiddlewareBundle
5
 *
6
 * (c) Indra Gunawan <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Indragunawan\MiddlewareBundle\Annotation;
13
14
use Webmozart\Assert\Assert;
15
16
/**
17
 * @author Indra Gunawan <[email protected]>
18
 */
19
abstract class AbstractFilter
20
{
21
    /**
22
     * @var array
23
     */
24
    protected $names;
25
26
    /**
27
     * @var array
28
     */
29
    protected $only;
30
31
    /**
32
     * @var array
33
     */
34
    protected $except;
35
36 16
    public function __construct(array $values)
37
    {
38 16
        $names = (array) ($values['value'] ?? []);
39 16
        $only = (array) ($values['only'] ?? []);
40 16
        $except = (array) ($values['except'] ?? []);
41
42 16
        Assert::minCount($names, 1);
43 15
        Assert::allStringNotEmpty($names);
44 12
        Assert::allStringNotEmpty($only);
45 10
        Assert::allStringNotEmpty($except);
46
47 8
        unset($values['value'], $values['only'], $values['except']);
48 8
        if (\count($values) > 0) {
49 1
            throw new \InvalidArgumentException(sprintf('The annotation @%s does not have a property named "%s".', \get_class($this), implode('", "', array_keys($values))));
50
        }
51
52 7
        if ($result = \array_intersect($only, $except)) {
53 1
            throw new \InvalidArgumentException(sprintf('You cannot put "%s" in "only" and "except" at the same time.', implode('", "', $result)));
54
        }
55
56 6
        $this->names = $names;
57 6
        $this->only = $only;
58 6
        $this->except = $except;
59 6
    }
60
61 5
    public function getNames(): array
62
    {
63 5
        return $this->names;
64
    }
65
66 5
    public function getOnly(): array
67
    {
68 5
        return $this->only;
69
    }
70
71 5
    public function getExcept(): array
72
    {
73 5
        return $this->except;
74
    }
75
76 1
    public function isSupportsMethod(string $method): bool
77
    {
78 1
        if (empty($this->only) && empty($this->except)) {
79 1
            return true;
80
        }
81
82 1
        if (\in_array($method, $this->only, true)) {
83 1
            return true;
84
        }
85
86 1
        if (!empty($this->except) && !\in_array($method, $this->except, true)) {
87 1
            return true;
88
        }
89
90 1
        return false;
91
    }
92
}
93