IsAuthorizationCompletedPlugin   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 59
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A isTransactionSuccessful() 0 3 1
A getAuthorizeTransactionLogEntry() 0 5 1
A check() 0 3 1
A isAuthorizationTransactionSuccessful() 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\AfterPay\Communication\Plugin\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
16
/**
17
 * @method \SprykerEco\Zed\AfterPay\Business\AfterPayFacadeInterface getFacade()
18
 * @method \SprykerEco\Zed\AfterPay\Persistence\AfterPayQueryContainerInterface getQueryContainer()
19
 * @method \SprykerEco\Zed\AfterPay\Communication\AfterPayCommunicationFactory getFactory()
20
 * @method \SprykerEco\Zed\AfterPay\AfterPayConfig getConfig()
21
 */
22
class IsAuthorizationCompletedPlugin extends AbstractPlugin implements ConditionInterface
23
{
24
    public const AUTHORIZE_TRANSACTION_ACCEPTED = AfterPayConfig::API_TRANSACTION_OUTCOME_ACCEPTED;
25
26
    /**
27
     * {@inheritDoc}
28
     * - Checks if the `authorization` operation was successfully accepted for the order.
29
     *
30
     * @api
31
     *
32
     * @param \Orm\Zed\Sales\Persistence\SpySalesOrderItem $orderItem
33
     *
34
     * @return bool
35
     */
36
    public function check(SpySalesOrderItem $orderItem): bool
37
    {
38
        return $this->isAuthorizationTransactionSuccessful($orderItem->getFkSalesOrder());
39
    }
40
41
    /**
42
     * @param int $idSalesOrder
43
     *
44
     * @return bool
45
     */
46
    protected function isAuthorizationTransactionSuccessful(int $idSalesOrder): bool
47
    {
48
        $order = $this->getQueryContainer()->querySalesOrder($idSalesOrder)->findOne();
49
        if ($order === null) {
50
            return false;
51
        }
52
53
        $authorizeTransactionLog = $this->getAuthorizeTransactionLogEntry($order->getOrderReference());
54
        if ($authorizeTransactionLog === null) {
55
            return false;
56
        }
57
58
        return $this->isTransactionSuccessful($authorizeTransactionLog);
59
    }
60
61
    /**
62
     * @param string $orderReference
63
     *
64
     * @return \Orm\Zed\AfterPay\Persistence\SpyPaymentAfterPayTransactionLog|null
65
     */
66
    protected function getAuthorizeTransactionLogEntry(string $orderReference): ?SpyPaymentAfterPayTransactionLog
67
    {
68
        $transactionLogQuery = $this->getQueryContainer()->queryAuthorizeTransactionLog($orderReference);
69
70
        return $transactionLogQuery->findOne();
71
    }
72
73
    /**
74
     * @param \Orm\Zed\AfterPay\Persistence\SpyPaymentAfterPayTransactionLog $authorizeTransactionLog
75
     *
76
     * @return bool
77
     */
78
    protected function isTransactionSuccessful(SpyPaymentAfterPayTransactionLog $authorizeTransactionLog): bool
79
    {
80
        return $authorizeTransactionLog->getOutcome() === static::AUTHORIZE_TRANSACTION_ACCEPTED;
81
    }
82
}
83