Completed
Push — master ( 24066c...e82759 )
by Joschi
02:49
created

AbstractRelation   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 64
ccs 0
cts 17
cp 0
rs 10
wmc 5
lcom 1
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
B construct() 0 21 5
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
/**
40
 * Abstract base relation
41
 *
42
 * @package Apparat\Object
43
 * @subpackage Apparat\Object\Domain
44
 */
45
abstract class AbstractRelation implements RelationInterface
46
{
47
    /**
48
     * Relation types
49
     *
50
     * @var array
51
     */
52
    protected static $types = [
53
        self::CONTRIBUTES => true,
54
        self::CONTRIBUTED_BY => true,
55
        self::EMBEDS => true,
56
        self::EMBEDDED_BY => true,
57
        self::LIKES => true,
58
        self::LIKED_BY => true,
59
        self::REFERS_TO => true,
60
        self::REFERRED_BY => true,
61
        self::REPLIES_TO => true,
62
        self::REPLIED_BY => true,
63
        self::REPOSTS => true,
64
        self::REPOSTED_BY => true,
65
    ];
66
    /**
67
     * Relation type
68
     *
69
     * @var string
70
     */
71
    protected $type = null;
72
    /**
73
     * Coupling
74
     *
75
     * @var int
76
     */
77
    protected $coupling = self::LOOSE_COUPLING;
78
79
    /**
80
     * Constructor
81
     *
82
     * @param string $type Relation type
83
     * @param int $coupling Coupling
84
     * @throws OutOfBoundsException If the object type is invalid
85
     * @throws OutOfBoundsException If the object coupling is invalid
86
     */
87
    public function construct($type, $coupling = self::LOOSE_COUPLING)
88
    {
89
        // If the object type is invalid
90
        if (!strlen($type) || !array_key_exists($type, self::$types)) {
91
            throw new OutOfBoundsException(
92
                sprintf('Invalid object relation type "%s"', $type),
93
                OutOfBoundsException::INVALID_OBJECT_RELATION_TYPE
94
            );
95
        }
96
97
        // If the coupling type is invalid
98
        if (($coupling !== self::LOOSE_COUPLING) && ($coupling !== self::TIGHT_COUPLING)) {
99
            throw new OutOfBoundsException(
100
                sprintf('Invalid object coupling "%s"', $type),
101
                OutOfBoundsException::INVALID_OBJECT_COUPLING
102
            );
103
        }
104
105
        $this->type = $type;
106
        $this->coupling = $coupling;
107
    }
108
}
109