Complex classes like Option 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Option, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class Option extends AbstractService |
||
| 9 | { |
||
| 10 | protected $entityMapper = 'speckcatalog_option_mapper'; |
||
| 11 | protected $choiceService; |
||
| 12 | protected $imageService; |
||
| 13 | protected $productService; |
||
| 14 | |||
| 15 | 1 | public function getByProductId($productId, $populate = false, $recursive = false, array $extraFields = array()) |
|
| 16 | { |
||
| 17 | 1 | $options = $this->getEntityMapper()->getByProductId($productId, $extraFields); |
|
| 18 | 1 | if ($populate) { |
|
| 19 | 1 | foreach ($options as $option) { |
|
| 20 | 1 | $this->populate($option, $recursive); |
|
| 21 | 1 | } |
|
| 22 | 1 | } |
|
| 23 | 1 | return $options; |
|
| 24 | } |
||
| 25 | |||
| 26 | 1 | public function getByParentChoiceId($choiceId, $populate = false) |
|
| 27 | { |
||
| 28 | 1 | $options = $this->getEntityMapper()->getByParentChoiceId($choiceId); |
|
| 29 | 1 | if ($populate) { |
|
| 30 | 1 | foreach ($options as $option) { |
|
| 31 | 1 | $this->populate($option); |
|
| 32 | 1 | } |
|
| 33 | 1 | } |
|
| 34 | 1 | return $options; |
|
| 35 | } |
||
| 36 | |||
| 37 | public function getBuildersByProductId($productId) |
||
| 38 | { |
||
| 39 | $choices = $this->getEntityMapper()->getBuildersByProductId($productId); |
||
| 40 | var_dump($choices); |
||
|
|
|||
| 41 | die(); |
||
| 42 | $builders = array(); |
||
| 43 | foreach ($choices as $choice) { |
||
| 44 | $builders[$choice['product_id']][] = $choice['choice_id']; |
||
| 45 | } |
||
| 46 | foreach ($builders as $pid => $choiceIds) { |
||
| 47 | sort($choiceIds); |
||
| 48 | $builders[$pid] = implode(',', $choiceIds); |
||
| 49 | } |
||
| 50 | return $builders; |
||
| 51 | } |
||
| 52 | |||
| 53 | 1 | public function populate($option, $recursive = false, $children = true) |
|
| 54 | { |
||
| 55 | 1 | $optionId = $option->getOptionId(); |
|
| 56 | |||
| 57 | 1 | $allChildren = ($children === true) ? true : false; |
|
| 58 | 1 | $children = (is_array($children)) ? $children : array(); |
|
| 59 | |||
| 60 | 1 | if ($allChildren || in_array('choices', $children)) { |
|
| 61 | 1 | $choiceService = $this->getChoiceService(); |
|
| 62 | 1 | $choices = $choiceService->getByOptionId($optionId, true, $recursive); |
|
| 63 | 1 | $option->setChoices($choices); |
|
| 64 | 1 | } |
|
| 65 | 1 | if ($allChildren || in_array('images', $children)) { |
|
| 66 | 1 | $imageService = $this->getImageService(); |
|
| 67 | 1 | $option->setImages($imageService->getImages('option', $optionId)); |
|
| 68 | 1 | } |
|
| 69 | 1 | } |
|
| 70 | |||
| 71 | 2 | public function insert($option) |
|
| 72 | { |
||
| 73 | 2 | $parent = $this->getExistingParent($option); |
|
| 74 | 2 | $option = parent::insert($option); |
|
| 75 | |||
| 76 | 2 | if ($parent instanceof \SpeckCatalog\Model\Product) { |
|
| 77 | 1 | $this->getProductService()->addOption($parent, $option); |
|
| 78 | 1 | $option->setParent($parent); |
|
| 79 | 1 | } |
|
| 80 | 2 | if ($parent instanceof \SpeckCatalog\Model\Choice) { |
|
| 81 | 1 | $this->getChoiceService()->addOption($parent, $option); |
|
| 82 | 1 | $option->setParent($parent); |
|
| 83 | 1 | } |
|
| 84 | 2 | return $option; |
|
| 85 | } |
||
| 86 | |||
| 87 | 2 | public function getExistingParent($option) |
|
| 104 | |||
| 105 | 2 | public function update($option, array $originalValues = null) |
|
| 106 | { |
||
| 107 | 2 | if (null === $originalValues && is_array($option)) { |
|
| 108 | 1 | $originalValues['option_id'] = $option['option_id']; |
|
| 109 | 1 | } |
|
| 110 | 2 | if (null === $originalValues && $option instanceof \SpeckCatalog\Model\Option) { |
|
| 111 | 1 | $originalValues['option_id'] = $option->getOptionId(); |
|
| 112 | 1 | } |
|
| 113 | 2 | parent::update($option, $originalValues); |
|
| 114 | 2 | } |
|
| 115 | |||
| 116 | public function removeChoice($optionData, $choiceData) |
||
| 117 | { |
||
| 118 | $choiceService = $this->getChoiceService(); |
||
| 119 | |||
| 120 | $found = $choiceService->find($choiceData); |
||
| 121 | if (!$found) { |
||
| 122 | throw new \Exception(sprintf('choice not found - %n', $choiceData['choice_id'])); |
||
| 123 | } |
||
| 124 | |||
| 125 | $choiceService->delete($choiceData); //delete |
||
| 126 | if ($choiceService->find($choiceData)) { |
||
| 127 | throw new \Exception(sprintf('delete unsuccessful - %n remains after delete', $choiceData['choice_id'])); |
||
| 128 | } |
||
| 129 | |||
| 130 | return true; //choice was found, and then deleted successfully |
||
| 131 | } |
||
| 132 | |||
| 133 | 1 | public function sortChoices($optionId, array $order = array()) |
|
| 137 | |||
| 138 | /** |
||
| 139 | * @return choiceService |
||
| 140 | */ |
||
| 141 | 3 | public function getChoiceService() |
|
| 142 | { |
||
| 143 | 3 | if (null === $this->choiceService) { |
|
| 144 | 1 | $this->choiceService = $this->getServiceLocator()->get('speckcatalog_choice_service'); |
|
| 148 | |||
| 149 | /** |
||
| 150 | * @param $choiceService |
||
| 151 | * @return self |
||
| 152 | */ |
||
| 153 | 4 | public function setChoiceService($choiceService) |
|
| 158 | |||
| 159 | /** |
||
| 160 | * @return imageService |
||
| 161 | */ |
||
| 162 | 3 | public function getImageService() |
|
| 169 | |||
| 170 | /** |
||
| 171 | * @param $imageService |
||
| 172 | * @return self |
||
| 173 | */ |
||
| 174 | 2 | public function setImageService($imageService) |
|
| 179 | |||
| 180 | /** |
||
| 181 | * @return productService |
||
| 182 | */ |
||
| 183 | 2 | public function getProductService() |
|
| 190 | |||
| 191 | /** |
||
| 192 | * @param $productService |
||
| 193 | * @return self |
||
| 194 | */ |
||
| 195 | 1 | public function setProductService($productService) |
|
| 200 | } |
||
| 201 |