Passed
Push — develop ( 5c61b7...a7d981 )
by Freddie
03:02
created

SchemaAttributeLogicValidation   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 21
c 2
b 0
f 0
dl 0
loc 34
ccs 12
cts 12
cp 1
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A validate() 0 14 3
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\Validations;
11
12
use FlexPHP\Schema\Exception\InvalidSchemaAttributeException;
13
use FlexPHP\Schema\SchemaAttributeInterface;
14
use FlexPHP\Schema\Validators\Logics\ActionLogicValidator;
15
use FlexPHP\Schema\Validators\Logics\AiLogicValidator;
16
use FlexPHP\Schema\Validators\Logics\DefaultLogicValidator;
17
use FlexPHP\Schema\Validators\Logics\FcharsLogicValidator;
18
use FlexPHP\Schema\Validators\Logics\FcheckLogicValidator;
19
use FlexPHP\Schema\Validators\Logics\FormatLogicValidator;
20
use FlexPHP\Schema\Validators\Logics\PkLogicValidator;
21
use FlexPHP\Schema\Validators\Logics\TypeLogicValidator;
22
use Throwable;
23
24
class SchemaAttributeLogicValidation implements ValidationInterface
25
{
26
    private const VALIDATORS = [
27
        PkLogicValidator::class,
28
        AiLogicValidator::class,
29
        TypeLogicValidator::class,
30
        FormatLogicValidator::class,
31
        FcharsLogicValidator::class,
32
        FcheckLogicValidator::class,
33
        ActionLogicValidator::class,
34
        DefaultLogicValidator::class,
35
    ];
36
37
    private SchemaAttributeInterface $property;
38
39 283
    public function __construct(SchemaAttributeInterface $property)
40
    {
41 283
        $this->property = $property;
42 283
    }
43
44 283
    public function validate(): void
45
    {
46 283
        if (empty($this->property->constraints())) {
47 39
            return;
48
        }
49
50 255
        $name = 'Logic: [' . $this->property->name() . '] ';
51
52
        try {
53 255
            \array_map(function ($validator): void {
54 255
                (new $validator($this->property))->validate();
55 255
            }, self::VALIDATORS);
56 92
        } catch (Throwable $throwable) {
57 92
            throw new InvalidSchemaAttributeException($name . $throwable->getMessage());
58
        }
59 163
    }
60
}
61