Completed
Push — master ( 108e84...f077fb )
by Joschi
08:25
created

AbstractRelation   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 197
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 97.96%

Importance

Changes 9
Bugs 0 Features 3
Metric Value
c 9
b 0
f 3
dl 0
loc 197
ccs 48
cts 49
cp 0.9796
rs 10
wmc 20
lcom 2
cbo 1

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 3
A getUrl() 0 4 1
A setUrl() 0 6 1
A getLabel() 0 4 1
A setLabel() 0 6 1
A getEmail() 0 4 1
A setEmail() 0 6 1
A getCoupling() 0 4 1
A setCoupling() 0 14 3
A getRelationType() 0 4 1
A __toString() 0 5 2
A getSignature() 0 9 4
1
<?php
2
3
/**
4
 * apparat-object
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Object
8
 * @subpackage  Apparat\Object\<Layer>
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license     http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Apparat\Object\Domain\Model\Relation;
38
39
use Apparat\Object\Domain\Model\Uri\ApparatUrl;
40
use Apparat\Object\Domain\Model\Uri\Url;
41
42
/**
43
 * Abstract base relation
44
 *
45
 * @package Apparat\Object
46
 * @subpackage Apparat\Object\Domain
47
 */
48
abstract class AbstractRelation implements RelationInterface
49
{
50
    /**
51
     * Relation type
52
     *
53
     * @var string
54
     */
55
    const TYPE = 'abstract';
56
    /**
57
     * Relation URL
58
     *
59
     * @var Url|ApparatUrl
60
     */
61
    protected $url = null;
62
    /**
63
     * Relation label
64
     *
65
     * @var string
66
     */
67
    protected $label = null;
68
    /**
69
     * Relation email
70
     *
71
     * @var string
72
     */
73
    protected $email = null;
74
    /**
75
     * Coupling
76
     *
77
     * @var int
78
     */
79
    protected $coupling = self::LOOSE_COUPLING;
80
81
    /**
82
     * @param Url $url Relation URL
83
     * @param string $label Relation label
84
     * @param string $email Relation email
85
     * @param int $coupling Coupling
86
     * @throws OutOfBoundsException If the object coupling is invalid
87
     */
88 40
    public function __construct(
89
        Url $url = null,
90
        $label = null,
91
        $email = null,
92
        $coupling = null
93
    ) {
94
        // If the coupling type is invalid
95 40
        if (($coupling !== self::LOOSE_COUPLING) && ($coupling !== self::TIGHT_COUPLING)) {
96 1
            throw new OutOfBoundsException(
97 1
                sprintf('Invalid object coupling "%s"', $coupling),
98 1
                OutOfBoundsException::INVALID_OBJECT_COUPLING
99
            );
100
        }
101
102 39
        $this->url = $url;
103 39
        $this->label = $label;
104 39
        $this->email = $email;
105 39
        $this->coupling = $coupling;
106 39
    }
107
108
    /**
109
     * Return the URL
110
     *
111
     * @return Url|ApparatUrl URL
112
     */
113 3
    public function getUrl()
114
    {
115 3
        return $this->url;
116
    }
117
118
    /**
119
     * Set the URL
120
     *
121
     * @param Url|ApparatUrl|null $url URL
122
     * @return RelationInterface Self reference
123
     */
124 1
    public function setUrl(Url $url = null)
125
    {
126 1
        $relation = clone $this;
127 1
        $relation->url = $url;
128 1
        return $relation;
129
    }
130
131
    /**
132
     * Return the label
133
     *
134
     * @return string|null Label
135
     */
136 1
    public function getLabel()
137
    {
138 1
        return $this->label;
139
    }
140
141
    /**
142
     * Set the label
143
     *
144
     * @param string|null $label Label
145
     * @return RelationInterface Self reference
146
     */
147 1
    public function setLabel($label)
148
    {
149 1
        $relation = clone $this;
150 1
        $relation->label = $label;
151 1
        return $relation;
152
    }
153
154
    /**
155
     * Return the email address
156
     *
157
     * @return string|null Email address
158
     */
159 2
    public function getEmail()
160
    {
161 2
        return $this->email;
162
    }
163
164
    /**
165
     * Set the email address
166
     *
167
     * @param string|null $email Email address
168
     * @return RelationInterface Self reference
169
     */
170 1
    public function setEmail($email)
171
    {
172 1
        $relation = clone $this;
173 1
        $relation->email = $email;
174 1
        return $relation;
175
    }
176
177
    /**
178
     * Return the coupling
179
     *
180
     * @return int Coupling
181
     */
182 1
    public function getCoupling()
183
    {
184 1
        return $this->coupling;
185
    }
186
187
    /**
188
     * Set the coupling
189
     *
190
     * @param int $coupling Coupling
191
     * @return RelationInterface Self reference
192
     * @throws OutOfBoundsException If the object coupling is invalid
193
     */
194 1
    public function setCoupling($coupling)
195
    {
196
        // If the coupling type is invalid
197 1
        if (($coupling !== self::LOOSE_COUPLING) && ($coupling !== self::TIGHT_COUPLING)) {
198 1
            throw new OutOfBoundsException(
199 1
                sprintf('Invalid object coupling "%s"', $coupling),
200 1
                OutOfBoundsException::INVALID_OBJECT_COUPLING
201
            );
202
        }
203
204 1
        $relation = clone $this;
205 1
        $relation->coupling = $coupling;
206 1
        return $relation;
207
    }
208
209
    /**
210
     * Return the unique relation signature
211
     *
212
     * @return string Relation signature
213
     */
214 38
    public function getSignature()
215
    {
216 38
        return md5(
217 38
            $this->getRelationType().
218
            (empty($this->url)
219 11
                ? (empty($this->email) ? (empty($this->label) ? '-' : $this->label) : $this->email)
220 38
                : $this->url)
221
        );
222
    }
223
224
    /**
225
     * Return the relation type
226
     *
227
     * @return string Relation type
228
     */
229 38
    public function getRelationType()
230
    {
231 38
        return static::TYPE;
232
    }
233
234
    /**
235
     * Serialize the property
236
     *
237
     * @return mixed Property serialization
238
     */
239 4
    public function __toString()
240
    {
241 4
        $relationParts = [$this->url, empty($this->email) ? null : '<'.$this->email.'>', $this->label];
242 4
        return implode(' ', array_filter($relationParts));
243
    }
244
}
245