Saver   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 69
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A savePaymentForOrderItems() 0 8 2
A savePaymentForOrder() 0 22 1
A saveOrderPayment() 0 13 3
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
17
class Saver implements SaverInterface
18
{
19
    /**
20
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
21
     * @param \Generated\Shared\Transfer\SaveOrderTransfer $saveOrderTransfer
22
     *
23
     * @return void
24
     */
25
    public function saveOrderPayment(QuoteTransfer $quoteTransfer, SaveOrderTransfer $saveOrderTransfer)
26
    {
27
        if ($quoteTransfer->getPayment()->getPaymentProvider() === BraintreeConfig::PROVIDER_NAME
28
            && $quoteTransfer->getPayment()->getBraintreeTransactionResponse()->getIsSuccess()
29
        ) {
30
            $paymentEntity = $this->savePaymentForOrder(
31
                $quoteTransfer->getPayment()->getBraintree(),
32
                $saveOrderTransfer->getIdSalesOrder()
33
            );
34
35
            $this->savePaymentForOrderItems(
36
                $saveOrderTransfer->getOrderItems(),
37
                $paymentEntity->getIdPaymentBraintree()
38
            );
39
        }
40
    }
41
42
    /**
43
     * @param \Generated\Shared\Transfer\BraintreePaymentTransfer $paymentTransfer
44
     * @param int $idSalesOrder
45
     *
46
     * @return \Orm\Zed\Braintree\Persistence\SpyPaymentBraintree
47
     */
48
    protected function savePaymentForOrder(BraintreePaymentTransfer $paymentTransfer, $idSalesOrder)
49
    {
50
        $paymentEntity = new SpyPaymentBraintree();
51
        $addressTransfer = $paymentTransfer->getBillingAddress();
52
53
        $formattedStreet = trim(sprintf(
54
            '%s %s %s',
55
            $addressTransfer->getAddress1(),
56
            $addressTransfer->getAddress2(),
57
            $addressTransfer->getAddress3()
58
        ));
59
60
        $paymentEntity->fromArray($addressTransfer->toArray());
61
        $paymentEntity->fromArray($paymentTransfer->toArray());
62
63
        $paymentEntity
64
            ->setStreet($formattedStreet)
65
            ->setCountryIso2Code($addressTransfer->getIso2Code())
66
            ->setFkSalesOrder($idSalesOrder);
67
        $paymentEntity->save();
68
69
        return $paymentEntity;
70
    }
71
72
    /**
73
     * @param \Generated\Shared\Transfer\ItemTransfer[] $orderItemTransfers
74
     * @param int $idPayment
75
     *
76
     * @return void
77
     */
78
    protected function savePaymentForOrderItems($orderItemTransfers, $idPayment)
79
    {
80
        foreach ($orderItemTransfers as $orderItemTransfer) {
81
            $paymentOrderItemEntity = new SpyPaymentBraintreeOrderItem();
82
            $paymentOrderItemEntity
83
                ->setFkPaymentBraintree($idPayment)
84
                ->setFkSalesOrderItem($orderItemTransfer->getIdSalesOrderItem());
85
            $paymentOrderItemEntity->save();
86
        }
87
    }
88
}
89