Passed
Pull Request — master (#44)
by Alex
02:51
created

ImmediateValueAnnotationElementAnnotationIsValid   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
eloc 20
c 2
b 1
f 1
dl 0
loc 26
rs 10
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 24 6
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