GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#2)
by Stefan
01:35
created

CaptureAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Karser\PayumSaferpay\Action;
4
5
use Karser\PayumSaferpay\Api;
6
use Karser\PayumSaferpay\Constants;
7
use Karser\PayumSaferpay\Request\Api\AssertPaymentPage;
8
use Karser\PayumSaferpay\Request\Api\AuthorizeTransaction;
9
use Karser\PayumSaferpay\Request\Api\CaptureTransaction;
10
use Karser\PayumSaferpay\Request\Api\InitPaymentPage;
11
use Karser\PayumSaferpay\Request\Api\InitTransaction;
12
use League\Uri\Http as HttpUri;
13
use League\Uri\Modifiers\MergeQuery;
14
use Payum\Core\Action\ActionInterface;
15
use Payum\Core\ApiAwareInterface;
16
use Payum\Core\ApiAwareTrait;
17
use Payum\Core\Bridge\Spl\ArrayObject;
18
use Payum\Core\Exception\LogicException;
19
use Payum\Core\GatewayAwareInterface;
20
use Payum\Core\GatewayAwareTrait;
21
use Payum\Core\Request\Capture;
22
use Payum\Core\Exception\RequestNotSupportedException;
23
use Payum\Core\Request\GetHttpRequest;
24
use Payum\Core\Request\GetHumanStatus;
25
use Payum\Core\Security\GenericTokenFactoryAwareInterface;
26
use Payum\Core\Security\GenericTokenFactoryAwareTrait;
27
use Payum\Core\Security\TokenInterface;
28
29
final class CaptureAction implements ActionInterface, ApiAwareInterface, GatewayAwareInterface, GenericTokenFactoryAwareInterface
30
{
31
    use ApiAwareTrait;
32
    use GatewayAwareTrait;
33
    use GenericTokenFactoryAwareTrait;
34 22
35
    /**
36 22
     * @var Api
37
     */
38 16
    protected $api;
39
40 16
    public function __construct()
41
    {
42 16
        $this->apiClass = Api::class;
43 5
    }
44 5
45 11
    /**
46 11
     * @param Capture $request
47 11
     */
48
    public function execute($request)
49
    {
50
        RequestNotSupportedException::assertSupports($this, $request);
51 16
52
        $model = ArrayObject::ensureArrayObject($request->getModel());
53 5
54
        $interface = $model['Interface'] ?? $this->api->getCaptureStrategy();
55 5
56
        switch ($interface) {
57 5
            case Constants::INTERFACE_PAYMENT_PAGE:
58 5
                $this->handlePaymentPageInterface($request);
59 5
                break;
60 5
            case Constants::INTERFACE_TRANSACTION:
61
                $this->handleTransactionInterface($request);
62 5
                break;
63 5
            default:
64 5
                throw new LogicException(sprintf('Unknown interface "%s"', $interface));
65 5
        }
66
    }
67
68 5
    private function handlePaymentPageInterface(Capture $request)
69
    {
70
        $model = ArrayObject::ensureArrayObject($request->getModel());
71 5
72
        if (empty($model['Token'])) {
73 5
            $token = $request->getToken();
74 1
            if (empty($model['ReturnUrls'])) {
75 1
                $model['ReturnUrls'] = $this->composeReturnUrls($token->getTargetUrl());
76
            }
77
            if (empty($model['Notification']['NotifyUrl'])) {
78 4
                $notifyToken = $this->tokenFactory->createNotifyToken($token->getGatewayName(), $token->getDetails());
79 1
                $model['Notification'] = array_merge($model['Notification'] ?? [], [
80 1
                    'NotifyUrl' => $notifyToken->getTargetUrl(),
81
                ]);
82 3
            }
83
            $this->gateway->execute(new InitPaymentPage($model)); //might throw redirect to iframe
84 3
        }
85 3
86 2
        $this->gateway->execute($httpRequest = new GetHttpRequest());
87
88 3 View Code Duplication
        if (isset($httpRequest->query['abort'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
            $model->replace(['Transaction' => array_merge($model['Transaction'], ['Status' => Constants::STATUS_ABORTED])]);
90 11
            return;
91
        }
92 11
93 View Code Duplication
        if (isset($httpRequest->query['fail'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94 11
            $model->replace(['Transaction' => array_merge($model['Transaction'], ['Status' => Constants::STATUS_FAILED])]);
95 11
            return;
96 11
        }
97 11
        $this->gateway->execute(new AssertPaymentPage($model));
98
99 11
        $this->gateway->execute($status = new GetHumanStatus($model));
100
        if ($status->isAuthorized()) {
101
            $this->gateway->execute(new CaptureTransaction($model));
102 11
        }
103
    }
104 11
105 1
    private function handleTransactionInterface(Capture $request)
106 1
    {
107
        $model = ArrayObject::ensureArrayObject($request->getModel());
108
109 10
        if (empty($model['Token'])) {
110 2
            if (empty($model['ReturnUrls'])) {
111 2
                $token = $request->getToken();
112
                $model['ReturnUrls'] = $this->composeReturnUrls($token->getTargetUrl());
113 8
            }
114
            $this->gateway->execute(new InitTransaction($model)); //might throw redirect to iframe
115 8
        }
116 8
117 7
        $this->gateway->execute($httpRequest = new GetHttpRequest());
118
119 8 View Code Duplication
        if (isset($httpRequest->query['abort'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
            $model->replace(['Transaction' => array_merge($model['Transaction'], ['Status' => Constants::STATUS_ABORTED])]);
121 16
            return;
122
        }
123 16
124 16 View Code Duplication
        if (isset($httpRequest->query['fail'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
125 16
            $model->replace(['Transaction' => array_merge($model['Transaction'], ['Status' => Constants::STATUS_FAILED])]);
126
            return;
127 16
        }
128 16
        $this->gateway->execute(new AuthorizeTransaction($model));
129 16
130
        $this->gateway->execute($status = new GetHumanStatus($model));
131 16
        if ($status->isAuthorized()) {
132 16
            $this->gateway->execute(new CaptureTransaction($model));
133 16
        }
134
    }
135
136 16
    private function composeReturnUrls(string $url): array
137 16
    {
138 16
        $successUrl = HttpUri::createFromString($url);
139
        $modifier = new MergeQuery('success=1');
140
        $successUrl = $modifier->process($successUrl);
141
142 32
        $failedUrl = HttpUri::createFromString($url);
143
        $modifier = new MergeQuery('fail=1');
144
        $failedUrl = $modifier->process($failedUrl);
145 32
146 32
        $cancelUri = HttpUri::createFromString($url);
147 32
        $modifier = new MergeQuery('abort=1');
148
        $cancelUri = $modifier->process($cancelUri);
149
150
        return [
151
            'Success' => (string) $successUrl,
152
            'Fail' => (string) $failedUrl,
153
            'Abort' => (string) $cancelUri,
154
        ];
155
    }
156
157
    public function supports($request): bool
158
    {
159
        return
160
            $request instanceof Capture &&
161
            $request->getModel() instanceof \ArrayAccess &&
162
            $request->getToken() instanceof TokenInterface
163
        ;
164
    }
165
}
166