Completed
Push — master ( 602f49...8498b7 )
by Alex
16s
created

ResourceAssociationSetEnd::__construct()   D

Complexity

Conditions 9
Paths 5

Size

Total Lines 44
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 4.909
c 0
b 0
f 0
cc 9
eloc 30
nc 5
nop 4
1
<?php
2
3
namespace POData\Providers\Metadata;
4
5
use POData\Common\Messages;
6
7
/**
8
 * Class ResourceAssociationSetEnd represents association (relationship) set end.
9
 */
10
class ResourceAssociationSetEnd
11
{
12
    /**
13
     * Resource set for the association end.
14
     *
15
     * @var ResourceSet
16
     */
17
    private $resourceSet;
18
19
    /**
20
     * Resource type for the association end.
21
     *
22
     * @var ResourceEntityType
23
     */
24
    private $resourceType;
25
26
    /**
27
     * Concrete resource type for the association end.  Should only be set if $resourceType is abstract.
28
     * Is assumed to be a derived type of $resourceType if set.
29
     *
30
     * @var ResourceEntityType
31
     */
32
    private $concreteType;
33
34
    /**
35
     * Resource property for the association end.
36
     *
37
     * @var ResourceProperty
38
     */
39
    private $resourceProperty;
40
41
    /**
42
     * Construct new instance of ResourceAssociationSetEnd
43
     * Note: The $resourceSet represents collection of an entity, The
44
     * $resourceType can be this entity's type or type of any of the
45
     * base resource of this entity, on which the navigation property
46
     * represented by $resourceProperty is defined.
47
     *
48
     * @param ResourceSet           $resourceSet      Resource set for the association end
49
     * @param ResourceEntityType    $resourceType     Resource type for the association end
50
     * @param ResourceProperty|null $resourceProperty Resource property for the association end
51
     *
52
     * @throws \InvalidArgumentException
53
     */
54
    public function __construct(
55
        ResourceSet $resourceSet,
56
        ResourceEntityType $resourceType,
57
        ResourceProperty $resourceProperty = null,
58
        ResourceEntityType $concreteType = null
59
    ) {
60
        if (null !== $resourceProperty
61
            && (null === $resourceType->resolveProperty($resourceProperty->getName())
62
                || (($resourceProperty->getKind() != ResourcePropertyKind::RESOURCE_REFERENCE)
63
                    && ($resourceProperty->getKind() != ResourcePropertyKind::RESOURCESET_REFERENCE)))
64
        ) {
65
            throw new \InvalidArgumentException(
66
                Messages::resourceAssociationSetEndPropertyMustBeNavigationProperty(
67
                    $resourceProperty->getName(),
68
                    $resourceType->getFullName()
69
                )
70
            );
71
        }
72
73
        if (!$resourceSet->getResourceType()->isAssignableFrom($resourceType)
74
            && !$resourceType->isAssignableFrom($resourceSet->getResourceType())
75
        ) {
76
            throw new \InvalidArgumentException(
77
                Messages::resourceAssociationSetEndResourceTypeMustBeAssignableToResourceSet(
78
                    $resourceType->getFullName(),
79
                    $resourceSet->getName()
80
                )
81
            );
82
        }
83
        if (null !== $concreteType) {
84
            if ($concreteType->isAbstract()) {
85
                $msg = 'Concrete type must not be abstract if explicitly supplied';
86
                throw new \InvalidArgumentException($msg);
87
            }
88
            $concType = $concreteType;
89
        } else {
90
            $concType = $resourceType;
91
        }
92
93
        $this->resourceSet = $resourceSet;
94
        $this->resourceType = $resourceType;
95
        $this->resourceProperty = $resourceProperty;
96
        $this->concreteType = $concType;
97
    }
98
99
    /**
100
     * To check this relationship belongs to a specific resource set, type
101
     * and property.
102
     *
103
     * @param ResourceSet      $resourceSet      Resource set for the association
104
     *                                           end
105
     * @param ResourceType     $resourceType     Resource type for the association
106
     *                                           end
107
     * @param ResourceProperty $resourceProperty Resource property for the
108
     *                                           association end
109
     *
110
     * @return bool
111
     */
112
    public function isBelongsTo(
113
        ResourceSet $resourceSet,
114
        ResourceType $resourceType,
115
        ResourceProperty $resourceProperty
116
    ) {
117
        return strcmp($resourceSet->getName(), $this->resourceSet->getName()) == 0
118
            && $this->resourceType->isAssignableFrom($resourceType)
119
            && ((null === $resourceProperty && null === $this->resourceProperty)
120
                || (null !== $resourceProperty && null !== $this->resourceProperty
121
                    && (strcmp($resourceProperty->getName(), $this->resourceProperty->getName()) == 0)));
122
    }
123
124
    /**
125
     * Gets reference to resource set.
126
     *
127
     * @return ResourceSet
128
     */
129
    public function getResourceSet()
130
    {
131
        return $this->resourceSet;
132
    }
133
134
    /**
135
     * Gets reference to resource type.
136
     *
137
     * @return ResourceEntityType
138
     */
139
    public function getResourceType()
140
    {
141
        return $this->resourceType;
142
    }
143
144
    /**
145
     * Gets reference to concrete type.
146
     *
147
     * @return ResourceEntityType
148
     */
149
    public function getConcreteType()
150
    {
151
        return $this->concreteType;
152
    }
153
154
    /**
155
     * Gets reference to resource property.
156
     *
157
     * @return ResourceProperty
158
     */
159
    public function getResourceProperty()
160
    {
161
        return $this->resourceProperty;
162
    }
163
}
164