Completed
Pull Request — master (#68)
by Christopher
03:45
created

ResourceAssociationSet::keyName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 3
1
<?php
2
3
namespace POData\Providers\Metadata;
4
5
use POData\Common\Messages;
6
7
/**
8
 * Class ResourceAssociationSet.
9
 */
10
class ResourceAssociationSet
11
{
12
    /**
13
     * name of the association set.
14
     *
15
     * @var string
16
     */
17
    private $_name;
18
19
    /**
20
     * End1 of association set.
21
     *
22
     * @var ResourceAssociationSetEnd
23
     */
24
    private $_end1;
25
26
    /**
27
     * End2 of association set.
28
     *
29
     * @var ResourceAssociationSetEnd
30
     */
31
    private $_end2;
32
33
    /**
34
     * Note: This property will be populated by the library,
35
     * so IDSMP implementor should not set this.
36
     * The association type hold by this association set.
37
     *
38
     * @var ResourceAssociationType
39
     */
40
    public $resourceAssociationType;
41
42
    /**
43
     * Construct new instance of ResourceAssociationSet.
44
     *
45
     * @param string                    $name Name of the association set
46
     * @param ResourceAssociationSetEnd $end1 First end set participating
47
     *                                        in the association set
48
     * @param ResourceAssociationSetEnd $end2 Second end set participating
49
     *                                        in the association set
50
     *
51
     * @throws \InvalidArgumentException
52
     */
53
    public function __construct(
54
        $name,
55
        ResourceAssociationSetEnd $end1,
56
        ResourceAssociationSetEnd $end2
57
    ) {
58
        if (is_null($end1->getResourceProperty())
59
            && is_null($end2->getResourceProperty())
60
        ) {
61
            throw new \InvalidArgumentException(
62
                Messages::resourceAssociationSetResourcePropertyCannotBeBothNull()
63
            );
64
        }
65
66
        if ($end1->getResourceType() == $end2->getResourceType()
67
            && $end1->getResourceProperty() == $end2->getResourceProperty()
68
        ) {
69
            throw new \InvalidArgumentException(
70
                Messages::resourceAssociationSetSelfReferencingAssociationCannotBeBiDirectional()
71
            );
72
        }
73
74
        $this->_name = $name;
75
        $this->_end1 = $end1;
76
        $this->_end2 = $end2;
77
    }
78
79
    /**
80
     * Retrieve the end for the given resource set, type and property.
81
     *
82
     * @param ResourceSet      $resourceSet      Resource set for the end
83
     * @param ResourceType     $resourceType     Resource type for the end
84
     * @param ResourceProperty $resourceProperty Resource property for the end
85
     *
86
     * @return ResourceAssociationSetEnd|null Resource association set end for the
87
     *                                   given parameters
88
     */
89 View Code Duplication
    public function getResourceAssociationSetEnd(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
        ResourceSet $resourceSet,
91
        ResourceType $resourceType,
92
        ResourceProperty $resourceProperty
93
    ) {
94
        if ($this->_end1->isBelongsTo($resourceSet, $resourceType, $resourceProperty)) {
95
            return $this->_end1;
96
        }
97
98
        if ($this->_end2->isBelongsTo($resourceSet, $resourceType, $resourceProperty)) {
99
            return $this->_end2;
100
        }
101
        return null;
102
    }
103
104
    /**
105
     * Retrieve the related end for the given resource set, type and property.
106
     *
107
     * @param ResourceSet      $resourceSet      Resource set for the end
108
     * @param ResourceType     $resourceType     Resource type for the end
109
     * @param ResourceProperty $resourceProperty Resource property for the end
110
     *
111
     * @return ResourceAssociationSetEnd|null Related resource association set end
112
     *                                   for the given parameters
113
     */
114 View Code Duplication
    public function getRelatedResourceAssociationSetEnd(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
115
        ResourceSet $resourceSet,
116
        ResourceType $resourceType,
117
        ResourceProperty $resourceProperty
118
    ) {
119
        if ($this->_end1->isBelongsTo($resourceSet, $resourceType, $resourceProperty)) {
120
            return $this->_end2;
121
        }
122
123
        if ($this->_end2->isBelongsTo($resourceSet, $resourceType, $resourceProperty)) {
124
            return $this->_end1;
125
        }
126
        return null;
127
    }
128
129
    /**
130
     * Get name of the association set.
131
     *
132
     * @return string
133
     */
134
    public function getName()
135
    {
136
        return $this->_name;
137
    }
138
139
    /**
140
     * Get first end of the association set.
141
     *
142
     *  @return ResourceAssociationSetEnd
143
     */
144
    public function getEnd1()
145
    {
146
        return $this->_end1;
147
    }
148
149
    /**
150
     * Get second end of the association set.
151
     *
152
     *  @return ResourceAssociationSetEnd
153
     */
154
    public function getEnd2()
155
    {
156
        return $this->_end2;
157
    }
158
159
    /**
160
     * Whether this association set represents a two way relationship between
161
     * resource sets.
162
     *
163
     * @return bool true if relationship is bidirectional, otherwise false
164
     */
165
    public function isBidirectional()
166
    {
167
        return !is_null($this->_end1->getResourceProperty())
168
            && !is_null($this->_end2->getResourceProperty());
169
    }
170
171
172
    public static function keyName(ResourceType $sourceType, $linkName, ResourceSet $targetResourceSet)
173
    {
174
        return $sourceType->getName() . '_' . $linkName . '_' . $targetResourceSet->getResourceType()->getName();
175
    }
176
}
177