validateResourceIdentifierObject()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
nc 4
nop 2
dl 0
loc 13
ccs 8
cts 8
cp 1
crap 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VGirol\JsonApiStructure\Concern;
6
7
use VGirol\JsonApiConstant\Members;
8
use VGirol\JsonApiStructure\Messages;
9
10
/**
11
 * Assertions relating to the resource linkage
12
 */
13
trait ValidateResourceLinkage
14
{
15
    /**
16
     * Asserts that a json fragment is a valid resource linkage object.
17
     *
18
     * It will do the following checks :
19
     * 1) asserts that the provided resource linkage is either an object, an array of objects or the `null` value.
20
     * 2) asserts that the resource linkage or the collection of resource linkage is valid
21
     * (@see validateResourceIdentifierObject).
22
     *
23
     * @param array|null $json
24
     * @param boolean    $strict If true, unsafe characters are not allowed when checking members name.
25
     *
26
     * @return void
27
     * @throws \VGirol\JsonApiStructure\Exception\ValidationException
28
     */
29 66
    public function validateResourceLinkage($json, bool $strict): void
30
    {
31 66
        if ($json === null) {
32 6
            return;
33
        }
34
35 60
        if (!\is_array($json)) {
0 ignored issues
show
introduced by
The condition is_array($json) is always true.
Loading history...
36 3
            $this->throw(Messages::RESOURCE_LINKAGE_BAD_TYPE, 403);
37
        }
38
39 57
        if (\count($json) == 0) {
40 9
            return;
41
        }
42
43 48
        if (!$this->isArrayOfObjects($json)) {
0 ignored issues
show
Bug introduced by
It seems like isArrayOfObjects() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
        if (!$this->/** @scrutinizer ignore-call */ isArrayOfObjects($json)) {
Loading history...
44 36
            $json = [$json];
45
        }
46 48
        foreach ($json as $resource) {
47 48
            $this->validateResourceIdentifierObject($resource, $strict);
48
        }
49 36
    }
50
51
    /**
52
     * Asserts that a json fragment is a valid resource identifier object.
53
     *
54
     * It will do the following checks :
55
     * 1) asserts that the resource has "id" member(@see validateResourceIdMember).
56
     * 2) asserts that the resource has "type" (@see validateResourceTypeMember) members.
57
     * 3) asserts that it contains only the following allowed members : "id", "type" and "meta"
58
     * (@see containsOnlyAllowedMembers).
59
     *
60
     * Optionaly, if presents, it will checks :
61
     * 4) asserts that the meta object is valid (@see validateMetaObject).
62
     *
63
     * @param array   $resource
64
     * @param boolean $strict   If true, unsafe characters are not allowed when checking members name.
65
     *
66
     * @return void
67
     * @throws \VGirol\JsonApiStructure\Exception\ValidationException
68
     */
69 96
    public function validateResourceIdentifierObject($resource, bool $strict): void
70
    {
71 96
        if (!\is_array($resource)) {
0 ignored issues
show
introduced by
The condition is_array($resource) is always true.
Loading history...
72 3
            $this->throw(Messages::RESOURCE_IDENTIFIER_MUST_BE_ARRAY, 403);
73
        }
74
75 93
        $this->validateResourceIdMember($resource);
0 ignored issues
show
Bug introduced by
The method validateResourceIdMember() does not exist on VGirol\JsonApiStructure\...ValidateResourceLinkage. Did you maybe mean validateResourceLinkage()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

75
        $this->/** @scrutinizer ignore-call */ 
76
               validateResourceIdMember($resource);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
76 75
        $this->validateResourceTypeMember($resource, $strict);
0 ignored issues
show
Bug introduced by
The method validateResourceTypeMember() does not exist on VGirol\JsonApiStructure\...ValidateResourceLinkage. Did you maybe mean validateResourceLinkage()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

76
        $this->/** @scrutinizer ignore-call */ 
77
               validateResourceTypeMember($resource, $strict);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
77
78 69
        $this->containsOnlyAllowedMembers($this->getRule('ResourceIdentifierObject.Allowed'), $resource);
0 ignored issues
show
Bug introduced by
It seems like containsOnlyAllowedMembers() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
        $this->/** @scrutinizer ignore-call */ 
79
               containsOnlyAllowedMembers($this->getRule('ResourceIdentifierObject.Allowed'), $resource);
Loading history...
Bug introduced by
It seems like getRule() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
        $this->containsOnlyAllowedMembers($this->/** @scrutinizer ignore-call */ getRule('ResourceIdentifierObject.Allowed'), $resource);
Loading history...
79
80 57
        if (\array_key_exists(Members::META, $resource)) {
81 9
            $this->validateMetaObject($resource[Members::META], $strict);
0 ignored issues
show
Bug introduced by
It seems like validateMetaObject() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

81
            $this->/** @scrutinizer ignore-call */ 
82
                   validateMetaObject($resource[Members::META], $strict);
Loading history...
82
        }
83 51
    }
84
}
85