PayumController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 9
nc 1
nop 9
dl 0
loc 21
rs 9.9666
c 1
b 1
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Eres\SyliusIyzicoPlugin\Controller;
6
7
use Eres\SyliusIyzicoPlugin\Event\PaymentEvent;
8
use FOS\RestBundle\View\View;
9
use Payum\Core\Payum;
10
use Payum\Core\Request\Generic;
11
use Payum\Core\Request\GetStatusInterface;
12
use Payum\Core\Security\HttpRequestVerifierInterface;
13
use Sylius\Bundle\PayumBundle\Factory\GetStatusFactoryInterface;
14
use Sylius\Bundle\PayumBundle\Factory\ResolveNextRouteFactoryInterface;
15
use Sylius\Bundle\ResourceBundle\Controller\RequestConfigurationFactoryInterface;
16
use Sylius\Bundle\ResourceBundle\Controller\ViewHandlerInterface;
17
use Sylius\Component\Core\Model\PaymentInterface;
18
use Sylius\Component\Order\Repository\OrderRepositoryInterface;
19
use Sylius\Component\Resource\Metadata\MetadataInterface;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpFoundation\Response;
22
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
23
use Symfony\Component\Routing\RouterInterface;
24
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
25
26
class PayumController
27
{
28
    /** @var Payum */
29
    private $payum;
30
31
    /** @var OrderRepositoryInterface */
32
    private $orderRepository;
33
34
    /** @var MetadataInterface */
35
    private $orderMetadata;
36
37
    /** @var RequestConfigurationFactoryInterface */
38
    private $requestConfigurationFactory;
39
40
    /** @var ViewHandlerInterface */
41
    private $viewHandler;
42
43
    /** @var RouterInterface */
44
    private $router;
45
46
    /** @var GetStatusFactoryInterface */
47
    private $getStatusRequestFactory;
48
49
    /** @var ResolveNextRouteFactoryInterface */
50
    private $resolveNextRouteRequestFactory;
51
52
    /** @var EventDispatcherInterface */
53
    private $eventDispatcher;
54
55
    public function __construct(
56
        Payum $payum,
57
        OrderRepositoryInterface $orderRepository,
58
        MetadataInterface $orderMetadata,
59
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
60
        ViewHandlerInterface $viewHandler,
61
        RouterInterface $router,
62
        GetStatusFactoryInterface $getStatusFactory,
63
        ResolveNextRouteFactoryInterface $resolveNextRouteFactory,
64
        EventDispatcherInterface $eventDispatcher
65
    )
66
    {
67
        $this->payum = $payum;
68
        $this->orderRepository = $orderRepository;
69
        $this->orderMetadata = $orderMetadata;
70
        $this->requestConfigurationFactory = $requestConfigurationFactory;
71
        $this->viewHandler = $viewHandler;
72
        $this->router = $router;
73
        $this->getStatusRequestFactory = $getStatusFactory;
74
        $this->resolveNextRouteRequestFactory = $resolveNextRouteFactory;
75
        $this->eventDispatcher = $eventDispatcher;
76
    }
77
78
    public function afterCaptureAction(Request $request): Response
79
    {
80
        $configuration = $this->requestConfigurationFactory->create($this->orderMetadata, $request);
81
82
        $token = $this->getHttpRequestVerifier()->verify($request);
83
84
        /** @var Generic|GetStatusInterface $status */
85
        $status = $this->getStatusRequestFactory->createNewWithModel($token);
86
        $this->payum->getGateway($token->getGatewayName())->execute($status);
87
88
        $resolveNextRoute = $this->resolveNextRouteRequestFactory->createNewWithModel($status->getFirstModel());
0 ignored issues
show
Bug introduced by
The method getFirstModel() does not exist on Payum\Core\Request\GetStatusInterface. Did you maybe mean getModel()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
        $resolveNextRoute = $this->resolveNextRouteRequestFactory->createNewWithModel($status->/** @scrutinizer ignore-call */ getFirstModel());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
89
        $this->payum->getGateway($token->getGatewayName())->execute($resolveNextRoute);
90
91
        $this->getHttpRequestVerifier()->invalidate($token);
92
93
        if (PaymentInterface::STATE_NEW !== $status->getValue()) {
94
95
            $model = $status->getModel();
96
97
            /** @var FlashBagInterface $flashBag */
98
            $flashBag = $request->getSession()->getBag('flashes');
99
100
            if (isset($model['iyzico_error_message']) && $model['iyzico_error_message']) {
101
                $flashBagMessage = $model['iyzico_error_message'];
102
            } else {
103
                $flashBagMessage = sprintf('sylius.payment.%s', $status->getValue());
104
            }
105
            $flashBag->add('info', $flashBagMessage);
106
107
        }
108
109
        $paymentEvent = new PaymentEvent();
110
        $paymentEvent->setPayment($status->getFirstModel());
111
        $this->eventDispatcher->dispatch('eres_sylius_iyzico_plugin.payment.post', $paymentEvent);
0 ignored issues
show
Bug introduced by
'eres_sylius_iyzico_plugin.payment.post' of type string is incompatible with the type object expected by parameter $event of Symfony\Contracts\EventD...erInterface::dispatch(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

111
        $this->eventDispatcher->dispatch(/** @scrutinizer ignore-type */ 'eres_sylius_iyzico_plugin.payment.post', $paymentEvent);
Loading history...
Unused Code introduced by
The call to Symfony\Contracts\EventD...erInterface::dispatch() has too many arguments starting with $paymentEvent. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

111
        $this->eventDispatcher->/** @scrutinizer ignore-call */ 
112
                                dispatch('eres_sylius_iyzico_plugin.payment.post', $paymentEvent);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
112
113
        return $this->viewHandler->handle(
114
            $configuration,
115
            View::createRouteRedirect($resolveNextRoute->getRouteName(), $resolveNextRoute->getRouteParameters())
116
        );
117
    }
118
119
    private function getHttpRequestVerifier(): HttpRequestVerifierInterface
120
    {
121
        return $this->payum->getHttpRequestVerifier();
122
    }
123
}
124