Passed
Push — feature/eco-3047/eco-3049-invo... ( b52d71...c342c3 )
by Aleksey
04:14
created

IsOrderPaidOmsCondition::isOrderPaid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
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 11
rs 10
cc 2
nc 2
nop 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\Heidelpay\Communication\Oms\Condition;
9
10
use Generated\Shared\Transfer\HeidelpayNotificationCollectionTransfer;
1 ignored issue
show
Bug introduced by
The type Generated\Shared\Transfe...ationCollectionTransfer 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\OrderTransfer;
1 ignored issue
show
Bug introduced by
The type Generated\Shared\Transfer\OrderTransfer 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 Orm\Zed\Sales\Persistence\SpySalesOrderItem;
1 ignored issue
show
Bug introduced by
The type Orm\Zed\Sales\Persistence\SpySalesOrderItem 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 SprykerEco\Zed\Heidelpay\Dependency\Facade\HeidelpayToSalesFacadeInterface;
14
use SprykerEco\Zed\Heidelpay\Persistence\HeidelpayRepositoryInterface;
15
16
class IsOrderPaidOmsCondition implements HeidelpayOmsConditionInterface
17
{
18
    protected const PAID_RECEIPT_PAYMENT_CODE = 'IV.RC';
19
20
    /**
21
     * @var \SprykerEco\Zed\Heidelpay\Persistence\HeidelpayRepositoryInterface
22
     */
23
    protected $repository;
24
25
    /**
26
     * @var \SprykerEco\Zed\Heidelpay\Dependency\Facade\HeidelpayToSalesFacadeInterface
27
     */
28
    protected $salesFacade;
29
30
    /**
31
     * @param \SprykerEco\Zed\Heidelpay\Persistence\HeidelpayRepositoryInterface $repository
32
     * @param \SprykerEco\Zed\Heidelpay\Dependency\Facade\HeidelpayToSalesFacadeInterface $salesFacade
33
     */
34
    public function __construct(
35
        HeidelpayRepositoryInterface $repository,
36
        HeidelpayToSalesFacadeInterface $salesFacade
37
    ) {
38
        $this->repository = $repository;
39
        $this->salesFacade = $salesFacade;
40
    }
41
42
    /**
43
     * @param \Orm\Zed\Sales\Persistence\SpySalesOrderItem $orderItem
44
     *
45
     * @return bool
46
     */
47
    public function check(SpySalesOrderItem $orderItem): bool
48
    {
49
        $heidelpayNotificationCollection = $this->repository
50
            ->getPaymentHeidelpayNotificationCollectionByTransactionIdAndPaymentCode(
51
                (string)$orderItem->getFkSalesOrder(),
52
                static::PAID_RECEIPT_PAYMENT_CODE
53
            );
54
55
        if ($heidelpayNotificationCollection->getNotifications()->count() === 0) {
56
            return false;
57
        }
58
59
        $orderTransfer = $this->salesFacade
60
            ->getOrderByIdSalesOrder($orderItem->getFkSalesOrder());
61
62
        return $this->isOrderPaid($heidelpayNotificationCollection, $orderTransfer);
63
    }
64
65
    /**
66
     * @param \Generated\Shared\Transfer\HeidelpayNotificationCollectionTransfer $heidelpayNotificationCollection
67
     * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
68
     *
69
     * @return bool
70
     */
71
    protected function isOrderPaid(
72
        HeidelpayNotificationCollectionTransfer $heidelpayNotificationCollection,
73
        OrderTransfer $orderTransfer
74
    ): bool {
75
        $paidAmount = 0;
76
77
        foreach ($heidelpayNotificationCollection->getNotifications() as $heidelpayNotificationTransfer) {
78
            $paidAmount += $heidelpayNotificationTransfer->getAmount();
79
        }
80
81
        return $paidAmount === $orderTransfer->getTotals()->getPriceToPay();
82
    }
83
}
84