| Total Complexity | 42 |
| Total Lines | 224 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like CheckoutComponentConfig 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 CheckoutComponentConfig, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class CheckoutComponentConfig |
||
| 18 | { |
||
| 19 | use Injectable; |
||
| 20 | |||
| 21 | protected $components; |
||
| 22 | |||
| 23 | protected $order; |
||
| 24 | |||
| 25 | protected $namespaced; //namespace fields according to their component |
||
| 26 | |||
| 27 | public function __construct(Order $order, $namespaced = true) |
||
| 28 | { |
||
| 29 | $this->components = ArrayList::create(); |
||
| 30 | $this->order = $order; |
||
| 31 | $this->namespaced = $namespaced; |
||
| 32 | } |
||
| 33 | |||
| 34 | public function getOrder() |
||
| 35 | { |
||
| 36 | return $this->order; |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @param CheckoutComponent $component |
||
| 41 | * @param string $insertBefore The class of the component to insert this one before |
||
| 42 | * @return $this |
||
| 43 | */ |
||
| 44 | public function addComponent(CheckoutComponent $component, $insertBefore = null) |
||
| 45 | { |
||
| 46 | if ($this->namespaced) { |
||
| 47 | $component = CheckoutComponentNamespaced::create($component); |
||
| 48 | } |
||
| 49 | if ($insertBefore) { |
||
| 50 | $existingItems = $this->getComponents(); |
||
| 51 | $this->components = ArrayList::create(); |
||
| 52 | $inserted = false; |
||
| 53 | foreach ($existingItems as $existingItem) { |
||
| 54 | if (!$inserted && $existingItem instanceof $insertBefore) { |
||
| 55 | $this->components->push($component); |
||
| 56 | $inserted = true; |
||
| 57 | } |
||
| 58 | $this->components->push($existingItem); |
||
| 59 | } |
||
| 60 | if (!$inserted) { |
||
| 61 | $this->components->push($component); |
||
| 62 | } |
||
| 63 | } else { |
||
| 64 | $this->getComponents()->push($component); |
||
| 65 | } |
||
| 66 | return $this; |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @return ArrayList Of CheckoutComponent |
||
| 71 | */ |
||
| 72 | public function getComponents() |
||
| 73 | { |
||
| 74 | if (!$this->components) { |
||
| 75 | $this->components = ArrayList::create(); |
||
| 76 | } |
||
| 77 | return $this->components; |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Returns the first available component with the given class or interface. |
||
| 82 | * |
||
| 83 | * @param string $type ClassName |
||
| 84 | * |
||
| 85 | * @return CheckoutComponent |
||
| 86 | */ |
||
| 87 | public function getComponentByType($type) |
||
| 88 | { |
||
| 89 | foreach ($this->components as $component) { |
||
| 90 | if ($this->namespaced) { |
||
| 91 | if ($component->Proxy() instanceof $type) { |
||
| 92 | return $component->Proxy(); |
||
| 93 | } |
||
| 94 | } else { |
||
| 95 | if ($component instanceof $type) { |
||
| 96 | return $component; |
||
| 97 | } |
||
| 98 | } |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Whether or not this config has a component that collects payment data. |
||
| 104 | * Should be used to determine whether or not to add an additional payment step after checkout. |
||
| 105 | * @return bool |
||
| 106 | */ |
||
| 107 | public function hasComponentWithPaymentData() |
||
| 108 | { |
||
| 109 | foreach ($this->getComponents() as $component) { |
||
| 110 | if ($component->providesPaymentData()) { |
||
| 111 | return true; |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | return false; |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Get combined form fields |
||
| 120 | * |
||
| 121 | * @return FieldList namespaced fields |
||
| 122 | */ |
||
| 123 | public function getFormFields() |
||
| 124 | { |
||
| 125 | $fields = FieldList::create(); |
||
| 126 | foreach ($this->getComponents() as $component) { |
||
| 127 | if ($cfields = $component->getFormFields($this->order)) { |
||
| 128 | $fields->merge($cfields); |
||
| 129 | } else { |
||
| 130 | user_error('getFields on ' . get_class($component) . ' must return a FieldList'); |
||
| 131 | } |
||
| 132 | } |
||
| 133 | return $fields; |
||
| 134 | } |
||
| 135 | |||
| 136 | public function getRequiredFields() |
||
| 137 | { |
||
| 138 | $required = []; |
||
| 139 | foreach ($this->getComponents() as $component) { |
||
| 140 | $required = array_merge($required, $component->getRequiredFields($this->order)); |
||
| 141 | } |
||
| 142 | return $required; |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Validate every component against given data. |
||
| 147 | * |
||
| 148 | * @param array $data data to validate |
||
| 149 | * |
||
| 150 | * @return boolean validation result |
||
| 151 | * @throws ValidationException |
||
| 152 | */ |
||
| 153 | public function validateData($data) |
||
| 154 | { |
||
| 155 | $result = ValidationResult::create(); |
||
| 156 | foreach ($this->getComponents() as $component) { |
||
| 157 | try { |
||
| 158 | $component->validateData($this->order, $this->dependantData($component, $data)); |
||
| 159 | } catch (ValidationException $e) { |
||
| 160 | //transfer messages into a single result |
||
| 161 | foreach ($e->getResult()->getMessages() as $code => $message) { |
||
| 162 | if (is_numeric($code)) { |
||
| 163 | $code = null; |
||
| 164 | } |
||
| 165 | if ($this->namespaced) { |
||
| 166 | $code = $component->namespaceFieldName($code); |
||
| 167 | } |
||
| 168 | $result->addError($message['message'], $code); |
||
| 169 | } |
||
| 170 | } |
||
| 171 | } |
||
| 172 | |||
| 173 | $this->order->extend('onValidateDataOnCheckout', $result); |
||
| 174 | |||
| 175 | if (!$result->isValid()) { |
||
| 176 | throw new ValidationException($result); |
||
| 177 | } |
||
| 178 | |||
| 179 | return true; |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Get combined data |
||
| 184 | * |
||
| 185 | * @return array map of field names to data values |
||
| 186 | */ |
||
| 187 | public function getData() |
||
| 188 | { |
||
| 189 | $data = []; |
||
| 190 | |||
| 191 | foreach ($this->getComponents() as $component) { |
||
| 192 | $orderdata = $component->getData($this->order); |
||
| 193 | |||
| 194 | if (is_array($orderdata)) { |
||
| 195 | $data = array_merge($data, $orderdata); |
||
| 196 | } else { |
||
| 197 | user_error('getData on ' . $component->name() . ' must return an array'); |
||
| 198 | } |
||
| 199 | } |
||
| 200 | return $data; |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Set data on all components |
||
| 205 | * |
||
| 206 | * @param array $data map of field names to data values |
||
| 207 | */ |
||
| 208 | public function setData($data) |
||
| 212 | } |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Helper function for saving data from other components. |
||
| 217 | */ |
||
| 218 | protected function dependantData($component, $data) |
||
| 219 | { |
||
| 220 | if (!$this->namespaced) { //no need to try and get un-namespaced dependant data |
||
| 241 | } |
||
| 242 | } |
||
| 243 |