TransferObject   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 147
c 0
b 0
f 0
wmc 16
lcom 1
cbo 0
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A __set() 0 8 1
A __get() 0 6 1
A getClassUsedToBe() 0 4 1
A getTypeOf() 0 4 1
A getTypeUsedToBe() 0 4 1
A getTypeWantsToBe() 0 4 1
A throwUnlessCorrectSubclass() 0 12 3
A throwUnlessCorrectType() 0 12 3
A throwUnlessPropertyExists() 0 6 2
1
<?php
2
3
namespace HustleWorks\Chute\DTO;
4
5
use Exception;
6
7
/**
8
 * TransferObject
9
 *
10
 * @category TransferObject
11
 * @package  App\TranserObjects
12
 * @author   Bill Condo <[email protected]>
13
 * @license  Proprietary and confidential
14
 * @link     http://ampedbroadcast.com
15
 */
16
abstract class TransferObject
17
{
18
    /**
19
     * TransferObject constructor.
20
     *
21
     * @param array|null $data
22
     */
23
    public function __construct(array $data = [])
24
    {
25
        foreach ($data as $property => $value) {
26
            $this->{$property} = $value;
27
        }
28
    }
29
30
31
    /**
32
     * Set
33
     *
34
     * @param $property
35
     * @param $value
36
     * @throws Exception
37
     */
38
    public function __set($property, $value)
39
    {
40
        $this->throwUnlessPropertyExists($property);
41
        $this->throwUnlessCorrectType($property, $value);
42
        $this->throwUnlessCorrectSubclass($property, $value);
43
44
        $this->{$property} = $value;
45
    }
46
47
    /**
48
     * Get
49
     *
50
     * @param $property
51
     *
52
     * @return mixed
53
     * @throws Exception
54
     */
55
    public function __get($property)
56
    {
57
        $this->throwUnlessPropertyExists($property);
58
59
        return $this->{$property};
60
    }
61
62
    /**
63
     * Get Class Used To Be
64
     *
65
     * @param $property
66
     *
67
     * @return string
68
     */
69
    private function getClassUsedToBe($property)
70
    {
71
        return get_class($this->{$property});
72
    }
73
74
    /**
75
     * Get Type Of
76
     *
77
     * @param $value
78
     *
79
     * @return string
80
     */
81
    private function getTypeOf($value)
82
    {
83
        return strtolower(gettype($value));
84
    }
85
86
    /**
87
     * Get Type Used To Be
88
     *
89
     * @param $property
90
     *
91
     * @return string
92
     */
93
    private function getTypeUsedToBe($property)
94
    {
95
        return $this->getTypeOf($this->{$property});
96
    }
97
98
    /**
99
     * Get Type Wants To Be
100
     *
101
     * @param $value
102
     *
103
     * @return string
104
     */
105
    private function getTypeWantsToBe($value)
106
    {
107
        return $this->getTypeOf($value);
108
    }
109
110
    /**
111
     * Throw Unless Correct Subclass
112
     *
113
     * @param $property
114
     * @param $value
115
     * @throws Exception
116
     */
117
    private function throwUnlessCorrectSubclass($property, $value)
118
    {
119
        $typeUsedToBe = $this->getTypeUsedToBe($property);
120
121
        if ($typeUsedToBe === "object") {
122
            $classUsedToBe = $this->getClassUsedToBe($property);
123
124
            if (!is_subclass_of($value, $classUsedToBe)) {
125
                throw new Exception();
126
            }
127
        }
128
    }
129
130
    /**
131
     * Throw Unless Correct Type
132
     *
133
     * @param $property
134
     * @param $value
135
     * @throws Exception
136
     */
137
    private function throwUnlessCorrectType($property, $value)
138
    {
139
        $typeUsedToBe = $this->getTypeUsedToBe($property);
140
141
        if ($typeUsedToBe !== "null") {
142
            $typeWantsToBe = $this->getTypeWantsToBe($value);
143
144
            if ($typeUsedToBe !== $typeWantsToBe) {
145
                throw new Exception();
146
            }
147
        }
148
    }
149
150
    /**
151
     * Throw Unless Property Exists
152
     *
153
     * @param $property
154
     * @throws Exception
155
     */
156
    private function throwUnlessPropertyExists($property)
157
    {
158
        if (property_exists($this, $property) !== true) {
159
            throw new Exception();
160
        }
161
    }
162
}
163