| Total Complexity | 41 |
| Total Lines | 343 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Autofill 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 Autofill, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 42 | class Autofill |
||
| 43 | extends \Aimeos\MShop\Plugin\Provider\Factory\Base |
||
| 44 | implements \Aimeos\MShop\Plugin\Provider\Iface, \Aimeos\MShop\Plugin\Provider\Factory\Iface |
||
| 45 | { |
||
| 46 | private array $beConfig = array( |
||
| 47 | 'address' => array( |
||
| 48 | 'code' => 'address', |
||
| 49 | 'internalcode' => 'address', |
||
| 50 | 'label' => 'Add customer address automatically', |
||
| 51 | 'type' => 'boolean', |
||
| 52 | 'internaltype' => 'boolean', |
||
| 53 | 'default' => '', |
||
| 54 | 'required' => false, |
||
| 55 | ), |
||
| 56 | 'delivery' => array( |
||
| 57 | 'code' => 'delivery', |
||
| 58 | 'internalcode' => 'delivery', |
||
| 59 | 'label' => 'Add delivery option automatically', |
||
| 60 | 'type' => 'boolean', |
||
| 61 | 'internaltype' => 'boolean', |
||
| 62 | 'default' => '', |
||
| 63 | 'required' => false, |
||
| 64 | ), |
||
| 65 | 'deliverycode' => array( |
||
| 66 | 'code' => 'deliverycode', |
||
| 67 | 'internalcode' => 'deliverycode', |
||
| 68 | 'label' => 'Add delivery by code', |
||
| 69 | 'type' => 'string', |
||
| 70 | 'internaltype' => 'string', |
||
| 71 | 'default' => '', |
||
| 72 | 'required' => false, |
||
| 73 | ), |
||
| 74 | 'payment' => array( |
||
| 75 | 'code' => 'payment', |
||
| 76 | 'internalcode' => 'payment', |
||
| 77 | 'label' => 'Add payment option automatically', |
||
| 78 | 'type' => 'boolean', |
||
| 79 | 'internaltype' => 'boolean', |
||
| 80 | 'default' => '', |
||
| 81 | 'required' => false, |
||
| 82 | ), |
||
| 83 | 'paymentcode' => array( |
||
| 84 | 'code' => 'paymentcode', |
||
| 85 | 'internalcode' => 'paymentcode', |
||
| 86 | 'label' => 'Add payment by code', |
||
| 87 | 'type' => 'string', |
||
| 88 | 'internaltype' => 'string', |
||
| 89 | 'default' => '', |
||
| 90 | 'required' => false, |
||
| 91 | ), |
||
| 92 | 'useorder' => array( |
||
| 93 | 'code' => 'useorder', |
||
| 94 | 'internalcode' => 'useorder', |
||
| 95 | 'label' => 'Add from last order', |
||
| 96 | 'type' => 'boolean', |
||
| 97 | 'internaltype' => 'boolean', |
||
| 98 | 'default' => '', |
||
| 99 | 'required' => false, |
||
| 100 | ), |
||
| 101 | 'orderaddress' => array( |
||
| 102 | 'code' => 'orderaddress', |
||
| 103 | 'internalcode' => 'orderaddress', |
||
| 104 | 'label' => 'Add address from last order', |
||
| 105 | 'type' => 'boolean', |
||
| 106 | 'internaltype' => 'boolean', |
||
| 107 | 'default' => '', |
||
| 108 | 'required' => false, |
||
| 109 | ), |
||
| 110 | 'orderservice' => array( |
||
| 111 | 'code' => 'orderservice', |
||
| 112 | 'internalcode' => 'orderservice', |
||
| 113 | 'label' => 'Add delivery/payment from last order', |
||
| 114 | 'type' => 'boolean', |
||
| 115 | 'internaltype' => 'boolean', |
||
| 116 | 'default' => '', |
||
| 117 | 'required' => false, |
||
| 118 | ), |
||
| 119 | ); |
||
| 120 | |||
| 121 | |||
| 122 | /** |
||
| 123 | * Checks the backend configuration attributes for validity. |
||
| 124 | * |
||
| 125 | * @param array $attributes Attributes added by the shop owner in the administraton interface |
||
| 126 | * @return array An array with the attribute keys as key and an error message as values for all attributes that are |
||
| 127 | * known by the provider but aren't valid |
||
| 128 | */ |
||
| 129 | public function checkConfigBE( array $attributes ) : array |
||
| 134 | } |
||
| 135 | |||
| 136 | |||
| 137 | /** |
||
| 138 | * Returns the configuration attribute definitions of the provider to generate a list of available fields and |
||
| 139 | * rules for the value of each field in the administration interface. |
||
| 140 | * |
||
| 141 | * @return array List of attribute definitions implementing \Aimeos\Base\Critera\Attribute\Iface |
||
| 142 | */ |
||
| 143 | public function getConfigBE() : array |
||
| 146 | } |
||
| 147 | |||
| 148 | |||
| 149 | /** |
||
| 150 | * Subscribes itself to a publisher |
||
| 151 | * |
||
| 152 | * @param \Aimeos\MW\Observer\Publisher\Iface $p Object implementing publisher interface |
||
| 153 | * @return \Aimeos\MShop\Plugin\Provider\Iface Plugin object for method chaining |
||
| 154 | */ |
||
| 155 | public function register( \Aimeos\MW\Observer\Publisher\Iface $p ) : \Aimeos\MW\Observer\Listener\Iface |
||
| 166 | } |
||
| 167 | |||
| 168 | |||
| 169 | /** |
||
| 170 | * Receives a notification from a publisher object |
||
| 171 | * |
||
| 172 | * @param \Aimeos\MW\Observer\Publisher\Iface $order Shop basket instance implementing publisher interface |
||
| 173 | * @param string $action Name of the action to listen for |
||
| 174 | * @param mixed $value Object or value changed in publisher |
||
| 175 | * @return mixed Modified value parameter |
||
| 176 | * @throws \Aimeos\MShop\Plugin\Provider\Exception if an error occurs |
||
| 177 | */ |
||
| 178 | public function update( \Aimeos\MW\Observer\Publisher\Iface $order, string $action, $value = null ) |
||
| 206 | } |
||
| 207 | |||
| 208 | |||
| 209 | /** |
||
| 210 | * Returns the order service item for the given type and code if available. |
||
| 211 | * |
||
| 212 | * @param \Aimeos\MShop\Order\Item\Iface $order Basket of the customer |
||
| 213 | * @param string $type Service type constant from \Aimeos\MShop\Order\Item\Service\Base |
||
| 214 | * @param string|null $code Service item code |
||
| 215 | * @return \Aimeos\MShop\Order\Item\Service\Iface|null Order service item if available or null otherwise |
||
| 216 | */ |
||
| 217 | protected function getServiceItem( \Aimeos\MShop\Order\Item\Iface $order, string $type, |
||
| 241 | } |
||
| 242 | |||
| 243 | |||
| 244 | /** |
||
| 245 | * Adds the addresses from the given order item to the basket. |
||
| 246 | * |
||
| 247 | * @param \Aimeos\MShop\Order\Item\Iface $order Basket object |
||
| 248 | * @param \Aimeos\MShop\Order\Item\Iface $item Existing order to fetch the addresses from |
||
| 249 | * @return \Aimeos\MShop\Order\Item\Iface Updated basket object |
||
| 250 | */ |
||
| 251 | protected function setAddresses( \Aimeos\MShop\Order\Item\Iface $order, |
||
| 252 | \Aimeos\MShop\Order\Item\Iface $item ) : \Aimeos\MShop\Order\Item\Iface |
||
| 253 | { |
||
| 254 | if( $order->getAddresses()->isEmpty() && (bool) $this->getConfigValue( 'orderaddress', true ) === true ) |
||
| 255 | { |
||
| 256 | $map = $item->getAddresses(); |
||
| 257 | |||
| 258 | foreach( $map as $type => $list ) { |
||
| 259 | map( $list )->setId( null ); |
||
| 260 | } |
||
| 261 | |||
| 262 | $order->setAddresses( $map ); |
||
| 263 | } |
||
| 264 | |||
| 265 | return $order; |
||
| 266 | } |
||
| 267 | |||
| 268 | |||
| 269 | /** |
||
| 270 | * Adds the services from the given order item to the basket. |
||
| 271 | * |
||
| 272 | * @param \Aimeos\MShop\Order\Item\Iface $order Basket object |
||
| 273 | * @param \Aimeos\MShop\Order\Item\Iface $item Existing order to fetch the services from |
||
| 274 | * @return \Aimeos\MShop\Order\Item\Iface Updated basket object |
||
| 275 | */ |
||
| 276 | protected function setServices( \Aimeos\MShop\Order\Item\Iface $order, |
||
| 277 | \Aimeos\MShop\Order\Item\Iface $item ) : \Aimeos\MShop\Order\Item\Iface |
||
| 278 | { |
||
| 279 | if( $order->getServices()->isEmpty() && $this->getConfigValue( 'orderservice', true ) == true ) |
||
| 280 | { |
||
| 281 | $map = $item->getServices(); |
||
| 282 | $serviceManager = \Aimeos\MShop::create( $this->context(), 'service' ); |
||
| 283 | |||
| 284 | foreach( $map as $type => $list ) |
||
| 285 | { |
||
| 286 | foreach( $list as $key => $service ) |
||
| 287 | { |
||
| 288 | if( $serviceItem = $service->getServiceItem() ) |
||
| 289 | { |
||
| 290 | $provider = $serviceManager->getProvider( $service->getServiceItem(), $service->getType() ); |
||
| 291 | |||
| 292 | if( $provider->isAvailable( $order ) === true ) |
||
| 293 | { |
||
| 294 | $attrItems = $service->getAttributeItems()->filter( function( $attr ) { |
||
| 295 | return in_array( $attrItem->getType(), ['', 'hidden'] ); |
||
| 296 | } ); |
||
| 297 | |||
| 298 | $service->setId( null )->setAttributeItems( $attrItems->setId( null ) ); |
||
| 299 | } |
||
| 300 | else |
||
| 301 | { |
||
| 302 | unset( $map[$type][$key] ); |
||
| 303 | } |
||
| 304 | } |
||
| 305 | } |
||
| 306 | } |
||
| 307 | |||
| 308 | $order->setServices( $map ); |
||
| 309 | } |
||
| 310 | |||
| 311 | return $order; |
||
| 312 | } |
||
| 313 | |||
| 314 | |||
| 315 | /** |
||
| 316 | * Adds the default addresses to the basket if they are not available. |
||
| 317 | * |
||
| 318 | * @param \Aimeos\MShop\Order\Item\Iface $order Basket object |
||
| 319 | * @return \Aimeos\MShop\Order\Item\Iface Updated basket object |
||
| 320 | */ |
||
| 321 | protected function setAddressDefault( \Aimeos\MShop\Order\Item\Iface $order ) : \Aimeos\MShop\Order\Item\Iface |
||
| 322 | { |
||
| 323 | $context = $this->context(); |
||
| 324 | $addresses = $order->getAddresses(); |
||
| 325 | $type = \Aimeos\MShop\Order\Item\Address\Base::TYPE_PAYMENT; |
||
| 326 | |||
| 327 | if( $context->user() !== null && !isset( $addresses[$type] ) |
||
| 328 | && (bool) $this->getConfigValue( 'address', false ) === true |
||
| 329 | ) { |
||
| 330 | $address = \Aimeos\MShop::create( $context, 'customer' ) |
||
| 331 | ->get( $context->user() )->getPaymentAddress(); |
||
| 332 | |||
| 333 | $addrItem = \Aimeos\MShop::create( $context, 'order/address' ) |
||
| 334 | ->create()->copyFrom( $address ); |
||
| 335 | |||
| 336 | $order->addAddress( $addrItem, $type ); |
||
| 337 | } |
||
| 338 | |||
| 339 | return $order; |
||
| 340 | } |
||
| 341 | |||
| 342 | |||
| 343 | /** |
||
| 344 | * Adds the default services to the basket if they are not available. |
||
| 345 | * |
||
| 346 | * @param \Aimeos\MShop\Order\Item\Iface $order Basket object |
||
| 347 | * @return \Aimeos\MShop\Order\Item\Iface Updated basket object |
||
| 348 | */ |
||
| 349 | protected function setServicesDefault( \Aimeos\MShop\Order\Item\Iface $order ) : \Aimeos\MShop\Order\Item\Iface |
||
| 385 | } |
||
| 386 | } |
||
| 387 |