CaptureCommandExecutor   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 31
c 1
b 0
f 0
dl 0
loc 88
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A executeOmsCommand() 0 28 3
A executeTransactionUpdatePaymentFirstDataItems() 0 6 2
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\FirstData\Business\CommandExecutor;
9
10
use Generated\Shared\Transfer\FirstDataApiRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...tDataApiRequestTransfer 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\FirstDataOmsCommandRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...sCommandRequestTransfer 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 Spryker\Zed\Kernel\Persistence\EntityManager\TransactionTrait;
13
use SprykerEco\Zed\FirstData\Business\Api\ApiClient\FirstDataApiClientInterface;
14
use SprykerEco\Zed\FirstData\FirstDataConfig;
15
use SprykerEco\Zed\FirstData\Persistence\FirstDataEntityManagerInterface;
16
use SprykerEco\Zed\FirstData\Persistence\FirstDataRepositoryInterface;
17
18
class CaptureCommandExecutor implements FirstDataCommandExecutorInterface
19
{
20
    use TransactionTrait;
21
22
    /**
23
     * @var \SprykerEco\Zed\FirstData\Persistence\FirstDataEntityManagerInterface
24
     */
25
    protected $entityManager;
26
27
    /**
28
     * @var \SprykerEco\Zed\FirstData\Persistence\FirstDataRepositoryInterface
29
     */
30
    protected $firstDataRepository;
31
32
    /**
33
     * @var \SprykerEco\Zed\FirstData\FirstDataConfig
34
     */
35
    protected $firstDataConfig;
36
37
    /**
38
     * @var \SprykerEco\Zed\FirstData\Business\Api\ApiClient\FirstDataApiClientInterface
39
     */
40
    protected $firstDataApiClient;
41
42
    /**
43
     * @param \SprykerEco\Zed\FirstData\Business\Api\ApiClient\FirstDataApiClientInterface $firstDataApiClient
44
     * @param \SprykerEco\Zed\FirstData\Persistence\FirstDataEntityManagerInterface $entityManager
45
     * @param \SprykerEco\Zed\FirstData\Persistence\FirstDataRepositoryInterface $firstDataRepository
46
     * @param \SprykerEco\Zed\FirstData\FirstDataConfig $firstDataConfig
47
     */
48
    public function __construct(
49
        FirstDataApiClientInterface $firstDataApiClient,
50
        FirstDataEntityManagerInterface $entityManager,
51
        FirstDataRepositoryInterface $firstDataRepository,
52
        FirstDataConfig $firstDataConfig
53
    ) {
54
        $this->firstDataApiClient = $firstDataApiClient;
55
        $this->entityManager = $entityManager;
56
        $this->firstDataRepository = $firstDataRepository;
57
        $this->firstDataConfig = $firstDataConfig;
58
    }
59
60
    /**
61
     * @param \Generated\Shared\Transfer\FirstDataOmsCommandRequestTransfer $firstDataOmsCommandRequestTransfer
62
     *
63
     * @return void
64
     */
65
    public function executeOmsCommand(FirstDataOmsCommandRequestTransfer $firstDataOmsCommandRequestTransfer): void
66
    {
67
        $paymentFirstDataTransfer = $this->firstDataRepository
68
            ->findPaymentFirstDataByIdSalesOrder($firstDataOmsCommandRequestTransfer->getOrderOrFail()->getIdSalesOrderOrFail());
69
70
        if (!$paymentFirstDataTransfer) {
71
            return;
72
        }
73
74
        $firstDataApiRequestTransfer = (new FirstDataApiRequestTransfer())
75
            ->setRequestType(FirstDataConfig::FIRST_DATA_CAPTURE_REQUEST_TYPE)
76
            ->setOrder($firstDataOmsCommandRequestTransfer->getOrder())
77
            ->setOrderItemIds($firstDataOmsCommandRequestTransfer->getSalesOrderItemIds())
78
            ->setTransactionId($paymentFirstDataTransfer->getOid());
79
80
        $firstDataApiResponseTransfer = $this->firstDataApiClient->performApiRequest($firstDataApiRequestTransfer);
81
82
        if (!$firstDataApiResponseTransfer->getIsSuccess()) {
83
            return;
84
        }
85
86
        $paymentFirstDataItemTransfers = $this->firstDataRepository
87
            ->getPaymentFirstDataItemCollection(
88
                $paymentFirstDataTransfer,
89
                $firstDataOmsCommandRequestTransfer->getSalesOrderItemIds()
90
            );
91
92
        $this->executeTransactionUpdatePaymentFirstDataItems($paymentFirstDataItemTransfers);
93
    }
94
95
    /**
96
     * @param \Generated\Shared\Transfer\PaymentFirstDataItemTransfer[] $paymentFirstDataItemTransfers
97
     *
98
     * @return void
99
     */
100
    protected function executeTransactionUpdatePaymentFirstDataItems(array $paymentFirstDataItemTransfers): void
101
    {
102
        $this->getTransactionHandler()->handleTransaction(function () use ($paymentFirstDataItemTransfers): void {
103
            foreach ($paymentFirstDataItemTransfers as $paymentFirstDataItemTransfer) {
104
                $paymentFirstDataItemTransfer->setStatus($this->firstDataConfig->getOmsStatusCaptured());
105
                $this->entityManager->savePaymentFirstDataItem($paymentFirstDataItemTransfer);
106
            }
107
        });
108
    }
109
}
110