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.

EventDispatchMiddleware   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 81
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A dispatchEvent() 0 44 2
A doDispatch() 0 10 2
1
<?php
2
3
namespace EightPoints\Bundle\GuzzleBundle\Middleware;
4
5
use EightPoints\Bundle\GuzzleBundle\Events\Event;
6
use EightPoints\Bundle\GuzzleBundle\Events\PostTransactionEvent;
7
use Exception;
8
use GuzzleHttp\Exception\RequestException;
9
use Psr\Http\Message\RequestInterface;
10
use Psr\Http\Message\ResponseInterface;
11
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
12
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcherInterface;
13
use EightPoints\Bundle\GuzzleBundle\Events\GuzzleEvents;
14
use EightPoints\Bundle\GuzzleBundle\Events\PreTransactionEvent;
15
16
/**
17
 * Dispatches an Event using the Symfony Event Dispatcher.
18
 * Dispatches a PRE_TRANSACTION event, before the transaction is sent
19
 * Dispatches a POST_TRANSACTION event, when the remote hosts responds.
20
 */
21
class EventDispatchMiddleware
22
{
23
    /** @var \Symfony\Component\EventDispatcher\EventDispatcherInterface */
24
    private $eventDispatcher;
25
26
    /** @var string */
27
    private $serviceName;
28
29
    /**
30
     * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher
31
     * @param string $serviceName
32
     */
33
    public function __construct(EventDispatcherInterface $eventDispatcher, string $serviceName)
34
    {
35
        $this->eventDispatcher = $eventDispatcher;
36
        $this->serviceName = $serviceName;
37
    }
38
39
    /**
40
     * @return \Closure
41
     */
42
    public function dispatchEvent() : \Closure
43
    {
44
        return function (callable $handler) {
45
46
            return function (
47
                RequestInterface $request,
48
                array $options
49
            ) use ($handler) {
50
                // Create the Pre Transaction event.
51
                $preTransactionEvent = new PreTransactionEvent($request, $this->serviceName);
52
53
                // Dispatch it through the symfony Dispatcher.
54
                $this->doDispatch($preTransactionEvent, GuzzleEvents::PRE_TRANSACTION);
55
                $this->doDispatch($preTransactionEvent, GuzzleEvents::preTransactionFor($this->serviceName));
56
57
                // Continue the handler chain.
58
                $promise = $handler($preTransactionEvent->getTransaction(), $options);
59
60
                // Handle the response form the server.
61
                return $promise->then(
62
                    function (ResponseInterface $response) {
63
                        // Create the Post Transaction event.
64
                        $postTransactionEvent = new PostTransactionEvent($response, $this->serviceName);
65
66
                        // Dispatch the event on the symfony event dispatcher.
67
                        $this->doDispatch($postTransactionEvent, GuzzleEvents::POST_TRANSACTION);
68
                        $this->doDispatch($postTransactionEvent, GuzzleEvents::postTransactionFor($this->serviceName));
69
70
                        // Continue down the chain.
71
                        return $postTransactionEvent->getTransaction();
72
                    },
73
                    function (Exception $reason) {
74
                        // Get the response. The response in a RequestException can be null too.
75
                        $response = $reason instanceof RequestException ? $reason->getResponse() : null;
76
77
                        // Create the Post Transaction event.
78
                        $postTransactionEvent = new PostTransactionEvent($response, $this->serviceName);
79
80
                        // Dispatch the event on the symfony event dispatcher.
81
                        $this->doDispatch($postTransactionEvent, GuzzleEvents::POST_TRANSACTION);
82
                        $this->doDispatch($postTransactionEvent, GuzzleEvents::postTransactionFor($this->serviceName));
83
84
                        // Continue down the chain.
85
                        return \GuzzleHttp\Promise\rejection_for($reason);
86
                    }
87
                );
88
            };
89
        };
90
    }
91
92
    private function doDispatch(Event $event, string $name): void
93
    {
94
        if ($this->eventDispatcher instanceof ContractsEventDispatcherInterface) {
0 ignored issues
show
introduced by
$this->eventDispatcher is always a sub-type of Symfony\Contracts\EventD...ventDispatcherInterface.
Loading history...
95
            $this->eventDispatcher->dispatch($event, $name);
0 ignored issues
show
Unused Code introduced by
The call to Symfony\Contracts\EventD...erInterface::dispatch() has too many arguments starting with $name. ( Ignorable by Annotation )

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

95
            $this->eventDispatcher->/** @scrutinizer ignore-call */ 
96
                                    dispatch($event, $name);

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...
96
97
            return;
98
        }
99
100
        // BC compatibility
101
        $this->eventDispatcher->dispatch($name, $event);
102
    }
103
}
104