Completed
Push — master ( 178a08...5c0b6f )
by Oleg
05:22
created

ResourceValidator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
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
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 12
    public function __construct(ResourceManagerInterface $manager, $options = null)
37
    {
38 12
        parent::__construct($options);
39 12
        $this->manager = $manager;
40 12
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45 11
    public function isValid($value)
46
    {
47 11
        $all = $this->manager->getAllResources();
48
49 11
        if (!is_array($value)) {
50
            throw new InvalidArgumentException('Resources param needs to be an array.');
51
        }
52
53 11
        foreach ($value as $resource) {
54 11
            if (!in_array($resource, $all, true)) {
55 5
                $this->resource = $resource;
56 5
                $this->error(self::INVALID);
57
            }
58
        }
59
60 11
        return true;
61
    }
62
}
63