Completed
Push — master ( b9f89d...74ba61 )
by Alex
29s queued 14s
created

__invoke()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 27
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 6
eloc 20
c 2
b 1
f 1
nc 5
nop 2
dl 0
loc 27
rs 8.9777
1
<?php
2
3
declare(strict_types=1);
4
5
6
namespace AlgoWeb\ODataMetadata\Edm\Validation\ValidationRules\INavigationProperty;
7
8
use AlgoWeb\ODataMetadata\Edm\Validation\EdmErrorCode;
9
use AlgoWeb\ODataMetadata\Edm\Validation\Internal\ValidationHelper;
10
use AlgoWeb\ODataMetadata\Edm\Validation\ValidationContext;
11
use AlgoWeb\ODataMetadata\EdmUtil;
12
use AlgoWeb\ODataMetadata\Interfaces\IEdmElement;
13
use AlgoWeb\ODataMetadata\Interfaces\INavigationProperty;
14
use AlgoWeb\ODataMetadata\StringConst;
15
16
/**
17
 *  Validates that if the dependent properties are equivalent to the key of the dependent end, the multiplicity of
18
 *  the dependent end cannot be 1
19
 *  Validates multiplicity of the dependent end according to the following rules:
20
 *  0..1, 1 - if dependent properties represent the dependent end key.
21
 *        * - if dependent properties don't represent the dependent end key.
22
 *
23
 * @package AlgoWeb\ODataMetadata\Edm\Validation\ValidationRules\INavigationProperty
24
 */
25
class NavigationPropertyDependentEndMultiplicity extends NavigationPropertyRule
26
{
27
    public function __invoke(ValidationContext $context, ?IEdmElement $navigationProperty)
28
    {
29
        assert($navigationProperty instanceof INavigationProperty);
30
        EdmUtil::checkArgumentNull($navigationProperty->Location(), 'navigationProperty->Location');
31
        $dependentProperties = $navigationProperty->getDependentProperties();
32
        if ($dependentProperties != null) {
33
            if (ValidationHelper::PropertySetsAreEquivalent(
34
                $navigationProperty->DeclaringEntityType()->Key(),
35
                $dependentProperties
36
            )) {
37
                if (!$navigationProperty->Multiplicity()->isZeroOrOne() &&
38
                    !$navigationProperty->Multiplicity()->isOne()
39
                ) {
40
                    $context->AddError(
41
                        $navigationProperty->Location(),
42
                        EdmErrorCode::InvalidMultiplicityOfDependentEnd(),
43
                        StringConst::EdmModel_Validator_Semantic_InvalidMultiplicityOfDependentEndMustBeZeroOneOrOne(
44
                            $navigationProperty->getName()
45
                        )
46
                    );
47
                }
48
            } elseif ($navigationProperty->Multiplicity()->isMany()) {
49
                $context->AddError(
50
                    $navigationProperty->Location(),
51
                    EdmErrorCode::InvalidMultiplicityOfDependentEnd(),
52
                    StringConst::EdmModel_Validator_Semantic_InvalidMultiplicityOfDependentEndMustBeMany(
53
                        $navigationProperty->getName()
54
                    )
55
                );
56
            }
57
        }
58
    }
59
}
60