Completed
Push — master ( 2ca52b...7d8595 )
by Joschi
02:51
created

AbstractRelation::getLabel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
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\Path\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 14
    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 14
        if (($coupling !== self::LOOSE_COUPLING) && ($coupling !== self::TIGHT_COUPLING)) {
95
            throw new OutOfBoundsException(
96
                sprintf('Invalid object coupling "%s"', $coupling),
97
                OutOfBoundsException::INVALID_OBJECT_COUPLING
98
            );
99
        }
100
101 14
        $this->url = $url;
102 14
        $this->label = $label;
103 14
        $this->email = $email;
104 14
        $this->coupling = $coupling;
105 14
    }
106
107
    /**
108
     * Return the relation type
109
     *
110
     * @return string Relation type
111
     */
112 14
    public function getType()
113
    {
114 14
        return static::TYPE;
115
    }
116
117
    /**
118
     * Return the URL
119
     *
120
     * @return Url URL
121
     */
122
    public function getUrl()
123
    {
124
        return $this->url;
125
    }
126
127
    /**
128
     * Set the URL
129
     *
130
     * @param Url|null $url URL
131
     * @return RelationInterface Self reference
132
     */
133
    public function setUrl(Url $url = null)
134
    {
135
        $relation = clone $this;
136
        $relation->url = $url;
137
        return $relation;
138
    }
139
140
    /**
141
     * Return the label
142
     *
143
     * @return string|null Label
144
     */
145
    public function getLabel()
146
    {
147
        return $this->label;
148
    }
149
150
    /**
151
     * Set the label
152
     *
153
     * @param string|null $label Label
154
     * @return RelationInterface Self reference
155
     */
156
    public function setLabel($label)
157
    {
158
        $relation = clone $this;
159
        $relation->label = $label;
160
        return $relation;
161
    }
162
163
    /**
164
     * Return the email address
165
     *
166
     * @return string|null Email address
167
     */
168
    public function getEmail()
169
    {
170
        return $this->email;
171
    }
172
173
    /**
174
     * Set the email address
175
     *
176
     * @param string|null $email Email address
177
     * @return RelationInterface Self reference
178
     */
179
    public function setEmail($email)
180
    {
181
        $relation = clone $this;
182
        $relation->email = $email;
183
        return $relation;
184
    }
185
186
    /**
187
     * Return the coupling
188
     *
189
     * @return int Coupling
190
     */
191
    public function getCoupling()
192
    {
193
        return $this->coupling;
194
    }
195
196
    /**
197
     * Set the coupling
198
     *
199
     * @param int $coupling Coupling
200
     * @return RelationInterface Self reference
201
     * @throws OutOfBoundsException If the object coupling is invalid
202
     */
203
    public function setCoupling($coupling)
204
    {
205
        // If the coupling type is invalid
206
        if (($coupling !== self::LOOSE_COUPLING) && ($coupling !== self::TIGHT_COUPLING)) {
207
            throw new OutOfBoundsException(
208
                sprintf('Invalid object coupling "%s"', $coupling),
209
                OutOfBoundsException::INVALID_OBJECT_COUPLING
210
            );
211
        }
212
213
        $relation = clone $this;
214
        $relation->coupling = $coupling;
215
        return $relation;
216
    }
217
218
    /**
219
     * Return the unique relation signature
220
     *
221
     * @return string Relation signature
222
     */
223 14
    public function getSignature()
224
    {
225 14
        return md5(
226
            empty($this->url) ?
227
                (empty($this->email) ?
228
                    (empty($this->label) ?
229
                        '-' : $this->label)
230
                    : $this->email)
231 14
                : $this->url);
232
    }
233
}
234