Total Complexity | 41 |
Total Lines | 415 |
Duplicated Lines | 0 % |
Changes | 20 | ||
Bugs | 0 | Features | 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 |
||
20 | class Standard |
||
21 | extends \Aimeos\Controller\Frontend\Base |
||
22 | implements \Aimeos\Controller\Frontend\Customer\Iface, \Aimeos\Controller\Frontend\Common\Iface |
||
23 | { |
||
24 | private $domains = []; |
||
25 | private $manager; |
||
26 | |||
27 | /** @var Iface */ |
||
28 | private $item; |
||
29 | |||
30 | |||
31 | /** |
||
32 | * Initializes the controller |
||
33 | * |
||
34 | * @param \Aimeos\MShop\Context\Item\Iface $context Common MShop context object |
||
35 | */ |
||
36 | public function __construct( \Aimeos\MShop\Context\Item\Iface $context ) |
||
37 | { |
||
38 | parent::__construct( $context ); |
||
39 | |||
40 | $this->manager = \Aimeos\MShop::create( $context, 'customer' ); |
||
41 | |||
42 | if( ( $userid = $context->getUserId() ) === null ) |
||
43 | { |
||
44 | /** controller/frontend/customer/groupids |
||
45 | * List of groups new customers should be assigned to |
||
46 | * |
||
47 | * Newly created customers will be assigned automatically to the groups |
||
48 | * given by their IDs. This is especially useful if those groups limit |
||
49 | * functionality for those users. |
||
50 | * |
||
51 | * @param array List of group IDs |
||
52 | * @since 2017.07 |
||
53 | * @category User |
||
54 | * @category Developer |
||
55 | */ |
||
56 | $groupIds = (array) $context->getConfig()->get( 'controller/frontend/customer/groupids', [] ); |
||
57 | $this->item = $this->manager->create()->setGroups( $groupIds ); |
||
58 | } |
||
59 | else |
||
60 | { |
||
61 | $this->item = $this->manager->get( $userid, [], true ); |
||
|
|||
62 | } |
||
63 | } |
||
64 | |||
65 | |||
66 | /** |
||
67 | * Clones objects in controller and resets values |
||
68 | */ |
||
69 | public function __clone() |
||
70 | { |
||
71 | $this->item = clone $this->item; |
||
72 | } |
||
73 | |||
74 | |||
75 | /** |
||
76 | * Creates a new customer item object pre-filled with the given values but not yet stored |
||
77 | * |
||
78 | * @param array $values Values added to the customer item (new or existing) like "customer.code" |
||
79 | * @return \Aimeos\Controller\Frontend\Customer\Iface Customer controller for fluent interface |
||
80 | * @since 2019.04 |
||
81 | */ |
||
82 | public function add( array $values ) : \Aimeos\Controller\Frontend\Customer\Iface |
||
83 | { |
||
84 | foreach( $values as $key => $value ) |
||
85 | { |
||
86 | if( is_scalar( $value ) ) { |
||
87 | $values[$key] = strip_tags( (string) $value ); // prevent XSS |
||
88 | } |
||
89 | } |
||
90 | |||
91 | $addrItem = $this->item->getPaymentAddress(); |
||
92 | |||
93 | if( $code = $values['customer.code'] ?? null ) { |
||
94 | $this->item->setCode( $code ); |
||
95 | } |
||
96 | |||
97 | if ( $oldPassword = $values['customer.oldpassword'] ?? null) { |
||
98 | $confirmed = $values['customer.newpassword'] === $values['customer.confirmnewpassword']; |
||
99 | $isNew = $values['customer.newpassword'] !== $values['customer.oldpassword']; |
||
100 | |||
101 | $passHelper = $this->getContext()->password(); |
||
102 | if ($passHelper->verify($oldPassword, $this->item->getPassword()) && $confirmed && $isNew) { |
||
103 | $this->item = $this->item->setPassword( $values['customer.newpassword'] ); |
||
104 | } |
||
105 | } |
||
106 | |||
107 | if( $password = $values['customer.password'] ?? null ) { |
||
108 | $this->item = $this->item->setPassword( $password ); |
||
109 | } |
||
110 | |||
111 | if( $this->item->getLabel() === '' ) |
||
112 | { |
||
113 | $label = $addrItem->getLastname(); |
||
114 | |||
115 | if( ( $firstName = $addrItem->getFirstname() ) !== '' ) { |
||
116 | $label = $firstName . ' ' . $label; |
||
117 | } |
||
118 | |||
119 | if( ( $company = $addrItem->getCompany() ) !== '' ) { |
||
120 | $label .= ' (' . $company . ')'; |
||
121 | } |
||
122 | |||
123 | $this->item->setLabel( $label ); |
||
124 | } |
||
125 | |||
126 | $this->item->fromArray( $values ); |
||
127 | return $this; |
||
128 | } |
||
129 | |||
130 | |||
131 | /** |
||
132 | * Adds the given address item to the customer object (not yet stored) |
||
133 | * |
||
134 | * @param \Aimeos\MShop\Common\Item\Address\Iface $item Address item to add |
||
135 | * @param int|null $idx Key in the list of address items or null to add the item at the end |
||
136 | * @return \Aimeos\Controller\Frontend\Customer\Iface Customer controller for fluent interface |
||
137 | * @since 2019.04 |
||
138 | */ |
||
139 | public function addAddressItem( \Aimeos\MShop\Common\Item\Address\Iface $item, int $idx = null ) : \Aimeos\Controller\Frontend\Customer\Iface |
||
140 | { |
||
141 | $this->item = $this->item->addAddressItem( $item, $idx ); |
||
142 | return $this; |
||
143 | } |
||
144 | |||
145 | |||
146 | /** |
||
147 | * Adds the given list item to the customer object (not yet stored) |
||
148 | * |
||
149 | * @param string $domain Domain name the referenced item belongs to |
||
150 | * @param \Aimeos\MShop\Common\Item\Lists\Iface $item List item to add |
||
151 | * @param \Aimeos\MShop\Common\Item\Iface|null $refItem Referenced item to add or null if list item contains refid value |
||
152 | * @return \Aimeos\Controller\Frontend\Customer\Iface Customer controller for fluent interface |
||
153 | * @since 2019.04 |
||
154 | */ |
||
155 | public function addListItem( string $domain, \Aimeos\MShop\Common\Item\Lists\Iface $item, |
||
156 | \Aimeos\MShop\Common\Item\Iface $refItem = null ) : \Aimeos\Controller\Frontend\Customer\Iface |
||
157 | { |
||
158 | if( $domain === 'customer/group' ) { |
||
159 | throw new Exception( sprintf( 'You are not allowed to manage groups' ) ); |
||
160 | } |
||
161 | |||
162 | $this->item = $this->item->addListItem( $domain, $item, $refItem ); |
||
163 | return $this; |
||
164 | } |
||
165 | |||
166 | |||
167 | /** |
||
168 | * Adds the given property item to the customer object (not yet stored) |
||
169 | * |
||
170 | * @param \Aimeos\MShop\Common\Item\Property\Iface $item Property item to add |
||
171 | * @return \Aimeos\Controller\Frontend\Customer\Iface Customer controller for fluent interface |
||
172 | * @since 2019.04 |
||
173 | */ |
||
174 | public function addPropertyItem( \Aimeos\MShop\Common\Item\Property\Iface $item ) : \Aimeos\Controller\Frontend\Customer\Iface |
||
175 | { |
||
176 | $this->item = $this->item->addPropertyItem( $item ); |
||
177 | return $this; |
||
178 | } |
||
179 | |||
180 | |||
181 | /** |
||
182 | * Creates a new address item object pre-filled with the given values |
||
183 | * |
||
184 | * @param array $values Associative list of key/value pairs for populating the item |
||
185 | * @return \Aimeos\MShop\Customer\Item\Address\Iface Address item |
||
186 | * @since 2019.04 |
||
187 | */ |
||
188 | public function createAddressItem( array $values = [] ) : \Aimeos\MShop\Customer\Item\Address\Iface |
||
191 | } |
||
192 | |||
193 | |||
194 | /** |
||
195 | * Creates a new list item object pre-filled with the given values |
||
196 | * |
||
197 | * @param array $values Associative list of key/value pairs for populating the item |
||
198 | * @return \Aimeos\MShop\Common\Item\Lists\Iface List item |
||
199 | * @since 2019.04 |
||
200 | */ |
||
201 | public function createListItem( array $values = [] ) : \Aimeos\MShop\Common\Item\Lists\Iface |
||
202 | { |
||
203 | return $this->manager->createListItem()->fromArray( $values ); |
||
204 | } |
||
205 | |||
206 | |||
207 | /** |
||
208 | * Creates a new property item object pre-filled with the given values |
||
209 | * |
||
210 | * @param array $values Associative list of key/value pairs for populating the item |
||
211 | * @return \Aimeos\MShop\Common\Item\Property\Iface Property item |
||
212 | * @since 2019.04 |
||
213 | */ |
||
214 | public function createPropertyItem( array $values = [] ) : \Aimeos\MShop\Common\Item\Property\Iface |
||
215 | { |
||
216 | return $this->manager->createPropertyItem()->fromArray( $values ); |
||
217 | } |
||
218 | |||
219 | |||
220 | /** |
||
221 | * Deletes a customer item that belongs to the current authenticated user |
||
222 | * |
||
223 | * @return Iface Customer controller for fluent interface |
||
224 | * @since 2019.04 |
||
225 | */ |
||
226 | public function delete() : Iface |
||
227 | { |
||
228 | if( $this->item && $this->item->getId() ) { |
||
229 | \Aimeos\MShop::create( $this->getContext(), 'customer' )->delete( $this->item->getId() ); |
||
230 | } |
||
231 | |||
232 | return $this; |
||
233 | } |
||
234 | |||
235 | |||
236 | /** |
||
237 | * Removes the given address item from the customer object (not yet stored) |
||
238 | * |
||
239 | * @param \Aimeos\MShop\Common\Item\Address\Iface $item Address item to remove |
||
240 | * @return \Aimeos\Controller\Frontend\Customer\Iface Customer controller for fluent interface |
||
241 | */ |
||
242 | public function deleteAddressItem( \Aimeos\MShop\Common\Item\Address\Iface $item ) : \Aimeos\Controller\Frontend\Customer\Iface |
||
243 | { |
||
244 | $this->item = $this->item->deleteAddressItem( $item ); |
||
245 | return $this; |
||
246 | } |
||
247 | |||
248 | |||
249 | /** |
||
250 | * Removes the given list item from the customer object (not yet stored) |
||
251 | * |
||
252 | * @param string $domain Domain name the referenced item belongs to |
||
253 | * @param \Aimeos\MShop\Common\Item\Lists\Iface $item List item to remove |
||
254 | * @param \Aimeos\MShop\Common\Item\Iface|null $refItem Referenced item to remove or null if only list item should be removed |
||
255 | * @return \Aimeos\Controller\Frontend\Customer\Iface Customer controller for fluent interface |
||
256 | */ |
||
257 | public function deleteListItem( string $domain, \Aimeos\MShop\Common\Item\Lists\Iface $listItem, |
||
258 | \Aimeos\MShop\Common\Item\Iface $refItem = null ) : \Aimeos\Controller\Frontend\Customer\Iface |
||
259 | { |
||
260 | if( $domain === 'customer/group' ) { |
||
261 | throw new Exception( sprintf( 'You are not allowed to manage groups' ) ); |
||
262 | } |
||
263 | |||
264 | $this->item = $this->item->deleteListItem( $domain, $listItem, $refItem ); |
||
265 | return $this; |
||
266 | } |
||
267 | |||
268 | |||
269 | /** |
||
270 | * Removes the given property item from the customer object (not yet stored) |
||
271 | * |
||
272 | * @param \Aimeos\MShop\Common\Item\Property\Iface $item Property item to remove |
||
273 | * @return Iface Customer controller for fluent interface |
||
274 | */ |
||
275 | public function deletePropertyItem( \Aimeos\MShop\Common\Item\Property\Iface $item ) : Iface |
||
276 | { |
||
277 | $this->item = $this->item->deletePropertyItem( $item ); |
||
278 | return $this; |
||
279 | } |
||
280 | |||
281 | |||
282 | /** |
||
283 | * Returns the customer item for the given customer code (usually e-mail address) |
||
284 | * |
||
285 | * This method doesn't check if the customer item belongs to the logged in user! |
||
286 | * |
||
287 | * @param string $code Unique customer code |
||
288 | * @return \Aimeos\MShop\Customer\Item\Iface Customer item including the referenced domains items |
||
289 | * @since 2019.04 |
||
290 | */ |
||
291 | public function find( string $code ) : \Aimeos\MShop\Customer\Item\Iface |
||
294 | } |
||
295 | |||
296 | |||
297 | /** |
||
298 | * Returns the customer item for the current authenticated user |
||
299 | * |
||
300 | * @return \Aimeos\MShop\Customer\Item\Iface Customer item including the referenced domains items |
||
301 | * @since 2019.04 |
||
302 | */ |
||
303 | public function get() : \Aimeos\MShop\Customer\Item\Iface |
||
306 | } |
||
307 | |||
308 | |||
309 | /** |
||
310 | * Adds or updates a modified customer item in the storage |
||
311 | * |
||
312 | * @return \Aimeos\Controller\Frontend\Customer\Iface Customer controller for fluent interface |
||
313 | * @since 2019.04 |
||
314 | */ |
||
315 | public function store() : \Aimeos\Controller\Frontend\Customer\Iface |
||
316 | { |
||
317 | ( $id = $this->item->getId() ) !== null ? $this->checkId( $id ) : $this->checkLimit(); |
||
318 | $context = $this->getContext(); |
||
319 | |||
320 | if( $id === null ) |
||
321 | { |
||
322 | $msg = $this->item->toArray(); |
||
323 | |||
324 | // Show only generated passwords in account creation e-mails |
||
325 | if( $this->item->getPassword() === '' ) |
||
326 | { |
||
327 | $msg['customer.password'] = substr( sha1( microtime( true ) . getmypid() . rand() ), -8 ); |
||
328 | $this->item->setPassword( $msg['customer.password'] ); |
||
329 | } |
||
330 | |||
331 | $context->getMessageQueue( 'mq-email', 'customer/email/account' )->add( json_encode( $msg ) ); |
||
332 | } |
||
333 | |||
334 | $this->item = $this->manager->save( $this->item ); |
||
335 | return $this; |
||
336 | } |
||
337 | |||
338 | |||
339 | /** |
||
340 | * Sets the domains that will be used when working with the customer item |
||
341 | * |
||
342 | * @param array $domains Domain names of the referenced items that should be fetched too |
||
343 | * @return \Aimeos\Controller\Frontend\Customer\Iface Customer controller for fluent interface |
||
344 | * @since 2019.04 |
||
345 | */ |
||
346 | public function uses( array $domains ) : \Aimeos\Controller\Frontend\Customer\Iface |
||
347 | { |
||
348 | $this->domains = $domains; |
||
349 | |||
350 | if( ( $id = $this->getContext()->getUserId() ) !== null ) { |
||
351 | $this->item = $this->manager->get( $id, $domains, true ); |
||
352 | } |
||
353 | |||
354 | return $this; |
||
355 | } |
||
356 | |||
357 | |||
358 | /** |
||
359 | * Checks if the current user is allowed to create more customer accounts |
||
360 | * |
||
361 | * @throws \Aimeos\Controller\Frontend\Customer\Exception If access isn't allowed |
||
362 | */ |
||
363 | protected function checkLimit() |
||
364 | { |
||
365 | $total = 0; |
||
366 | $context = $this->getContext(); |
||
367 | $config = $context->getConfig(); |
||
368 | |||
369 | /** controller/frontend/customer/limit-count |
||
370 | * Maximum number of customers within the time frame |
||
371 | * |
||
372 | * Creating new customers is limited to avoid abuse and mitigate denial of |
||
373 | * service attacks. The number of customer accountss created within the |
||
374 | * time frame configured by "controller/frontend/customer/limit-seconds" |
||
375 | * are counted before a new customer account (identified by the IP address) |
||
376 | * is created. If the number of accounts is higher than the configured value, |
||
377 | * an error message will be shown to the user instead of creating a new account. |
||
378 | * |
||
379 | * @param integer Number of customer accounts allowed within the time frame |
||
380 | * @since 2017.07 |
||
381 | * @category Developer |
||
382 | * @see controller/frontend/customer/limit-seconds |
||
383 | */ |
||
384 | $count = $config->get( 'controller/frontend/customer/limit-count', 3 ); |
||
385 | |||
386 | /** controller/frontend/customer/limit-seconds |
||
387 | * Customer account limitation time frame in seconds |
||
388 | * |
||
389 | * Creating new customer accounts is limited to avoid abuse and mitigate |
||
390 | * denial of service attacks. Within the configured time frame, only a |
||
391 | * limited number of customer accounts can be created. All accounts from |
||
392 | * the same source (identified by the IP address) within the last X |
||
393 | * seconds are counted. If the total value is higher then the number |
||
394 | * configured in "controller/frontend/customer/limit-count", an error |
||
395 | * message will be shown to the user instead of creating a new account. |
||
396 | * |
||
397 | * @param integer Number of seconds to check customer accounts within |
||
398 | * @since 2017.07 |
||
399 | * @category Developer |
||
400 | * @see controller/frontend/customer/limit-count |
||
401 | */ |
||
402 | $seconds = $config->get( 'controller/frontend/customer/limit-seconds', 14400 ); |
||
403 | |||
404 | $search = $this->manager->filter()->slice( 0, 0 ); |
||
405 | $expr = [ |
||
406 | $search->compare( '==', 'customer.editor', $context->getEditor() ), |
||
407 | $search->compare( '>=', 'customer.ctime', date( 'Y-m-d H:i:s', time() - $seconds ) ), |
||
408 | ]; |
||
409 | $search->setConditions( $search->and( $expr ) ); |
||
410 | |||
411 | $this->manager->search( $search, [], $total ); |
||
412 | |||
413 | if( $total >= $count ) { |
||
414 | throw new \Aimeos\Controller\Frontend\Customer\Exception( sprintf( 'Temporary limit reached' ) ); |
||
415 | } |
||
416 | } |
||
417 | |||
418 | |||
419 | /** |
||
420 | * Checks if the current user is allowed to retrieve the customer data for the given ID |
||
421 | * |
||
422 | * @param string $id Unique customer ID |
||
423 | * @return string Unique customer ID |
||
424 | * @throws \Aimeos\Controller\Frontend\Customer\Exception If access isn't allowed |
||
425 | */ |
||
426 | protected function checkId( string $id ) : string |
||
435 | } |
||
436 | } |
||
437 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..