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

BraintreeEntityManager   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 41
dl 0
loc 107
rs 10
c 2
b 0
f 0
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A updateIsShipmentOperationValue() 0 10 2
A getSalesOrderItemIds() 0 9 2
A updateByIdSalesOrder() 0 10 1
A addOrderItemsToTransactionLog() 0 27 3
A updateIsShipmentPaidValue() 0 7 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\Braintree\Persistence;
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 Orm\Zed\Braintree\Persistence\SpyPaymentBraintreeTransactionStatusLogToOrderItem;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\Braintree\Persis...ionStatusLogToOrderItem 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 Propel\Runtime\Collection\ObjectCollection;
13
use Spryker\Zed\Kernel\Persistence\AbstractEntityManager;
14
15
/**
16
 * @method \SprykerEco\Zed\Braintree\Persistence\BraintreePersistenceFactory getFactory()
17
 */
18
class BraintreeEntityManager extends AbstractEntityManager implements BraintreeEntityManagerInterface
19
{
20
    /**
21
     * @param int $idPaymentBraintree
22
     * @param bool $isShipmentPaid
23
     *
24
     * @return void
25
     */
26
    public function updateIsShipmentPaidValue(int $idPaymentBraintree, bool $isShipmentPaid): void
27
    {
28
        $paymentBraintreeEntity = $this->getFactory()->createPaymentBraintreeQuery()->findOneByIdPaymentBraintree($idPaymentBraintree);
29
30
        if ($paymentBraintreeEntity) {
31
            $paymentBraintreeEntity->setIsShipmentPaid($isShipmentPaid);
32
            $paymentBraintreeEntity->save();
33
        }
34
    }
35
36
    /**
37
     * @param int $idPaymentBraintree
38
     * @param \ArrayObject|\Generated\Shared\Transfer\ItemTransfer[] $itemTransfers
39
     * @param string $transactionId
40
     *
41
     * @return void
42
     */
43
    public function addOrderItemsToTransactionLog(int $idPaymentBraintree, iterable $itemTransfers, string $transactionId): void
44
    {
45
        $paymentBraintreeTransactionStatusLogEntity = $this->getFactory()
46
            ->createPaymentBraintreeTransactionStatusLogQuery()
47
            ->filterByTransactionId($transactionId)
48
            ->findOneByFkPaymentBraintree($idPaymentBraintree);
49
50
        if ($paymentBraintreeTransactionStatusLogEntity) {
51
            $paymentBraintreeOrderItemEntities = $this->getFactory()
52
                ->createPaymentBraintreeOrderItemQuery()
53
                ->filterByFkSalesOrderItem_In($this->getSalesOrderItemIds($itemTransfers))
54
                ->find();
55
56
            $objectCollection = new ObjectCollection();
57
            $objectCollection->setModel(SpyPaymentBraintreeTransactionStatusLogToOrderItem::class);
58
59
            foreach ($paymentBraintreeOrderItemEntities as $paymentBraintreeOrderItemEntity) {
60
                $paymentBraintreeTransactionOrderItemEntity = new SpyPaymentBraintreeTransactionStatusLogToOrderItem();
61
                $paymentBraintreeTransactionOrderItemEntity->setFkPaymentBraintreeTransactionStatusLog(
62
                    $paymentBraintreeTransactionStatusLogEntity->getIdPaymentBraintreeTransactionStatusLog()
63
                );
64
                $paymentBraintreeTransactionOrderItemEntity->setFkPaymentBraintreeOrderItem($paymentBraintreeOrderItemEntity->getIdPaymentBraintreeOrderItem());
65
66
                $objectCollection->append($paymentBraintreeTransactionOrderItemEntity);
67
            }
68
69
            $objectCollection->save();
70
        }
71
    }
72
73
    /**
74
     * @param int $idPaymentBraintree
75
     * @param string $transactionId
76
     * @param bool $isShipmentOperation
77
     *
78
     * @return void
79
     */
80
    public function updateIsShipmentOperationValue(int $idPaymentBraintree, string $transactionId, bool $isShipmentOperation): void
81
    {
82
        $paymentBraintreeTransactionStatusLogEntity = $this->getFactory()
83
            ->createPaymentBraintreeTransactionStatusLogQuery()
84
            ->filterByTransactionId($transactionId)
85
            ->findOneByFkPaymentBraintree($idPaymentBraintree);
86
87
        if ($paymentBraintreeTransactionStatusLogEntity) {
88
            $paymentBraintreeTransactionStatusLogEntity->setIsShipmentOperation($isShipmentOperation);
89
            $paymentBraintreeTransactionStatusLogEntity->save();
90
        }
91
    }
92
93
    /**
94
     * @param int $idSalesOrder
95
     * @param \Generated\Shared\Transfer\BraintreePaymentTransfer $braintreePaymentTransfer
96
     *
97
     * @return void
98
     */
99
    public function updateByIdSalesOrder(int $idSalesOrder, BraintreePaymentTransfer $braintreePaymentTransfer): void
100
    {
101
        $paymentBraintreeEntity = $this
102
            ->getFactory()
103
            ->createPaymentBraintreeQuery()
104
            ->findOneByFkSalesOrder($idSalesOrder);
105
106
        $paymentBraintreeEntity->fromArray($braintreePaymentTransfer->toArray());
107
        $paymentBraintreeEntity->setFkSalesOrder($idSalesOrder);
108
        $paymentBraintreeEntity->save();
109
    }
110
111
    /**
112
     * @param \ArrayObject|\Generated\Shared\Transfer\ItemTransfer[] $itemTransfers
113
     *
114
     * @return array
115
     */
116
    protected function getSalesOrderItemIds(iterable $itemTransfers): array
117
    {
118
        $salesOrderItemIds = [];
119
120
        foreach ($itemTransfers as $itemTransfer) {
121
            $salesOrderItemIds[] = $itemTransfer->getIdSalesOrderItem();
122
        }
123
124
        return $salesOrderItemIds;
125
    }
126
}
127