Passed
Push — feature/eco-574/eco-2266-check... ( efd21d )
by Aleksey
08:13
created

IsCaptureCompletedPlugin   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 51
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A check() 0 3 1
A getFullCaptureTransactionLogEntry() 0 5 1
A isTransactionSuccessful() 0 3 1
A isCaptureTransactionSuccessful() 0 8 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\Afterpay\Communication\Plugin\Checkout\Oms\Condition;
9
10
use Orm\Zed\Afterpay\Persistence\SpyPaymentAfterpayTransactionLog;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\Afterpay\Persist...tAfterpayTransactionLog 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\Sales\Persistence\SpySalesOrderItem;
0 ignored issues
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...
12
use Spryker\Zed\Kernel\Communication\AbstractPlugin;
13
use Spryker\Zed\Oms\Dependency\Plugin\Condition\ConditionInterface;
14
use SprykerEco\Shared\Afterpay\AfterpayConfig;
15
use SprykerEco\Shared\Afterpay\AfterpayConstants;
16
17
/**
18
 * @method \SprykerEco\Zed\Afterpay\Business\AfterpayFacadeInterface getFacade()
19
 * @method \SprykerEco\Zed\Afterpay\Persistence\AfterpayQueryContainer getQueryContainer()
20
 * @method \SprykerEco\Zed\Afterpay\Communication\AfterpayCommunicationFactory getFactory()
21
 * @method \SprykerEco\Zed\Afterpay\AfterpayConfig getConfig()
22
 */
23
class IsCaptureCompletedPlugin extends AbstractPlugin implements ConditionInterface
24
{
25
    public const CAPTURE_TRANSACTION_ACCEPTED = AfterpayConfig::API_TRANSACTION_OUTCOME_ACCEPTED;
26
27
    /**
28
     * @api
29
     *
30
     * @param \Orm\Zed\Sales\Persistence\SpySalesOrderItem $orderItem
31
     *
32
     * @return bool
33
     */
34
    public function check(SpySalesOrderItem $orderItem): bool
35
    {
36
        return $this->isCaptureTransactionSuccessful($orderItem->getFkSalesOrder());
37
    }
38
39
    /**
40
     * @param int $idSalesOrder
41
     *
42
     * @return bool
43
     */
44
    protected function isCaptureTransactionSuccessful(int $idSalesOrder): bool
45
    {
46
        $captureTransactionLog = $this->getFullCaptureTransactionLogEntry($idSalesOrder);
47
        if ($captureTransactionLog === null) {
48
            return false;
49
        }
50
51
        return $this->isTransactionSuccessful($captureTransactionLog);
52
    }
53
54
    /**
55
     * @param int $idSalesOrder
56
     *
57
     * @return \Orm\Zed\Afterpay\Persistence\SpyPaymentAfterpayTransactionLog|null
58
     */
59
    protected function getFullCaptureTransactionLogEntry(int $idSalesOrder): ?SpyPaymentAfterpayTransactionLog
60
    {
61
        $transactionLogQuery = $this->getQueryContainer()->queryCaptureTransactionLog($idSalesOrder);
62
63
        return $transactionLogQuery->findOne();
64
    }
65
66
    /**
67
     * @param \Orm\Zed\Afterpay\Persistence\SpyPaymentAfterpayTransactionLog $captureTransactionLog
68
     *
69
     * @return bool
70
     */
71
    protected function isTransactionSuccessful(SpyPaymentAfterpayTransactionLog $captureTransactionLog): bool
72
    {
73
        return $captureTransactionLog->getOutcome() === static::CAPTURE_TRANSACTION_ACCEPTED;
74
    }
75
}
76