Total Complexity | 40 |
Total Lines | 278 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Model often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Model, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Picqer\Financials\Exact; |
||
10 | abstract class Model implements \JsonSerializable |
||
11 | { |
||
12 | |||
13 | /** |
||
14 | * @var Connection |
||
15 | */ |
||
16 | protected $connection; |
||
17 | |||
18 | /** |
||
19 | * @var array The model's attributes |
||
20 | */ |
||
21 | protected $attributes = []; |
||
22 | |||
23 | /** |
||
24 | * @deferred array The model's collection values |
||
25 | */ |
||
26 | protected $deferred = []; |
||
27 | |||
28 | /** |
||
29 | * @var array The model's fillable attributes |
||
30 | */ |
||
31 | protected $fillable = []; |
||
32 | |||
33 | /** |
||
34 | * @var string The URL endpoint of this model |
||
35 | */ |
||
36 | protected $url = ''; |
||
37 | |||
38 | /** |
||
39 | * @var string Name of the primary key for this model |
||
40 | */ |
||
41 | protected $primaryKey = 'ID'; |
||
42 | |||
43 | |||
44 | public function __construct(Connection $connection, array $attributes = []) |
||
45 | { |
||
46 | $this->connection = $connection; |
||
47 | $this->fill($attributes); |
||
48 | } |
||
49 | |||
50 | |||
51 | /** |
||
52 | * Get the connection instance |
||
53 | * |
||
54 | * @return Connection |
||
55 | */ |
||
56 | public function connection() |
||
59 | } |
||
60 | |||
61 | |||
62 | /** |
||
63 | * Get the model's attributes |
||
64 | * |
||
65 | * @return array |
||
66 | */ |
||
67 | public function attributes() |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Get the model's url |
||
74 | * |
||
75 | * @return string |
||
76 | */ |
||
77 | public function url() |
||
78 | { |
||
79 | return $this->url; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Get the model's primary key |
||
84 | * |
||
85 | * @return string |
||
86 | */ |
||
87 | public function primaryKey() |
||
88 | { |
||
89 | return $this->primaryKey; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Get the model's primary key value |
||
94 | * |
||
95 | * @return mixed |
||
96 | */ |
||
97 | public function primaryKeyContent() |
||
98 | { |
||
99 | return $this->__get($this->primaryKey); |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * Fill the entity from an array |
||
104 | * |
||
105 | * @param array $attributes |
||
106 | */ |
||
107 | protected function fill(array $attributes) |
||
108 | { |
||
109 | foreach ($this->fillableFromArray($attributes) as $key => $value) { |
||
110 | if ($this->isFillable($key)) { |
||
111 | $this->setAttribute($key, $value); |
||
112 | } |
||
113 | } |
||
114 | } |
||
115 | |||
116 | |||
117 | /** |
||
118 | * Get the fillable attributes of an array |
||
119 | * |
||
120 | * @param array $attributes |
||
121 | * |
||
122 | * @return array |
||
123 | */ |
||
124 | protected function fillableFromArray(array $attributes) |
||
125 | { |
||
126 | if (count($this->fillable) > 0) { |
||
127 | return array_intersect_key($attributes, array_flip($this->fillable)); |
||
128 | } |
||
129 | |||
130 | return $attributes; |
||
131 | } |
||
132 | |||
133 | |||
134 | protected function isFillable($key) |
||
135 | { |
||
136 | return in_array($key, $this->fillable); |
||
137 | } |
||
138 | |||
139 | |||
140 | protected function setAttribute($key, $value) |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Resolve deferred values |
||
147 | * |
||
148 | * @param string $key |
||
149 | * @return bool Returns true when collection is found |
||
150 | */ |
||
151 | protected function lazyLoad($key) |
||
152 | { |
||
153 | // Check previously resolved or manualy set. |
||
154 | if (isset($this->deferred[$key])) { |
||
155 | return true; |
||
156 | } |
||
157 | |||
158 | try { |
||
159 | if (array_key_exists($key, $this->attributes) && is_array($this->attributes[$key]) && array_key_exists('__deferred', $this->attributes[$key])) { |
||
160 | $class = preg_replace('/(.+?)s?$/', __NAMESPACE__ . '\\\$1', $key); // Filter plural 's' and add namespace |
||
161 | $deferred = new $class($this->connection()); |
||
162 | $uri = $this->attributes[$key]['__deferred']['uri']; |
||
163 | $deferred->connection()->nextUrl = $uri; // $uri is complete, by setting it to nextUrl Connection->formatUrl leaves it as is. |
||
164 | $result = $deferred->connection()->get($uri); |
||
165 | $this->deferred[$key] = $deferred->collectionFromResult($result); |
||
166 | |||
167 | return true; |
||
168 | } |
||
169 | } |
||
170 | catch (\Exception $e) { |
||
171 | // We tried lets leave it as is. |
||
172 | } |
||
173 | |||
174 | return false; |
||
175 | } |
||
176 | |||
177 | public function __get($key) |
||
185 | } |
||
186 | } |
||
187 | |||
188 | |||
189 | public function __set($key, $value) |
||
190 | { |
||
191 | if ($this->isFillable($key)) { |
||
192 | if (is_array($value)) { |
||
193 | $this->deferred[$key] = $value; |
||
194 | return; |
||
195 | } |
||
196 | |||
197 | $this->setAttribute($key, $value); |
||
198 | } |
||
199 | } |
||
200 | |||
201 | public function __isset($name) |
||
204 | } |
||
205 | |||
206 | public function __call($name, $arguments) |
||
207 | { |
||
208 | return $this->__get($name); |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * Checks if primaryKey holds a value |
||
213 | * |
||
214 | * @return boolean |
||
215 | */ |
||
216 | public function exists() |
||
223 | } |
||
224 | |||
225 | |||
226 | /** |
||
227 | * Return the JSON representation of the data |
||
228 | * |
||
229 | * @param int $options http://php.net/manual/en/json.constants.php |
||
230 | * |
||
231 | * @return string |
||
232 | */ |
||
233 | public function json($options = 0, $withDeferred = false) |
||
258 | } |
||
259 | |||
260 | |||
261 | /** |
||
262 | * Return serializable data |
||
263 | * |
||
264 | * @return array |
||
265 | */ |
||
266 | public function jsonSerialize() |
||
267 | { |
||
268 | return $this->attributes; |
||
269 | } |
||
270 | |||
271 | |||
272 | /** |
||
273 | * Check whether the current user has rights for an action on this endpoint |
||
274 | * https://start.exactonline.nl/docs/HlpRestAPIResources.aspx?SourceAction=10 |
||
275 | * |
||
276 | * @param string $action |
||
277 | * |
||
278 | * @return boolean |
||
279 | */ |
||
280 | public function userHasRights($action = 'GET') |
||
288 | } |
||
289 | |||
291 |