Passed
Push — master ( f4feef...3322ef )
by payever
10:36
created

ActionDecider::assertArguments()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 2
1
<?php
2
3
/**
4
 * PHP version 5.4 and 7
5
 *
6
 * @category  Action
7
 * @package   Payever\Payments
8
 * @author    payever GmbH <[email protected]>
9
 * @copyright 2017-2021 payever GmbH
10
 * @license   MIT <https://opensource.org/licenses/MIT>
11
 * @link      https://docs.payever.org/shopsystems/api/getting-started
12
 */
13
14
namespace Payever\ExternalIntegration\Payments\Action;
15
16
use Payever\ExternalIntegration\Payments\Base\PaymentsApiClientInterface;
17
use Payever\ExternalIntegration\Payments\Http\MessageEntity\GetTransactionResultEntity;
18
use Payever\ExternalIntegration\Payments\Http\ResponseEntity\GetTransactionResponse;
19
20
/**
21
 * This class represents payment actions used in Payever API
22
 * @SuppressWarnings(PHPMD.MissingImport)
23
 */
24
class ActionDecider implements ActionDeciderInterface
25
{
26
    /** @var PaymentsApiClientInterface */
27
    protected $api;
28
29
    /**
30
     * AbstractActionDecider constructor.
31
     *
32
     * @param PaymentsApiClientInterface $api
33
     */
34
    public function __construct(PaymentsApiClientInterface $api)
35
    {
36
        $this->api = $api;
37
    }
38
39
    /**
40
     * {@inheritDoc}
41
     * @SuppressWarnings(PHPMD.BooleanArgumentFlag)
42
     */
43
    public function isActionAllowed($paymentId, $transactionAction, $throwException = true)
44
    {
45
        $this->assertArguments($transactionAction, $paymentId);
46
        if (in_array($this->getPolyfillAction($transactionAction), $this->getEnabledActions($paymentId), true)) {
47
            return true;
48
        }
49
50
        return $this->assertFound($throwException, $transactionAction, $paymentId);
51
    }
52
53
    /**
54
     * Check if the cancel action for the transaction is allowed
55
     *
56
     * @param string $paymentId
57
     * @param bool $throwException
58
     *
59
     * @return bool
60
     *
61
     * @throws \Exception when $throwException is true and target action is not allowed
62
     * @SuppressWarnings(PHPMD.BooleanArgumentFlag)
63
     */
64
    public function isCancelAllowed($paymentId, $throwException = true)
65
    {
66
        return $this->isActionAllowed($paymentId, static::ACTION_CANCEL, $throwException);
67
    }
68
69
    /**
70
     * Check if the refund action for the transaction is allowed
71
     *
72
     * @param string $paymentId
73
     * @param bool $throwException
74
     *
75
     * @return bool
76
     *
77
     * @throws \Exception when $throwException is true and target action is not allowed
78
     * @SuppressWarnings(PHPMD.BooleanArgumentFlag)
79
     */
80
    public function isRefundAllowed($paymentId, $throwException = true)
81
    {
82
        return $this->isActionAllowed($paymentId, static::ACTION_REFUND, $throwException);
83
    }
84
85
    /**
86
     * Check if the shipping goods action for the transaction is allowed
87
     *
88
     * @param string $paymentId
89
     * @param bool $throwException
90
     *
91
     * @return bool
92
     *
93
     * @throws \Exception when $throwException is true and target action is not allowed
94
     * @SuppressWarnings(PHPMD.BooleanArgumentFlag)
95
     */
96
    public function isShippingAllowed($paymentId, $throwException = true)
97
    {
98
        return $this->isActionAllowed($paymentId, static::ACTION_SHIPPING_GOODS, $throwException);
99
    }
100
101
    /**
102
     * @param string $paymentId
103
     * @return string[]
104
     */
105
    protected function getEnabledActions($paymentId)
106
    {
107
        $response = $this->api->getTransactionRequest($paymentId);
108
        /** @var GetTransactionResponse $getTransactionEntity */
109
        $getTransactionEntity = $response->getResponseEntity();
110
        /** @var GetTransactionResultEntity $getTransactionResult */
111
        $getTransactionResult = $getTransactionEntity->getResult();
112
        $actions = $getTransactionResult->getActions();
113
        $enabledActions = [];
114
        foreach ($actions as $action) {
115
            if (!is_object($action) || !isset($action->action, $action->enabled) || !(bool) $action->enabled) {
116
                continue;
117
            }
118
            $enabledActions[] = $this->getPolyfillAction($action->action);
119
        }
120
121
        return $enabledActions;
122
    }
123
124
    /**
125
     * @param string $action
126
     * @return string
127
     */
128
    protected function getPolyfillAction($action)
129
    {
130
        if ($action === ActionDeciderInterface::ACTION_RETURN) {
0 ignored issues
show
Deprecated Code introduced by
The constant Payever\ExternalIntegrat...nterface::ACTION_RETURN has been deprecated with message: will be removed in future versions, refund should be used instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
131
            $action = ActionDeciderInterface::ACTION_REFUND;
132
        }
133
134
        return $action;
135
    }
136
137
    /**
138
     * @param string|null $transactionAction
139
     * @param string|null $paymentId
140
     * @throws \Exception
141
     */
142
    protected function assertArguments($transactionAction, $paymentId)
143
    {
144
        if (empty($transactionAction) || empty($paymentId)) {
145
            throw new \Exception('Wrong arguments.');
146
        }
147
    }
148
149
    /**
150
     * @param bool $throwException
151
     * @param string $transactionAction
152
     * @param string $paymentId
153
     * @return bool
154
     * @throws ActionNotAllowedException
155
     */
156
    protected function assertFound($throwException, $transactionAction, $paymentId)
157
    {
158
        if ($throwException) {
159
            $message = sprintf(
160
                'Action "%s" is not allowed for payment id "%s"',
161
                $transactionAction,
162
                $paymentId
163
            );
164
165
            throw new ActionNotAllowedException($message);
166
        }
167
168
        return false;
169
    }
170
}
171