CapturePlugin::getOrderTransfer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace SprykerEco\Zed\AfterPay\Communication\Plugin\Oms\Command;
9
10
use Generated\Shared\Transfer\ItemTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\ItemTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Generated\Shared\Transfer\OrderTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\OrderTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Orm\Zed\Sales\Persistence\SpySalesOrder;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\Sales\Persistence\SpySalesOrder was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Spryker\Zed\Kernel\Communication\AbstractPlugin;
14
use Spryker\Zed\Oms\Business\Util\ReadOnlyArrayObject;
15
use Spryker\Zed\Oms\Dependency\Plugin\Command\CommandByOrderInterface;
16
17
/**
18
 * @method \SprykerEco\Zed\AfterPay\Business\AfterPayFacadeInterface getFacade()
19
 * @method \SprykerEco\Zed\AfterPay\Communication\AfterPayCommunicationFactory getFactory()
20
 * @method \SprykerEco\Zed\AfterPay\Persistence\AfterPayQueryContainer getQueryContainer()
21
 * @method \SprykerEco\Zed\AfterPay\AfterPayConfig getConfig()
22
 */
23
class CapturePlugin extends AbstractPlugin implements CommandByOrderInterface
24
{
25
    /**
26
     * {@inheritDoc}
27
     * - Sends payment `capture` request to AfterPay gateway, to capture payment for a specific order item.
28
     * - If it is the first item `capture` request for given order, captures also full expense amount.
29
     * - Saves the transaction result in DB and updates payment with new total captured amount.
30
     *
31
     * @api
32
     *
33
     * @param array<\Orm\Zed\Sales\Persistence\SpySalesOrderItem> $orderItems
34
     * @param \Orm\Zed\Sales\Persistence\SpySalesOrder $orderEntity
35
     * @param \Spryker\Zed\Oms\Business\Util\ReadOnlyArrayObject $data
36
     *
37
     * @return array
38
     */
39
    public function run(array $orderItems, SpySalesOrder $orderEntity, ReadOnlyArrayObject $data): array
40
    {
41
        $orderTransfer = $this->getOrderTransfer($orderEntity);
42
        $afterPayCallTransfer = $this->getFactory()
43
            ->createOrderToCallConverter()
44
            ->convert($orderTransfer);
45
46
        $items = $this->getItemTransfers($orderItems);
47
48
        $this->getFacade()->capturePayment($items, $afterPayCallTransfer);
49
50
        return [];
51
    }
52
53
    /**
54
     * @param array<\Orm\Zed\Sales\Persistence\SpySalesOrderItem> $orderItems
55
     *
56
     * @return array<\Generated\Shared\Transfer\ItemTransfer>
57
     */
58
    protected function getItemTransfers(array $orderItems): array
59
    {
60
        $items = [];
61
62
        foreach ($orderItems as $orderItem) {
63
            $items[] = (new ItemTransfer())
64
                ->fromArray($orderItem->toArray(), true)
65
                ->setUnitGrossPrice($orderItem->getGrossPrice())
66
                ->setUnitNetPrice($orderItem->getNetPrice())
67
                ->setUnitPriceToPayAggregation($orderItem->getPriceToPayAggregation())
68
                ->setUnitTaxAmountFullAggregation($orderItem->getTaxAmountFullAggregation());
69
        }
70
71
        return $items;
72
    }
73
74
    /**
75
     * @param \Orm\Zed\Sales\Persistence\SpySalesOrder $order
76
     *
77
     * @return \Generated\Shared\Transfer\OrderTransfer
78
     */
79
    protected function getOrderTransfer(SpySalesOrder $order): OrderTransfer
80
    {
81
        return $this->getFactory()
82
            ->getSalesFacade()
83
            ->getOrderByIdSalesOrder($order->getIdSalesOrder());
84
    }
85
}
86