| Total Complexity | 26 |
| Total Lines | 261 |
| Duplicated Lines | 16.86 % |
| Changes | 0 | ||
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 | abstract class AbstractAmazonpayCommandPlugin extends AbstractPlugin implements CommandByOrderInterface |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * @var bool |
||
| 32 | */ |
||
| 33 | protected $wasShippingCharged; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @return string |
||
| 37 | */ |
||
| 38 | protected function getAffectingRequestedAmountItemsStateFlag() |
||
| 39 | { |
||
| 40 | return ''; |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @param \Orm\Zed\Sales\Persistence\SpySalesOrder $orderEntity |
||
| 45 | * @param \ArrayObject|\Generated\Shared\Transfer\ItemTransfer[] $itemTransfers |
||
| 46 | * |
||
| 47 | * @return int |
||
| 48 | */ |
||
| 49 | protected function getRequestedAmountByOrderAndItems(SpySalesOrder $orderEntity, ArrayObject $itemTransfers) |
||
| 50 | { |
||
| 51 | $subtotal = $this->getPriceToPay($itemTransfers); |
||
| 52 | |||
| 53 | if (!$this->wasShippingCharged |
||
| 54 | && $this->getFactory() |
||
| 55 | ->createRequestAmountCalculator() |
||
| 56 | ->shouldChargeShipping($orderEntity, $this->getAffectingRequestedAmountItemsStateFlag())) { |
||
| 57 | $subtotal += $this->getShipmentPrice($orderEntity); |
||
| 58 | $this->wasShippingCharged = true; |
||
| 59 | } |
||
| 60 | |||
| 61 | return $subtotal; |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param \ArrayObject $itemTransfers |
||
| 66 | * |
||
| 67 | * @return int |
||
| 68 | */ |
||
| 69 | protected function getPriceToPay(ArrayObject $itemTransfers) |
||
| 70 | { |
||
| 71 | $subtotal = 0; |
||
| 72 | |||
| 73 | foreach ($itemTransfers as $itemTransfer) { |
||
| 74 | $subtotal += $itemTransfer->getUnitPriceToPayAggregation(); |
||
| 75 | } |
||
| 76 | |||
| 77 | return $subtotal; |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @param \Orm\Zed\Sales\Persistence\SpySalesOrder $orderEntity |
||
| 82 | * |
||
| 83 | * @return int |
||
| 84 | */ |
||
| 85 | protected function getShipmentPrice(SpySalesOrder $orderEntity) |
||
| 86 | { |
||
| 87 | return $this->getExpenseByType($orderEntity, ShipmentConstants::SHIPMENT_EXPENSE_TYPE) |
||
| 88 | ->getPriceToPayAggregation(); |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @param \Orm\Zed\Sales\Persistence\SpySalesOrder $orderEntity |
||
| 93 | * @param string $type |
||
| 94 | * |
||
| 95 | * @return \Orm\Zed\Sales\Persistence\SpySalesExpense|null |
||
|
1 ignored issue
–
show
|
|||
| 96 | */ |
||
| 97 | protected function getExpenseByType(SpySalesOrder $orderEntity, $type) |
||
| 98 | { |
||
| 99 | foreach ($orderEntity->getExpenses() as $expense) { |
||
| 100 | if ($expense->getType() === $type) { |
||
| 101 | return $expense; |
||
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 105 | return null; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @param \Orm\Zed\Sales\Persistence\SpySalesOrderItem[] $salesOrderItems |
||
| 110 | * |
||
| 111 | * @return \Generated\Shared\Transfer\AmazonpayCallTransfer[] |
||
| 112 | */ |
||
| 113 | View Code Duplication | protected function groupSalesOrderItemsByAuthId(array $salesOrderItems) |
|
| 114 | { |
||
| 115 | $groups = []; |
||
| 116 | |||
| 117 | foreach ($salesOrderItems as $salesOrderItem) { |
||
| 118 | $payment = $this->getPaymentDetails($salesOrderItem); |
||
| 119 | |||
| 120 | if (!$payment) { |
||
| 121 | continue; |
||
| 122 | } |
||
| 123 | |||
| 124 | $groupData = $groups[$payment->getAuthorizationReferenceId()] ?? $this->createAmazonpayCallTransfer($payment); |
||
| 125 | |||
| 126 | $groupData->addItem( |
||
| 127 | $this->mapSalesOrderItemToItemTransfer($salesOrderItem) |
||
| 128 | ); |
||
| 129 | |||
| 130 | $groups[$payment->getAuthorizationReferenceId()] = $groupData; |
||
| 131 | } |
||
| 132 | |||
| 133 | return $groups; |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @param \Orm\Zed\Sales\Persistence\SpySalesOrderItem[] $salesOrderItems |
||
| 138 | * |
||
| 139 | * @return \Generated\Shared\Transfer\AmazonpayCallTransfer[] |
||
| 140 | */ |
||
| 141 | View Code Duplication | protected function groupSalesOrderItemsByCaptureId(array $salesOrderItems) |
|
| 142 | { |
||
| 143 | $groups = []; |
||
| 144 | |||
| 145 | foreach ($salesOrderItems as $salesOrderItem) { |
||
| 146 | $payment = $this->getPaymentDetails($salesOrderItem); |
||
| 147 | |||
| 148 | if (!$payment) { |
||
| 149 | continue; |
||
| 150 | } |
||
| 151 | |||
| 152 | $groupData = $groups[$payment->getAmazonCaptureId()] ?? $this->createAmazonpayCallTransfer($payment); |
||
| 153 | |||
| 154 | $groupData->addItem( |
||
| 155 | $this->mapSalesOrderItemToItemTransfer($salesOrderItem) |
||
| 156 | ); |
||
| 157 | |||
| 158 | $groups[$payment->getAmazonCaptureId()] = $groupData; |
||
| 159 | } |
||
| 160 | |||
| 161 | return $groups; |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @param \Orm\Zed\AmazonPay\Persistence\SpyPaymentAmazonpay $payment |
||
| 166 | * |
||
| 167 | * @return \Generated\Shared\Transfer\AmazonpayCallTransfer |
||
| 168 | */ |
||
| 169 | protected function createAmazonpayCallTransfer(SpyPaymentAmazonpay $payment) |
||
| 170 | { |
||
| 171 | $amazonPayment = $this->getFactory() |
||
| 172 | ->createPaymentAmazonpayConverter() |
||
| 173 | ->mapEntityToTransfer($payment); |
||
| 174 | |||
| 175 | $amazonpayCallTransfer = new AmazonpayCallTransfer(); |
||
| 176 | $amazonpayCallTransfer->setAmazonpayPayment($amazonPayment); |
||
| 177 | |||
| 178 | return $amazonpayCallTransfer; |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @param \Generated\Shared\Transfer\AmazonpayCallTransfer $amazonpayCallTransfer |
||
| 183 | * @param array $salesOrderItems |
||
| 184 | * |
||
| 185 | * @return void |
||
| 186 | */ |
||
| 187 | protected function populateItems(AmazonpayCallTransfer $amazonpayCallTransfer, array $salesOrderItems) |
||
| 188 | { |
||
| 189 | $amazonpayCallTransfer->setItems( |
||
| 190 | new ArrayObject( |
||
| 191 | array_map([$this, 'mapSalesOrderItemToItemTransfer'], $salesOrderItems) |
||
| 192 | ) |
||
| 193 | ); |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @param \Orm\Zed\Sales\Persistence\SpySalesOrderItem $salesOrderItem |
||
| 198 | * |
||
| 199 | * @return null|\Orm\Zed\AmazonPay\Persistence\SpyPaymentAmazonpay |
||
| 200 | */ |
||
| 201 | protected function getPaymentDetails(SpySalesOrderItem $salesOrderItem) |
||
| 202 | { |
||
| 203 | /** @var \Orm\Zed\AmazonPay\Persistence\SpyPaymentAmazonpaySalesOrderItem $payment */ |
||
| 204 | $payment = $salesOrderItem->getSpyPaymentAmazonpaySalesOrderItems()->getLast(); |
||
| 205 | |||
| 206 | if (!$payment) { |
||
| 207 | return null; |
||
| 208 | } |
||
| 209 | |||
| 210 | return $payment->getSpyPaymentAmazonpay(); |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param \Orm\Zed\Sales\Persistence\SpySalesOrderItem $salesOrderItemEntity |
||
| 215 | * |
||
| 216 | * @return \Generated\Shared\Transfer\ItemTransfer |
||
| 217 | */ |
||
| 218 | protected function mapSalesOrderItemToItemTransfer(SpySalesOrderItem $salesOrderItemEntity) |
||
| 219 | { |
||
| 220 | $itemTransfer = (new ItemTransfer()) |
||
| 221 | ->fromArray($salesOrderItemEntity->toArray(), true); |
||
| 222 | |||
| 223 | $itemTransfer->setUnitGrossPrice($salesOrderItemEntity->getGrossPrice()); |
||
| 224 | $itemTransfer->setUnitNetPrice($salesOrderItemEntity->getNetPrice()); |
||
| 225 | $itemTransfer->setUnitPrice($salesOrderItemEntity->getPrice()); |
||
| 226 | $itemTransfer->setUnitPriceToPayAggregation($salesOrderItemEntity->getPriceToPayAggregation()); |
||
| 227 | $itemTransfer->setUnitSubtotalAggregation($salesOrderItemEntity->getSubtotalAggregation()); |
||
| 228 | $itemTransfer->setUnitProductOptionPriceAggregation($salesOrderItemEntity->getProductOptionPriceAggregation()); |
||
| 229 | $itemTransfer->setUnitExpensePriceAggregation($salesOrderItemEntity->getExpensePriceAggregation()); |
||
| 230 | $itemTransfer->setUnitTaxAmount($salesOrderItemEntity->getTaxAmount()); |
||
| 231 | $itemTransfer->setUnitTaxAmountFullAggregation($salesOrderItemEntity->getTaxAmountFullAggregation()); |
||
| 232 | $itemTransfer->setUnitDiscountAmountAggregation($salesOrderItemEntity->getDiscountAmountAggregation()); |
||
| 233 | $itemTransfer->setUnitDiscountAmountFullAggregation($salesOrderItemEntity->getDiscountAmountFullAggregation()); |
||
| 234 | $itemTransfer->setRefundableAmount($salesOrderItemEntity->getRefundableAmount()); |
||
| 235 | |||
| 236 | return $itemTransfer; |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @param \IteratorAggregate|\Orm\Zed\Sales\Persistence\SpySalesOrderItem[] $salesOrderItems |
||
| 241 | * @param string $statusName |
||
| 242 | * |
||
| 243 | * @return void |
||
| 244 | */ |
||
| 245 | protected function setOrderItemsStatus(IteratorAggregate $salesOrderItems, $statusName) |
||
| 246 | { |
||
| 247 | $statusEntity = $this->getOmsStatusByName($statusName); |
||
| 248 | |||
| 249 | foreach ($salesOrderItems as $salesOrderItem) { |
||
| 250 | $salesOrderItem->setFkOmsOrderItemState($statusEntity->getIdOmsOrderItemState()); |
||
| 251 | $salesOrderItem->save(); |
||
| 252 | } |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @param string $statusName |
||
| 257 | * |
||
| 258 | * @return \Orm\Zed\Oms\Persistence\SpyOmsOrderItemState |
||
|
1 ignored issue
–
show
|
|||
| 259 | */ |
||
| 260 | protected function getOmsStatusByName($statusName) |
||
| 261 | { |
||
| 262 | $statusEntity = SpyOmsOrderItemStateQuery::create()->filterByName($statusName) |
||
| 263 | ->findOneOrCreate(); |
||
| 264 | $statusEntity->save(); |
||
| 265 | |||
| 266 | return $statusEntity; |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @param \Orm\Zed\Sales\Persistence\SpySalesOrderAddress $address |
||
| 271 | * |
||
| 272 | * @return \Generated\Shared\Transfer\AddressTransfer |
||
| 273 | */ |
||
| 274 | protected function buildAddressTransfer(SpySalesOrderAddress $address) |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @param \Orm\Zed\Sales\Persistence\SpySalesOrderItem[] $salesOrderItems |
||
| 283 | * |
||
| 284 | * @return \Orm\Zed\AmazonPay\Persistence\SpyPaymentAmazonpay |
||
| 285 | */ |
||
| 286 | protected function getPayment(array $salesOrderItems) |
||
| 289 | } |
||
| 290 | } |
||
| 291 |
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