| Total Complexity | 41 |
| Total Lines | 413 |
| Duplicated Lines | 0 % |
| Changes | 18 | ||
| 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 |
||
| 22 | class Standard |
||
| 23 | extends \Aimeos\Controller\Frontend\Base |
||
| 24 | implements Iface, \Aimeos\Controller\Frontend\Common\Iface |
||
| 25 | { |
||
| 26 | private $domains = []; |
||
| 27 | private $manager; |
||
| 28 | |||
| 29 | /** @var Iface */ |
||
| 30 | private $item; |
||
| 31 | |||
| 32 | |||
| 33 | /** |
||
| 34 | * Initializes the controller |
||
| 35 | * |
||
| 36 | * @param \Aimeos\MShop\Context\Item\Iface $context Common MShop context object |
||
| 37 | */ |
||
| 38 | public function __construct( \Aimeos\MShop\Context\Item\Iface $context ) |
||
| 39 | { |
||
| 40 | parent::__construct( $context ); |
||
| 41 | |||
| 42 | $this->manager = \Aimeos\MShop::create( $context, 'customer' ); |
||
| 43 | |||
| 44 | if( ( $userid = $context->getUserId() ) === null ) |
||
| 45 | { |
||
| 46 | /** controller/frontend/customer/groupids |
||
| 47 | * List of groups new customers should be assigned to |
||
| 48 | * |
||
| 49 | * Newly created customers will be assigned automatically to the groups |
||
| 50 | * given by their IDs. This is especially useful if those groups limit |
||
| 51 | * functionality for those users. |
||
| 52 | * |
||
| 53 | * @param array List of group IDs |
||
| 54 | * @since 2017.07 |
||
| 55 | * @category User |
||
| 56 | * @category Developer |
||
| 57 | */ |
||
| 58 | $groupIds = (array) $context->getConfig()->get( 'controller/frontend/customer/groupids', [] ); |
||
| 59 | $this->item = $this->manager->create()->setGroups( $groupIds ); |
||
| 60 | } |
||
| 61 | else |
||
| 62 | { |
||
| 63 | $this->item = $this->manager->get( $userid, [], true ); |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | |||
| 68 | /** |
||
| 69 | * Clones objects in controller and resets values |
||
| 70 | */ |
||
| 71 | public function __clone() |
||
| 72 | { |
||
| 73 | $this->item = clone $this->item; |
||
| 74 | } |
||
| 75 | |||
| 76 | |||
| 77 | /** |
||
| 78 | * Creates a new customer item object pre-filled with the given values but not yet stored |
||
| 79 | * |
||
| 80 | * @param array $values Values added to the customer item (new or existing) like "customer.code" |
||
| 81 | * @return Iface Customer controller for fluent interface |
||
| 82 | * @since 2019.04 |
||
| 83 | */ |
||
| 84 | public function add( array $values ) : Iface |
||
| 85 | { |
||
| 86 | foreach( $values as $key => $value ) |
||
| 87 | { |
||
| 88 | if( is_scalar( $value ) ) { |
||
| 89 | $values[$key] = strip_tags( (string) $value ); // prevent XSS |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | $addrItem = $this->item->getPaymentAddress(); |
||
| 94 | |||
| 95 | if( $code = $values['customer.code'] ?? null ) { |
||
| 96 | $this->item->setCode( $code ); |
||
| 97 | } |
||
| 98 | |||
| 99 | if ( $oldPassword = $values['customer.oldpassword'] ?? null) { |
||
| 100 | $confirmed = $values['customer.newpassword'] === $values['customer.confirmnewpassword']; |
||
| 101 | $isNew = $values['customer.newpassword'] !== $values['customer.oldpassword']; |
||
| 102 | if ($this->item->verifyPassword($oldPassword) && $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 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 ) : 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 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 ) : 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 Iface Customer controller for fluent interface |
||
| 172 | * @since 2019.04 |
||
| 173 | */ |
||
| 174 | public function addPropertyItem( \Aimeos\MShop\Common\Item\Property\Iface $item ) : 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 Iface Customer controller for fluent interface |
||
| 241 | */ |
||
| 242 | public function deleteAddressItem( \Aimeos\MShop\Common\Item\Address\Iface $item ) : 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 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 ) : 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 Iface Customer controller for fluent interface |
||
| 313 | * @since 2019.04 |
||
| 314 | */ |
||
| 315 | public function store() : 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 Iface Customer controller for fluent interface |
||
| 344 | * @since 2019.04 |
||
| 345 | */ |
||
| 346 | public function uses( array $domains ) : Iface |
||
| 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 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths