Total Complexity | 45 |
Total Lines | 350 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Standard 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 Standard, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class Standard |
||
22 | extends \Aimeos\MShop\Common\Item\Base |
||
23 | implements \Aimeos\MShop\Common\Item\Lists\Iface |
||
24 | { |
||
25 | use \Aimeos\MShop\Common\Item\Config\Traits; |
||
26 | |||
27 | |||
28 | private string $date; |
||
29 | private string $prefix; |
||
30 | private ?\Aimeos\MShop\Common\Item\Iface $refItem = null; |
||
31 | |||
32 | |||
33 | /** |
||
34 | * Initializes the list item object. |
||
35 | * |
||
36 | * @param string $prefix Property prefix when converting to array |
||
37 | * @param array $values Associative list of key/value pairs of the list item |
||
38 | */ |
||
39 | public function __construct( string $prefix, array $values = [] ) |
||
40 | { |
||
41 | parent::__construct( $prefix, $values, str_replace( '.', '/', rtrim( $prefix, '.' ) ) ); |
||
42 | |||
43 | $this->date = isset( $values['.date'] ) ? $values['.date'] : date( 'Y-m-d H:i:s' ); |
||
44 | $this->prefix = (string) $prefix; |
||
45 | } |
||
46 | |||
47 | |||
48 | /** |
||
49 | * Returns the parent ID of the common list item, |
||
50 | * like the unique ID of a product or a tree node. |
||
51 | * |
||
52 | * @return string|null Parent ID of the common list item |
||
53 | */ |
||
54 | public function getParentId() : ?string |
||
55 | { |
||
56 | return $this->get( $this->prefix . 'parentid' ); |
||
57 | } |
||
58 | |||
59 | |||
60 | /** |
||
61 | * Sets the parent ID of the common list item, |
||
62 | * like the unique ID of a product or a tree node. |
||
63 | * |
||
64 | * @param string|null $parentid New parent ID of the common list item |
||
65 | * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls |
||
66 | */ |
||
67 | public function setParentId( ?string $parentid ) : \Aimeos\MShop\Common\Item\Iface |
||
68 | { |
||
69 | return $this->set( $this->prefix . 'parentid', $parentid ); |
||
70 | } |
||
71 | |||
72 | |||
73 | /** |
||
74 | * Returns the unique key of the list item |
||
75 | * |
||
76 | * @return string Unique key consisting of domain/type/refid |
||
77 | */ |
||
78 | public function getKey() : string |
||
79 | { |
||
80 | return $this->getDomain() . '|' . $this->getType() . '|' . $this->getRefId(); |
||
81 | } |
||
82 | |||
83 | |||
84 | /** |
||
85 | * Returns the domain of the common list item, e.g. text or media. |
||
86 | * |
||
87 | * @return string Domain of the common list item |
||
88 | */ |
||
89 | public function getDomain() : string |
||
90 | { |
||
91 | return $this->get( $this->prefix . 'domain', '' ); |
||
92 | } |
||
93 | |||
94 | |||
95 | /** |
||
96 | * Sets the new domain of the common list item, e.g. text od media. |
||
97 | * |
||
98 | * @param string $domain New domain of the common list item |
||
99 | * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls |
||
100 | */ |
||
101 | public function setDomain( string $domain ) : \Aimeos\MShop\Common\Item\Iface |
||
102 | { |
||
103 | return $this->set( $this->prefix . 'domain', $domain ); |
||
104 | } |
||
105 | |||
106 | |||
107 | /** |
||
108 | * Returns the reference id of the common list item, like the unique id |
||
109 | * of a text item or a media item. |
||
110 | * |
||
111 | * @return string Reference id of the common list item |
||
112 | */ |
||
113 | public function getRefId() : string |
||
114 | { |
||
115 | return $this->get( $this->prefix . 'refid', '' ); |
||
116 | } |
||
117 | |||
118 | |||
119 | /** |
||
120 | * Sets the new reference id of the common list item, like the unique id |
||
121 | * of a text item or a media item. |
||
122 | * |
||
123 | * @param string $refid New reference id of the common list item |
||
124 | * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls |
||
125 | */ |
||
126 | public function setRefId( string $refid ) : \Aimeos\MShop\Common\Item\Lists\Iface |
||
127 | { |
||
128 | return $this->set( $this->prefix . 'refid', $refid ); |
||
129 | } |
||
130 | |||
131 | |||
132 | /** |
||
133 | * Returns the start date of the common list item (YYYY-MM-DD hh:mm:ss). |
||
134 | * |
||
135 | * @return string|null Start date of the common list item (YYYY-MM-DD hh:mm:ss) |
||
136 | */ |
||
137 | public function getDateStart() : ?string |
||
138 | { |
||
139 | $value = $this->get( $this->prefix . 'datestart' ); |
||
140 | return $value ? substr( $value, 0, 19 ) : null; |
||
141 | } |
||
142 | |||
143 | |||
144 | /** |
||
145 | * Sets the new start date of the common list item (YYYY-MM-DD hh:mm:ss). |
||
146 | * |
||
147 | * @param string|null $date New start date of the common list item (YYYY-MM-DD hh:mm:ss) |
||
148 | * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls |
||
149 | */ |
||
150 | public function setDateStart( ?string $date ) : \Aimeos\MShop\Common\Item\Iface |
||
151 | { |
||
152 | return $this->set( $this->prefix . 'datestart', $this->checkDateFormat( $date ) ); |
||
153 | } |
||
154 | |||
155 | |||
156 | /** |
||
157 | * Returns the end date of the common list item (YYYY-MM-DD hh:mm:ss). |
||
158 | * |
||
159 | * @return string|null End date of the common list item (YYYY-MM-DD hh:mm:ss) |
||
160 | */ |
||
161 | public function getDateEnd() : ?string |
||
162 | { |
||
163 | $value = $this->get( $this->prefix . 'dateend' ); |
||
164 | return $value ? substr( $value, 0, 19 ) : null; |
||
165 | } |
||
166 | |||
167 | |||
168 | /** |
||
169 | * Sets the new end date of the common list item (YYYY-MM-DD hh:mm:ss). |
||
170 | * |
||
171 | * @param string|null $date New end date of the common list item (YYYY-MM-DD hh:mm:ss) |
||
172 | * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls |
||
173 | */ |
||
174 | public function setDateEnd( ?string $date ) : \Aimeos\MShop\Common\Item\Iface |
||
175 | { |
||
176 | return $this->set( $this->prefix . 'dateend', $this->checkDateFormat( $date ) ); |
||
177 | } |
||
178 | |||
179 | |||
180 | /** |
||
181 | * Returns the type of the list item. |
||
182 | * |
||
183 | * @return string Type of the list item |
||
184 | */ |
||
185 | public function getType() : string |
||
186 | { |
||
187 | return $this->get( $this->prefix . 'type', 'default' ); |
||
188 | } |
||
189 | |||
190 | |||
191 | |||
192 | /** |
||
193 | * Sets the new type of the list item. |
||
194 | * |
||
195 | * @param string $type type of the list item |
||
196 | * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls |
||
197 | */ |
||
198 | public function setType( string $type ) : \Aimeos\MShop\Common\Item\Iface |
||
199 | { |
||
200 | return $this->set( $this->prefix . 'type', $this->checkCode( $type ) ); |
||
201 | } |
||
202 | |||
203 | |||
204 | /** |
||
205 | * Returns the position of the list item. |
||
206 | * |
||
207 | * @return int Position of the list item |
||
208 | */ |
||
209 | public function getPosition() : int |
||
212 | } |
||
213 | |||
214 | |||
215 | /** |
||
216 | * Sets the new position of the list item. |
||
217 | * |
||
218 | * @param int $pos position of the list item |
||
219 | * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls |
||
220 | */ |
||
221 | public function setPosition( int $pos ) : \Aimeos\MShop\Common\Item\Iface |
||
222 | { |
||
223 | return $this->set( $this->prefix . 'position', $pos ); |
||
224 | } |
||
225 | |||
226 | |||
227 | /** |
||
228 | * Returns the status of the list item. |
||
229 | * |
||
230 | * @return int Status of the item |
||
231 | */ |
||
232 | public function getStatus() : int |
||
233 | { |
||
234 | return $this->get( $this->prefix . 'status', 1 ); |
||
235 | } |
||
236 | |||
237 | |||
238 | /** |
||
239 | * Sets the new status of the list item. |
||
240 | * |
||
241 | * @param int $status Status of the item |
||
242 | * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls |
||
243 | */ |
||
244 | public function setStatus( int $status ) : \Aimeos\MShop\Common\Item\Iface |
||
245 | { |
||
246 | return $this->set( $this->prefix . 'status', $status ); |
||
247 | } |
||
248 | |||
249 | |||
250 | /** |
||
251 | * Returns the configuration of the list item. |
||
252 | * |
||
253 | * @return array Custom configuration values |
||
254 | */ |
||
255 | public function getConfig() : array |
||
256 | { |
||
257 | return $this->get( $this->prefix . 'config', [] ); |
||
258 | } |
||
259 | |||
260 | |||
261 | /** |
||
262 | * Sets the new configuration for the list item. |
||
263 | * |
||
264 | * @param array $config Custom configuration values |
||
265 | * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls |
||
266 | */ |
||
267 | public function setConfig( array $config ) : \Aimeos\MShop\Common\Item\Iface |
||
268 | { |
||
269 | return $this->set( $this->prefix . 'config', $config ); |
||
270 | } |
||
271 | |||
272 | |||
273 | /** |
||
274 | * Returns the referenced item if it's available. |
||
275 | * |
||
276 | * @return \Aimeos\MShop\Common\Item\Iface|null Referenced list item |
||
277 | */ |
||
278 | public function getRefItem() : ?\Aimeos\MShop\Common\Item\Iface |
||
279 | { |
||
280 | return $this->refItem; |
||
281 | } |
||
282 | |||
283 | |||
284 | /** |
||
285 | * Stores the item referenced by the list item. |
||
286 | * |
||
287 | * @param \Aimeos\MShop\Common\Item\Iface|null $refItem Item referenced by the list item or null for no reference |
||
288 | * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls |
||
289 | */ |
||
290 | public function setRefItem( ?\Aimeos\MShop\Common\Item\Iface $refItem ) : \Aimeos\MShop\Common\Item\Lists\Iface |
||
295 | } |
||
296 | |||
297 | |||
298 | /** |
||
299 | * Tests if the item is available based on status, time, language and currency |
||
300 | * |
||
301 | * @return boolean True if available, false if not |
||
302 | */ |
||
303 | public function isAvailable() : bool |
||
304 | { |
||
305 | return parent::isAvailable() && $this->getStatus() > 0 |
||
306 | && ( $this->getDateStart() === null || $this->getDateStart() < $this->date ) |
||
307 | && ( $this->getDateEnd() === null || $this->getDateEnd() > $this->date ); |
||
308 | } |
||
309 | |||
310 | |||
311 | /* |
||
312 | * Sets the item values from the given array and removes that entries from the list |
||
313 | * |
||
314 | * @param array &$list Associative list of item keys and their values |
||
315 | * @param bool True to set private properties too, false for public only |
||
316 | * @return \Aimeos\MShop\Common\Item\Lists\Iface List item for chaining method calls |
||
317 | */ |
||
318 | public function fromArray( array &$list, bool $private = false ) : \Aimeos\MShop\Common\Item\Iface |
||
342 | } |
||
343 | |||
344 | |||
345 | /** |
||
346 | * Returns the item values as array. |
||
347 | * |
||
348 | * @param bool True to return private properties, false for public only |
||
349 | * @return array Associative list of item properties and their values |
||
350 | */ |
||
351 | public function toArray( bool $private = false ) : array |
||
371 | } |
||
372 | |||
374 |