Passed
Push — bugfix/eco-2800-paid-notificat... ( 786e71 )
by Aleksey
08:37
created

isCaptureRelatedNotification()   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\CrefoPay\Business\Processor;
9
10
use ArrayObject;
11
use Generated\Shared\Transfer\CrefoPayNotificationTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...PayNotificationTransfer 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\PaymentCrefoPayOrderItemCollectionTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...rItemCollectionTransfer 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\PaymentCrefoPayOrderItemTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...efoPayOrderItemTransfer 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 SprykerEco\Zed\CrefoPay\Business\Mapper\OmsStatus\CrefoPayOmsStatusMapperInterface;
15
use SprykerEco\Zed\CrefoPay\Business\Reader\CrefoPayReaderInterface;
16
use SprykerEco\Zed\CrefoPay\Business\Writer\CrefoPayWriterInterface;
17
18
class CrefoPayNotificationProcessor implements CrefoPayNotificationProcessorInterface
19
{
20
    /**
21
     * @var \SprykerEco\Zed\CrefoPay\Business\Mapper\OmsStatus\CrefoPayOmsStatusMapperInterface
22
     */
23
    protected $statusMapper;
24
25
    /**
26
     * @var \SprykerEco\Zed\CrefoPay\Business\Reader\CrefoPayReaderInterface
27
     */
28
    protected $reader;
29
30
    /**
31
     * @var \SprykerEco\Zed\CrefoPay\Business\Writer\CrefoPayWriterInterface
32
     */
33
    protected $writer;
34
35
    /**
36
     * @param \SprykerEco\Zed\CrefoPay\Business\Mapper\OmsStatus\CrefoPayOmsStatusMapperInterface $statusMapper
37
     * @param \SprykerEco\Zed\CrefoPay\Business\Reader\CrefoPayReaderInterface $reader
38
     * @param \SprykerEco\Zed\CrefoPay\Business\Writer\CrefoPayWriterInterface $writer
39
     */
40
    public function __construct(
41
        CrefoPayOmsStatusMapperInterface $statusMapper,
42
        CrefoPayReaderInterface $reader,
43
        CrefoPayWriterInterface $writer
44
    ) {
45
        $this->statusMapper = $statusMapper;
46
        $this->reader = $reader;
47
        $this->writer = $writer;
48
    }
49
50
    /**
51
     * @param \Generated\Shared\Transfer\CrefoPayNotificationTransfer $notificationTransfer
52
     *
53
     * @return \Generated\Shared\Transfer\CrefoPayNotificationTransfer
54
     */
55
    public function processNotification(CrefoPayNotificationTransfer $notificationTransfer): CrefoPayNotificationTransfer
56
    {
57
        $status = $this->getOmsStatus($notificationTransfer);
58
        $paymentCrefoPayOrderItemsCollection = $this->getCrefoPayOrderItemCollection($notificationTransfer);
59
60
        $paymentCrefoPayOrderItems = array_map(
61
            function (PaymentCrefoPayOrderItemTransfer $paymentCrefoPayOrderItemTransfer) use ($status) {
62
                return $paymentCrefoPayOrderItemTransfer->setStatus($status);
63
            },
64
            $paymentCrefoPayOrderItemsCollection->getCrefoPayOrderItems()->getArrayCopy()
65
        );
66
67
        $paymentCrefoPayOrderItemsCollection->setCrefoPayOrderItems(new ArrayObject($paymentCrefoPayOrderItems));
68
69
        $this->writer->createNotificationEntities(
70
            $notificationTransfer,
71
            $paymentCrefoPayOrderItemsCollection
72
        );
73
74
        return $notificationTransfer;
75
    }
76
77
    /**
78
     * @param \Generated\Shared\Transfer\CrefoPayNotificationTransfer $notificationTransfer
79
     *
80
     * @return string|null
81
     */
82
    protected function getOmsStatus(CrefoPayNotificationTransfer $notificationTransfer): ?string
83
    {
84
        if (!empty($notificationTransfer->getTransactionStatus())) {
85
            $status = $this->statusMapper
86
                ->mapNotificationTransactionStatusToOmsStatus(
87
                    $notificationTransfer->getTransactionStatus()
88
                );
89
        }
90
91
        if (!empty($notificationTransfer->getOrderStatus())) {
92
            $status = $this->statusMapper
93
                ->mapNotificationOrderStatusToOmsStatus(
94
                    $notificationTransfer->getOrderStatus()
95
                );
96
        }
97
98
        return $status ?? null;
99
    }
100
101
    /**
102
     * @param \Generated\Shared\Transfer\CrefoPayNotificationTransfer $notificationTransfer
103
     *
104
     * @return \Generated\Shared\Transfer\PaymentCrefoPayOrderItemCollectionTransfer
105
     */
106
    protected function getCrefoPayOrderItemCollection(CrefoPayNotificationTransfer $notificationTransfer): PaymentCrefoPayOrderItemCollectionTransfer
107
    {
108
        if ($this->isCaptureRelatedNotification($notificationTransfer)) {
109
            return $this->reader
110
                ->findPaymentCrefoPayOrderItemsByCaptureId($notificationTransfer->getCaptureID());
111
        }
112
113
        return $this->reader
114
            ->findPaymentCrefoPayOrderItemsByCrefoPayOrderId($notificationTransfer->getOrderID());
115
    }
116
117
    /**
118
     * @param \Generated\Shared\Transfer\CrefoPayNotificationTransfer $notificationTransfer
119
     *
120
     * @return bool
121
     */
122
    protected function isCaptureRelatedNotification(CrefoPayNotificationTransfer $notificationTransfer): bool
123
    {
124
        return !empty($notificationTransfer->getCaptureID());
125
    }
126
}
127