PkLogicValidator::validate()   B
last analyzed

Complexity

Conditions 8
Paths 6

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 9
c 1
b 0
f 0
nc 6
nop 0
dl 0
loc 17
ccs 10
cts 10
cp 1
crap 8
rs 8.4444
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\Logics;
11
12
use FlexPHP\Schema\Exception\InvalidSchemaAttributeException;
13
use FlexPHP\Schema\SchemaAttributeInterface;
14
use FlexPHP\Schema\Traits\AttributeHelperTrait;
15
use FlexPHP\Schema\Validations\ValidationInterface;
16
17
class PkLogicValidator implements ValidationInterface
18
{
19
    use AttributeHelperTrait;
20
21
    private SchemaAttributeInterface $property;
22
23 255
    public function __construct(SchemaAttributeInterface $property)
24
    {
25 255
        $this->property = $property;
26 255
    }
27
28 255
    public function validate(): void
29
    {
30 255
        if ($this->property->isPk()) {
31 29
            if (!$this->property->isRequired()) {
32 3
                throw new InvalidSchemaAttributeException('Primary Key must be required.');
33
            }
34
35 26
            if ($this->property->isFk()) {
36 2
                throw new InvalidSchemaAttributeException('Primary Key cannot be Foreing Key too.');
37
            }
38
39 24
            if ($this->isInt($this->property) && !$this->property->isAi()) {
40 1
                throw new InvalidSchemaAttributeException('Primary Key numeric not autoincrement.');
41
            }
42
43 23
            if ($this->property->isAi() && $this->hasSizing($this->property)) {
44 1
                throw new InvalidSchemaAttributeException('Primary Key autoincrement cannot has sizing.');
45
            }
46
        }
47 248
    }
48
}
49