Passed
Pull Request — master (#45)
by Aleksey
04:47
created

OrderManager::savePaymentDetail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
rs 10
1
<?php
2
3
/**
4
 * MIT License
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Zed\Payone\Business\Order;
9
10
use Generated\Shared\Transfer\CheckoutResponseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\CheckoutResponseTransfer 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\PaymentDetailTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\PaymentDetailTransfer 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 Generated\Shared\Transfer\PaymentPayoneOrderItemTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...PayoneOrderItemTransfer 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 Generated\Shared\Transfer\PayonePaymentTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\PayonePaymentTransfer 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...
14
use Generated\Shared\Transfer\QuoteTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\QuoteTransfer 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...
15
use Orm\Zed\Payone\Persistence\SpyPaymentPayone;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\Payone\Persistence\SpyPaymentPayone 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...
16
use Orm\Zed\Payone\Persistence\SpyPaymentPayoneDetail;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\Payone\Persistence\SpyPaymentPayoneDetail 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...
17
use Propel\Runtime\Propel;
18
use SprykerEco\Shared\Payone\PayoneTransactionStatusConstants;
19
use SprykerEco\Zed\Payone\PayoneConfig;
20
use SprykerEco\Zed\Payone\Persistence\PayoneEntityManagerInterface;
21
22
class OrderManager implements OrderManagerInterface
23
{
24
    /**
25
     * @var \SprykerEco\Zed\Payone\PayoneConfig
26
     */
27
    private $config;
28
29
    /**
30
     * @var \SprykerEco\Zed\Payone\Persistence\PayoneEntityManagerInterface
31
     */
32
    protected $payoneEntityManager;
33
34
    /**
35
     * @param \SprykerEco\Zed\Payone\PayoneConfig $config
36
     * @param \SprykerEco\Zed\Payone\Persistence\PayoneEntityManagerInterface $payoneEntityManager
37
     */
38
    public function __construct(
39
        PayoneConfig $config,
40
        PayoneEntityManagerInterface $payoneEntityManager
41
    ) {
42
        $this->config = $config;
43
        $this->payoneEntityManager = $payoneEntityManager;
44
    }
45
46
    /**
47
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
48
     * @param \Generated\Shared\Transfer\CheckoutResponseTransfer $checkoutResponse
49
     *
50
     * @return void
51
     */
52
    public function saveOrder(QuoteTransfer $quoteTransfer, CheckoutResponseTransfer $checkoutResponse)
53
    {
54
        Propel::getConnection()->beginTransaction();
55
56
        $paymentTransfer = $quoteTransfer->getPayment()->getPayone();
57
        $paymentTransfer->setFkSalesOrder($checkoutResponse->getSaveOrder()->getIdSalesOrder());
58
        $payment = $this->savePayment($paymentTransfer);
59
60
        $paymentDetailTransfer = $paymentTransfer->getPaymentDetail();
61
        $this->savePaymentDetail($payment, $paymentDetailTransfer);
62
        $this->savePaymentPayoneOrderItems($checkoutResponse, $payment->getIdPaymentPayone());
63
64
        Propel::getConnection()->commit();
65
    }
66
67
    /**
68
     * @param \Generated\Shared\Transfer\PayonePaymentTransfer $paymentTransfer
69
     *
70
     * @return \Orm\Zed\Payone\Persistence\SpyPaymentPayone
71
     */
72
    protected function savePayment(PayonePaymentTransfer $paymentTransfer)
73
    {
74
        $payment = new SpyPaymentPayone();
75
        $payment->fromArray(($paymentTransfer->toArray()));
76
77
        if ($payment->getReference() === null) {
78
            $orderEntity = $payment->getSpySalesOrder();
79
            $payment->setReference($this->config->generatePayoneReference($paymentTransfer, $orderEntity));
80
        }
81
82
        $payment->save();
83
84
        return $payment;
85
    }
86
87
    /**
88
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayone $payment
89
     * @param \Generated\Shared\Transfer\PaymentDetailTransfer $paymentDetailTransfer
90
     *
91
     * @return void
92
     */
93
    protected function savePaymentDetail(SpyPaymentPayone $payment, PaymentDetailTransfer $paymentDetailTransfer)
94
    {
95
        $paymentDetailEntity = new SpyPaymentPayoneDetail();
96
        $paymentDetailEntity->setSpyPaymentPayone($payment);
97
        $paymentDetailEntity->fromArray($paymentDetailTransfer->toArray());
98
        $paymentDetailEntity->save();
99
    }
100
101
    /**
102
     * @param \Generated\Shared\Transfer\CheckoutResponseTransfer $checkoutResponse
103
     * @param int $idSalesOrderItem
104
     *
105
     * @return void
106
     */
107
    protected function savePaymentPayoneOrderItems(CheckoutResponseTransfer $checkoutResponse, int $idSalesOrderItem): void
108
    {
109
        foreach ($checkoutResponse->getSaveOrder()->getOrderItems() as $itemTransfer) {
110
            $paymentPayoneOrderItemTransfer = (new PaymentPayoneOrderItemTransfer())
111
                ->setIdPaymentPayone($idSalesOrderItem)
112
                ->setIdSalesOrderItem($itemTransfer->getIdSalesOrderItem())
113
                ->setStatus(PayoneTransactionStatusConstants::STATUS_NEW);
114
115
            $this->payoneEntityManager->createPaymentPayoneOrderItem($paymentPayoneOrderItemTransfer);
116
        }
117
    }
118
}
119