executeTransactionUpdatePaymentFirstDataItems()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 2
nc 1
nop 1
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 CancelCommandExecutor 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_CANCEL_RESERVATION_REQUEST_TYPE)
76
            ->setOrder($firstDataOmsCommandRequestTransfer->getOrder())
77
            ->setTransactionId($paymentFirstDataTransfer->getOid());
78
79
        $firstDataApiResponseTransfer = $this->firstDataApiClient->performApiRequest($firstDataApiRequestTransfer);
80
81
        if (!$firstDataApiResponseTransfer->getIsSuccess()) {
82
            return;
83
        }
84
85
        $paymentFirstDataItemTransfers = $this->firstDataRepository
86
            ->getPaymentFirstDataItemCollection($paymentFirstDataTransfer);
87
88
        $this->executeTransactionUpdatePaymentFirstDataItems($paymentFirstDataItemTransfers);
89
    }
90
91
    /**
92
     * @param \Generated\Shared\Transfer\PaymentFirstDataItemTransfer[] $paymentFirstDataItemTransfers
93
     *
94
     * @return void
95
     */
96
    protected function executeTransactionUpdatePaymentFirstDataItems(array $paymentFirstDataItemTransfers): void
97
    {
98
        $this->getTransactionHandler()->handleTransaction(function () use ($paymentFirstDataItemTransfers): void {
99
            foreach ($paymentFirstDataItemTransfers as $paymentFirstDataItemTransfer) {
100
                $paymentFirstDataItemTransfer->setStatus($this->firstDataConfig->getOmsStatusCanceled());
101
                $this->entityManager->savePaymentFirstDataItem($paymentFirstDataItemTransfer);
102
            }
103
        });
104
    }
105
}
106