Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 28 | class PointDiffProcessor extends ItemHolderValidator implements PurchaseProcessor |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * @var EntityManagerInterface |
||
| 32 | */ |
||
| 33 | protected $entityManager; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var BaseInfo |
||
| 37 | */ |
||
| 38 | protected $BaseInfo; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * AddPointProcessor constructor. |
||
| 42 | * |
||
| 43 | * @param EntityManagerInterface $entityManager |
||
| 44 | * @param BaseInfo $BaseInfo |
||
|
|
|||
| 45 | */ |
||
| 46 | 712 | public function __construct(EntityManagerInterface $entityManager, BaseInfo $BaseInfo) |
|
| 51 | |||
| 52 | /* |
||
| 53 | * ItemHolderValidator |
||
| 54 | */ |
||
| 55 | |||
| 56 | /** |
||
| 57 | * {@inheritdoc} |
||
| 58 | */ |
||
| 59 | 176 | protected function validate(ItemHolderInterface $itemHolder, PurchaseContext $context) |
|
| 78 | |||
| 79 | /* |
||
| 80 | * PurchaseProcessor |
||
| 81 | */ |
||
| 82 | |||
| 83 | /** |
||
| 84 | * {@inheritdoc} |
||
| 85 | */ |
||
| 86 | 16 | View Code Duplication | public function prepare(ItemHolderInterface $itemHolder, PurchaseContext $context) |
| 98 | |||
| 99 | /** |
||
| 100 | * {@inheritdoc |
||
| 101 | */ |
||
| 102 | public function commit(ItemHolderInterface $target, PurchaseContext $context) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * {@inheritdoc |
||
| 109 | */ |
||
| 110 | View Code Duplication | public function rollback(ItemHolderInterface $itemHolder, PurchaseContext $context) |
|
| 121 | |||
| 122 | /* |
||
| 123 | * Helper methods |
||
| 124 | */ |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Processorが実行出来るかどうかを返す. |
||
| 128 | * |
||
| 129 | * 以下を満たす場合に実行できる. |
||
| 130 | * |
||
| 131 | * - ポイント設定が有効であること. |
||
| 132 | * - $itemHolderがOrderエンティティであること. |
||
| 133 | * - OrderStatusが新規受付、入金済み、対応中、発送済みのどれかであること |
||
| 134 | * - 会員のOrderであること. |
||
| 135 | * - PurchaseContextでOriginHolderが渡ってきている |
||
| 136 | * |
||
| 137 | * @param ItemHolderInterface $itemHolder |
||
| 138 | * @param PurchaseContext $context |
||
| 139 | * |
||
| 140 | * @return bool |
||
| 141 | */ |
||
| 142 | 187 | private function supports(ItemHolderInterface $itemHolder, PurchaseContext $context) |
|
| 143 | { |
||
| 144 | 187 | if (!$this->BaseInfo->isOptionPoint()) { |
|
| 145 | return false; |
||
| 146 | } |
||
| 147 | |||
| 148 | 187 | if (!$itemHolder instanceof Order) { |
|
| 149 | return false; |
||
| 150 | } |
||
| 151 | |||
| 152 | 187 | switch ($itemHolder->getOrderStatus()->getId()) { |
|
| 153 | case OrderStatus::NEW: |
||
| 154 | case OrderStatus::PAID: |
||
| 155 | case OrderStatus::IN_PROGRESS: |
||
| 156 | case OrderStatus::DELIVERED: |
||
| 157 | 35 | break; |
|
| 158 | default: |
||
| 159 | 155 | return false; |
|
| 160 | } |
||
| 161 | |||
| 162 | 35 | if (!$itemHolder->getCustomer()) { |
|
| 163 | return false; |
||
| 164 | } |
||
| 165 | |||
| 166 | 35 | if (is_null($context->getOriginHolder())) { |
|
| 167 | return false; |
||
| 168 | } |
||
| 169 | |||
| 170 | 35 | return true; |
|
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * 利用ポイントの差を計算する |
||
| 175 | * この差が新規利用ポイントとなる |
||
| 176 | * |
||
| 177 | * 使用ポイントが増えた場合プラスとなる |
||
| 178 | * 50 -> 100 : 50 |
||
| 179 | * 100 -> 50 : -50 |
||
| 180 | * |
||
| 181 | * @param ItemHolderInterface $itemHolder |
||
| 182 | * @param PurchaseContext $context |
||
| 183 | * |
||
| 184 | * @return int |
||
| 185 | */ |
||
| 186 | 35 | protected function getDiffOfUsePoint(ItemHolderInterface $itemHolder, PurchaseContext $context) |
|
| 197 | } |
||
| 198 |