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

__invoke()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 24
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 6
eloc 19
c 2
b 1
f 1
nc 4
nop 2
dl 0
loc 24
rs 9.0111
1
<?php
2
3
declare(strict_types=1);
4
5
6
namespace AlgoWeb\ODataMetadata\Edm\Validation\ValidationRules\IDirectValueAnnotation;
7
8
use AlgoWeb\ODataMetadata\CsdlConstants;
9
use AlgoWeb\ODataMetadata\Edm\Validation\EdmErrorCode;
10
use AlgoWeb\ODataMetadata\Edm\Validation\ValidationContext;
11
use AlgoWeb\ODataMetadata\EdmConstants;
12
use AlgoWeb\ODataMetadata\EdmUtil;
13
use AlgoWeb\ODataMetadata\Interfaces\Annotations\IDirectValueAnnotation;
14
use AlgoWeb\ODataMetadata\Interfaces\IEdmElement;
15
use AlgoWeb\ODataMetadata\Interfaces\Values\IStringValue;
16
use AlgoWeb\ODataMetadata\StringConst;
17
18
/**
19
 * Validates that an immediate value annotation has a name and a namespace.
20
 *
21
 * @package AlgoWeb\ODataMetadata\Edm\Validation\ValidationRules\IDirectValueAnnotation
22
 */
23
class ImmediateValueAnnotationElementAnnotationIsValid extends DirectValueAnnotationRule
24
{
25
    public function __invoke(ValidationContext $context, ?IEdmElement $annotation)
26
    {
27
        assert($annotation instanceof IDirectValueAnnotation);
28
        $stringValue = $annotation->getValue();
29
        if ($stringValue != null && $stringValue instanceof IStringValue) {
30
            if (boolval(
31
                $context
32
                    ->getModel()
33
                    ->getDirectValueAnnotationsManager()
34
                    ->getAnnotationValue(
35
                        $stringValue,
36
                        EdmConstants::InternalUri,
37
                        CsdlConstants::IsSerializedAsElementAnnotation
38
                    ) ?? false
39
            )
40
            ) {
41
                if (EdmUtil::IsNullOrWhiteSpaceInternal($annotation->getNamespaceUri()) ||
42
                    EdmUtil::IsNullOrWhiteSpaceInternal($annotation->getName())
43
                ) {
44
                    EdmUtil::checkArgumentNull($annotation->Location(), 'annotation->Location');
45
                    $context->AddError(
46
                        $annotation->Location(),
47
                        EdmErrorCode::InvalidElementAnnotation(),
48
                        StringConst::EdmModel_Validator_Semantic_InvalidElementAnnotationMismatchedTerm()
49
                    );
50
                }
51
            }
52
        }
53
    }
54
}
55