NotifyAction::execute()   B
last analyzed

Complexity

Conditions 8
Paths 8

Size

Total Lines 54
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 29
nc 8
nop 1
dl 0
loc 54
rs 8.2114
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * You can find more information about us on https://bitbag.io and write us
7
 * an email on [email protected].
8
 */
9
10
declare(strict_types=1);
11
12
namespace BitBag\SyliusMolliePlugin\Action;
13
14
use BitBag\SyliusMolliePlugin\Action\Api\BaseApiAwareAction;
15
use BitBag\SyliusMolliePlugin\Action\StateMachine\SetStatusOrderAction;
16
use BitBag\SyliusMolliePlugin\Entity\SubscriptionInterface;
17
use BitBag\SyliusMolliePlugin\Logger\MollieLoggerActionInterface;
18
use BitBag\SyliusMolliePlugin\Repository\SubscriptionRepositoryInterface;
19
use BitBag\SyliusMolliePlugin\Request\StateMachine\StatusRecurringSubscription;
20
use Mollie\Api\Exceptions\ApiException;
21
use Payum\Core\Action\ActionInterface;
22
use Payum\Core\ApiAwareInterface;
23
use Payum\Core\Bridge\Spl\ArrayObject;
24
use Payum\Core\Exception\RequestNotSupportedException;
25
use Payum\Core\GatewayAwareInterface;
26
use Payum\Core\GatewayAwareTrait;
27
use Payum\Core\Reply\HttpResponse;
28
use Payum\Core\Request\GetHttpRequest;
29
use Payum\Core\Request\Notify;
30
use Symfony\Component\HttpFoundation\Response;
31
32
final class NotifyAction extends BaseApiAwareAction implements ActionInterface, ApiAwareInterface, GatewayAwareInterface
33
{
34
    use GatewayAwareTrait;
35
36
    /** @var GetHttpRequest */
37
    private $getHttpRequest;
38
39
    /** @var SubscriptionRepositoryInterface */
40
    private $subscriptionRepository;
41
42
    /** @var SetStatusOrderAction */
43
    private $setStatusOrderAction;
44
45
    /** @var MollieLoggerActionInterface */
46
    private $loggerAction;
47
48
    public function __construct(
49
        GetHttpRequest $getHttpRequest,
50
        SubscriptionRepositoryInterface $subscriptionRepository,
51
        SetStatusOrderAction $setStatusOrderAction,
52
        MollieLoggerActionInterface $loggerAction
53
    ) {
54
        $this->getHttpRequest = $getHttpRequest;
55
        $this->subscriptionRepository = $subscriptionRepository;
56
        $this->setStatusOrderAction = $setStatusOrderAction;
57
        $this->loggerAction = $loggerAction;
58
    }
59
60
    /** @param Notify $request */
61
    public function execute($request): void
62
    {
63
        RequestNotSupportedException::assertSupports($this, $request);
64
65
        $details = ArrayObject::ensureArrayObject($request->getModel());
66
        $this->gateway->execute($this->getHttpRequest);
67
68
        if (true === isset($details['payment_mollie_id'])) {
69
            try {
70
                $payment = $this->mollieApiClient->payments->get($this->getHttpRequest->request['id']);
71
            } catch (\Exception $e) {
72
                $this->loggerAction->addNegativeLog(sprintf('Error with get customer from mollie with: %s', $e->getMessage()));
73
74
                throw new ApiException(sprintf('Error with get customer from mollie with: %s', $e->getMessage()));
75
            }
76
77
            if ($details['metadata']['order_id'] === filter_var($payment->metadata->order_id, \FILTER_VALIDATE_INT)) {
78
                $details['payment_mollie_id'] = $this->getHttpRequest->request['id'];
79
            }
80
81
            $this->loggerAction->addLog(sprintf('Notify payment with id: %s', $payment->id));
82
83
            throw new HttpResponse(Response::$statusTexts[Response::HTTP_OK], Response::HTTP_OK);
84
        }
85
86
        if (true === isset($details['subscription_mollie_id'])) {
87
            /** @var SubscriptionInterface $subscription */
88
            $subscription = $this->subscriptionRepository->findOneByOrderId($details['metadata']['order_id']);
89
90
            $this->gateway->execute(new StatusRecurringSubscription($subscription));
91
92
            $this->loggerAction->addLog(sprintf('Notify subscription with id: %s', $details['subscription_mollie_id']));
93
94
            throw new HttpResponse(Response::$statusTexts[Response::HTTP_OK], Response::HTTP_OK);
95
        }
96
97
        if (true === isset($details['order_mollie_id'])) {
98
            try {
99
                $order = $this->mollieApiClient->orders->get($this->getHttpRequest->request['id']);
100
            } catch (\Exception $e) {
101
                $this->loggerAction->addNegativeLog(sprintf('Error with get order from mollie with: %s', $e->getMessage()));
102
103
                throw new ApiException('Error to get order with ' . $e->getMessage());
104
            }
105
106
            if ($details['metadata']['order_id'] === filter_var($order->metadata->order_id, \FILTER_VALIDATE_INT)) {
107
                $details['order_mollie_id'] = $this->getHttpRequest->request['id'];
108
            }
109
110
            $this->setStatusOrderAction->execute($order);
111
112
            $this->loggerAction->addLog(sprintf('Notify order with id: %s', $order->id));
113
114
            throw new HttpResponse(Response::$statusTexts[Response::HTTP_OK], Response::HTTP_OK);
115
        }
116
    }
117
118
    public function supports($request): bool
119
    {
120
        return
121
            $request instanceof Notify &&
122
            $request->getModel() instanceof \ArrayAccess
123
            ;
124
    }
125
}
126