Completed
Pull Request — master (#34)
by Roman
14:40 queued 07:36
created

Saver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
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\Braintree\Business\Order;
9
10
use Generated\Shared\Transfer\BraintreePaymentTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\BraintreePaymentTransfer 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\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...
12
use Generated\Shared\Transfer\SaveOrderTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\SaveOrderTransfer 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 Orm\Zed\Braintree\Persistence\SpyPaymentBraintree;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\Braintree\Persistence\SpyPaymentBraintree 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 Orm\Zed\Braintree\Persistence\SpyPaymentBraintreeOrderItem;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\Braintree\Persis...ymentBraintreeOrderItem 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 SprykerEco\Shared\Braintree\BraintreeConfig;
16
use SprykerEco\Zed\Braintree\Persistence\BraintreeEntityManagerInterface;
17
18
class Saver implements SaverInterface
19
{
20
    /**
21
     * @var \SprykerEco\Zed\Braintree\Persistence\BraintreeEntityManagerInterface
22
     */
23
    protected $braintreeEntityManager;
24
25
    /**
26
     * @param \SprykerEco\Zed\Braintree\Persistence\BraintreeEntityManagerInterface $braintreeEntityManager
27
     */
28
    public function __construct(BraintreeEntityManagerInterface $braintreeEntityManager)
29
    {
30
        $this->braintreeEntityManager = $braintreeEntityManager;
31
    }
32
33
    /**
34
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
35
     * @param \Generated\Shared\Transfer\SaveOrderTransfer $saveOrderTransfer
36
     *
37
     * @return void
38
     */
39
    public function saveOrderPayment(QuoteTransfer $quoteTransfer, SaveOrderTransfer $saveOrderTransfer)
40
    {
41
        if ($quoteTransfer->getPayment()->getPaymentProvider() === BraintreeConfig::PROVIDER_NAME) {
42
            $paymentEntity = $this->savePaymentForOrder(
43
                $quoteTransfer->getPayment()->getBraintree(),
44
                $saveOrderTransfer->getIdSalesOrder()
45
            );
46
47
            $this->savePaymentForOrderItems(
48
                $saveOrderTransfer->getOrderItems(),
49
                $paymentEntity->getIdPaymentBraintree()
50
            );
51
        }
52
    }
53
54
    /**
55
     * @param \Generated\Shared\Transfer\BraintreePaymentTransfer $paymentTransfer
56
     * @param int $idSalesOrder
57
     *
58
     * @return \Orm\Zed\Braintree\Persistence\SpyPaymentBraintree
59
     */
60
    protected function savePaymentForOrder(BraintreePaymentTransfer $paymentTransfer, $idSalesOrder)
61
    {
62
        $paymentEntity = new SpyPaymentBraintree();
63
        $addressTransfer = $paymentTransfer->getBillingAddress();
64
65
        $formattedStreet = trim(sprintf(
66
            '%s %s %s',
67
            $addressTransfer->getAddress1(),
68
            $addressTransfer->getAddress2(),
69
            $addressTransfer->getAddress3()
70
        ));
71
72
        $paymentEntity->fromArray($addressTransfer->toArray());
73
        $paymentEntity->fromArray($paymentTransfer->toArray());
74
75
        $paymentEntity
76
            ->setStreet($formattedStreet)
77
            ->setCountryIso2Code($addressTransfer->getIso2Code())
78
            ->setFkSalesOrder($idSalesOrder);
79
        $paymentEntity->save();
80
81
        return $paymentEntity;
82
    }
83
84
    /**
85
     * @param \Generated\Shared\Transfer\ItemTransfer[] $orderItemTransfers
86
     * @param int $idPayment
87
     *
88
     * @return void
89
     */
90
    protected function savePaymentForOrderItems($orderItemTransfers, $idPayment)
91
    {
92
        foreach ($orderItemTransfers as $orderItemTransfer) {
93
            $paymentOrderItemEntity = new SpyPaymentBraintreeOrderItem();
94
            $paymentOrderItemEntity
95
                ->setFkPaymentBraintree($idPayment)
96
                ->setFkSalesOrderItem($orderItemTransfer->getIdSalesOrderItem());
97
            $paymentOrderItemEntity->save();
98
        }
99
    }
100
101
    /**
102
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
103
     * @param \Generated\Shared\Transfer\SaveOrderTransfer $saveOrderTransfer
104
     *
105
     * @return void
106
     */
107
    public function updateOrderPayment(QuoteTransfer $quoteTransfer, SaveOrderTransfer $saveOrderTransfer): void
108
    {
109
        $paymentTransfer = $quoteTransfer->getPayment();
110
        if ($paymentTransfer->getPaymentProvider() !== BraintreeConfig::PROVIDER_NAME) {
111
            return;
112
        }
113
114
        $idSalesOrder = $saveOrderTransfer->getIdSalesOrder();
115
        $braintreePaymentTransfer = $paymentTransfer->getBraintree();
116
117
        $this->braintreeEntityManager->updateByIdSalesOrder($idSalesOrder, $braintreePaymentTransfer);
118
    }
119
}
120