ActionConstraintValidator::getInvalidActions()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
nc 3
nop 1
dl 0
loc 20
ccs 11
cts 11
cp 1
crap 3
rs 9.9332
c 2
b 0
f 0
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of FlexPHP.
4
 *
5
 * (c) Freddie Gar <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace FlexPHP\Schema\Validators\Constraints;
11
12
use FlexPHP\Schema\Constants\Action;
13
use Symfony\Component\Validator\Constraints\Choice;
14
use Symfony\Component\Validator\Constraints\Count;
15
use Symfony\Component\Validator\Constraints\Type;
16
use Symfony\Component\Validator\ConstraintViolationListInterface;
17
use Symfony\Component\Validator\Validation;
18
19
/**
20
 * @Annotation
21
 */
22
class ActionConstraintValidator
23
{
24
    private const ACTIONS = [
25
        Action::ALL,
26
        Action::INDEX,
27
        Action::CREATE,
28
        Action::READ,
29
        Action::UPDATE,
30
        Action::PATCH,
31
        Action::DELETE,
32
    ];
33
34
    /**
35
     * @param mixed $value
36
     */
37 52
    public function validate($value): ConstraintViolationListInterface
38
    {
39 52
        if (($errors = $this->validateType($value))->count()) {
40 8
            return $errors;
41
        }
42
43 46
        if (empty($value)) {
44
            $value = Action::ALL;
45
        }
46
47 46
        $invalid = $this->getInvalidActions($value);
48
49 46
        $validator = Validation::createValidator();
50
51 46
        return $validator->validate($invalid, [
52 46
            new Count([
53 46
                'max' => 0,
54 46
                'maxMessage' => 'Allowed values are: ' . \implode(',', self::ACTIONS),
55
            ]),
56
        ]);
57
    }
58
59
    /**
60
     * @param mixed $value
61
     */
62 52
    private function validateType($value): ConstraintViolationListInterface
63
    {
64 52
        $validator = Validation::createValidator();
65
66 52
        return $validator->validate($value, [
67 52
            new Type([
68 52
                'type' => 'string',
69
                'message' => 'Logic: [it must be string type]',
70
            ]),
71
        ]);
72
    }
73
74 46
    private function getInvalidActions(string $value): array
75
    {
76 46
        $invalid = [];
77 46
        $actions = \explode(',', $value);
78
79 46
        $validator = Validation::createValidator();
80
81 46
        foreach ($actions as $action) {
82 46
            $errors = $validator->validate($action, [
83 46
                new Choice([
84 46
                    'choices' => self::ACTIONS,
85
                ]),
86
            ]);
87
88 46
            if (\count($errors) > 0) {
89 2
                $invalid[] = $action;
90
            }
91
        }
92
93 46
        return $invalid;
94
    }
95
}
96