Completed
Push — master ( 2d80b2...8a71ed )
by Joschi
03:21
created

AbstractRelation::getSignature()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.074

Importance

Changes 5
Bugs 0 Features 2
Metric Value
cc 4
eloc 6
c 5
b 0
f 2
nc 1
nop 0
dl 0
loc 9
ccs 5
cts 6
cp 0.8333
crap 4.074
rs 9.2
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\Url;
40
41
/**
42
 * Abstract base relation
43
 *
44
 * @package Apparat\Object
45
 * @subpackage Apparat\Object\Domain
46
 */
47
abstract class AbstractRelation implements RelationInterface
48
{
49
    /**
50
     * Relation type
51
     *
52
     * @var string
53
     */
54
    const TYPE = 'abstract';
55
    /**
56
     * Relation URL
57
     *
58
     * @var Url
59
     */
60
    protected $url = null;
61
    /**
62
     * Relation label
63
     *
64
     * @var string
65
     */
66
    protected $label = null;
67
    /**
68
     * Relation email
69
     *
70
     * @var string
71
     */
72
    protected $email = null;
73
    /**
74
     * Coupling
75
     *
76
     * @var int
77
     */
78
    protected $coupling = self::LOOSE_COUPLING;
79
80
    /**
81
     * @param Url $url Relation URL
82
     * @param string $label Relation label
83
     * @param string $email Relation email
84
     * @param int $coupling Coupling
85
     * @throws OutOfBoundsException If the object coupling is invalid
86
     */
87 34
    public function __construct(
88
        Url $url = null,
89
        $label = null,
90
        $email = null,
91
        $coupling = null
92
    ) {
93
        // If the coupling type is invalid
94 34
        if (($coupling !== self::LOOSE_COUPLING) && ($coupling !== self::TIGHT_COUPLING)) {
95 1
            throw new OutOfBoundsException(
96 1
                sprintf('Invalid object coupling "%s"', $coupling),
97 1
                OutOfBoundsException::INVALID_OBJECT_COUPLING
98
            );
99
        }
100
101 33
        $this->url = $url;
102 33
        $this->label = $label;
103 33
        $this->email = $email;
104 33
        $this->coupling = $coupling;
105 33
    }
106
107
    /**
108
     * Return the URL
109
     *
110
     * @return Url URL
111
     */
112 2
    public function getUrl()
113
    {
114 2
        return $this->url;
115
    }
116
117
    /**
118
     * Set the URL
119
     *
120
     * @param Url|null $url URL
121
     * @return RelationInterface Self reference
122
     */
123 1
    public function setUrl(Url $url = null)
124
    {
125 1
        $relation = clone $this;
126 1
        $relation->url = $url;
127 1
        return $relation;
128
    }
129
130
    /**
131
     * Return the label
132
     *
133
     * @return string|null Label
134
     */
135 1
    public function getLabel()
136
    {
137 1
        return $this->label;
138
    }
139
140
    /**
141
     * Set the label
142
     *
143
     * @param string|null $label Label
144
     * @return RelationInterface Self reference
145
     */
146 1
    public function setLabel($label)
147
    {
148 1
        $relation = clone $this;
149 1
        $relation->label = $label;
150 1
        return $relation;
151
    }
152
153
    /**
154
     * Return the email address
155
     *
156
     * @return string|null Email address
157
     */
158 2
    public function getEmail()
159
    {
160 2
        return $this->email;
161
    }
162
163
    /**
164
     * Set the email address
165
     *
166
     * @param string|null $email Email address
167
     * @return RelationInterface Self reference
168
     */
169 1
    public function setEmail($email)
170
    {
171 1
        $relation = clone $this;
172 1
        $relation->email = $email;
173 1
        return $relation;
174
    }
175
176
    /**
177
     * Return the coupling
178
     *
179
     * @return int Coupling
180
     */
181 1
    public function getCoupling()
182
    {
183 1
        return $this->coupling;
184
    }
185
186
    /**
187
     * Set the coupling
188
     *
189
     * @param int $coupling Coupling
190
     * @return RelationInterface Self reference
191
     * @throws OutOfBoundsException If the object coupling is invalid
192
     */
193 1
    public function setCoupling($coupling)
194
    {
195
        // If the coupling type is invalid
196 1
        if (($coupling !== self::LOOSE_COUPLING) && ($coupling !== self::TIGHT_COUPLING)) {
197 1
            throw new OutOfBoundsException(
198 1
                sprintf('Invalid object coupling "%s"', $coupling),
199 1
                OutOfBoundsException::INVALID_OBJECT_COUPLING
200
            );
201
        }
202
203 1
        $relation = clone $this;
204 1
        $relation->coupling = $coupling;
205 1
        return $relation;
206
    }
207
208
    /**
209
     * Return the unique relation signature
210
     *
211
     * @return string Relation signature
212
     */
213 32
    public function getSignature()
214
    {
215 32
        return md5(
216 32
            $this->getType().
217
            (empty($this->url)
218 10
                ? (empty($this->email) ? (empty($this->label) ? '-' : $this->label) : $this->email)
219 32
                : $this->url)
220
        );
221
    }
222
223
    /**
224
     * Return the relation type
225
     *
226
     * @return string Relation type
227
     */
228 32
    public function getType()
229
    {
230 32
        return static::TYPE;
231
    }
232
233
    /**
234
     * Serialize the property
235
     *
236
     * @return mixed Property serialization
237
     */
238 4
    public function __toString()
239
    {
240 4
        $relationParts = [$this->url, empty($this->email) ? null : '<'.$this->email.'>', $this->label];
241 4
        return implode(' ', array_filter($relationParts));
242
    }
243
}
244