Completed
Push — master ( 3eb757...3bc7c8 )
by Oleg
03:46
created

ResourceValidator::isValid()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.016

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 9
cts 10
cp 0.9
rs 9.6666
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 4.016
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DataFlowServer\Authorization\Validation;
5
6
use SlayerBirden\DataFlowServer\Authorization\ResourceManagerInterface;
7
use Zend\Validator\AbstractValidator;
8
use Zend\Validator\Exception\InvalidArgumentException;
9
10
final class ResourceValidator extends AbstractValidator
11
{
12
    const INVALID = 'invalidResource';
13
14
    /**
15
     * @var null|ResourceManagerInterface
16
     */
17
    private $manager;
18
    /**
19
     * @var string
20
     */
21
    protected $resource;
22
23
    /**
24
     * @var array Error message templates
25
     */
26
    protected $messageTemplates = [
27
        self::INVALID => "Resource '%resource%' is invalid and is not among the list of known resources.",
28
    ];
29
    /**
30
     * @var array Error message template variables
31
     */
32
    protected $messageVariables = [
33
        'resource' => 'resource'
34
    ];
35
36 38
    public function __construct(ResourceManagerInterface $manager, $options = null)
37
    {
38 38
        parent::__construct($options);
39 38
        $this->manager = $manager;
40 38
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45 34
    public function isValid($value)
46
    {
47 34
        $all = $this->manager->getAllResources();
48
49 34
        if (!is_array($value)) {
50
            throw new InvalidArgumentException('Resources param needs to be an array.');
51
        }
52
53 34
        foreach ($value as $resource) {
54 34
            if (!in_array($resource, $all, true)) {
55 4
                $this->resource = $resource;
56 4
                $this->error(self::INVALID);
57 4
                return false;
58
            }
59
        }
60
61 32
        return true;
62
    }
63
}
64