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

ResourceValidator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 53
ccs 12
cts 13
cp 0.9231
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A isValid() 0 17 4
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