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 (#7)
by Cees-Jan
02:31
created

MiddlewareRunner::getPriority()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
ccs 0
cts 7
cp 0
rs 9.4285
cc 3
eloc 7
nc 2
nop 2
crap 12
1
<?php declare(strict_types=1);
2
3
namespace ApiClients\Foundation\Middleware;
4
5
use ApiClients\Foundation\Middleware\Annotation\Priority as PriorityAnnotation;
6
use Doctrine\Common\Annotations\AnnotationReader;
7
use Psr\Http\Message\RequestInterface;
8
use Psr\Http\Message\ResponseInterface;
9
use React\Promise\CancellablePromiseInterface;
10
use ReflectionMethod;
11
use Throwable;
12
use function React\Promise\reject;
13
use function React\Promise\resolve;
14
15
final class MiddlewareRunner
16
{
17
    /**
18
     * @var array
19
     */
20
    private $options;
21
22
    /**
23
     * @var MiddlewareInterface[]
24
     */
25
    private $middlewares;
26
27
    /**
28
     * @var AnnotationReader
29
     */
30
    private $annotationReader;
31
32
    /**
33
     * @var string
34
     */
35
    private $id;
36
37
    /**
38
     * MiddlewareRunner constructor.
39
     * @param array $options
40
     * @param MiddlewareInterface[] $middlewares
41
     */
42
    public function __construct(array $options, MiddlewareInterface ...$middlewares)
43
    {
44
        $this->options = $options;
45
        $this->middlewares = $this->orderMiddlewares(...$middlewares);
46
        $this->id = bin2hex(random_bytes(32));
47
        $this->middlewares = $middlewares;
0 ignored issues
show
Documentation Bug introduced by
It seems like $middlewares of type array<integer,array<inte...\MiddlewareInterface>>> is incompatible with the declared type array<integer,object<Api...e\MiddlewareInterface>> of property $middlewares.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
48
        $this->annotationReader = new AnnotationReader();
49
    }
50
51
    /**
52
     * Sort the middlewares by priority
53
     *
54
     * @param string $method
55
     * @param MiddlewareInterface[] $middlewares
56
     * @return array
57
     */
58
    protected function orderMiddlewares(string $method, MiddlewareInterface ...$middlewares): array
59
    {
60
        usort($middlewares, function (MiddlewareInterface $left, MiddlewareInterface $right) use ($method) {
61
            return $this->getPriority($method, $right) <=> $this->getPriority($method, $left);
62
        });
63
64
        return $middlewares;
65
    }
66
67
    private function getPriority(string $method, MiddlewareInterface $middleware): int
68
    {
69
        $methodReflection = new ReflectionMethod($middleware, $method);
70
        /** @var PriorityAnnotation $annotation */
71
        $annotation = $this->annotationReader->getMethodAnnotation($methodReflection, PriorityAnnotation::class);
72
73
        if ($annotation !== null &&
74
            get_class($annotation) === PriorityAnnotation::class
75
        ) {
76
            return $annotation->priority();
77
        }
78
79
        return $middleware->priority();
0 ignored issues
show
Deprecated Code introduced by
The method ApiClients\Foundation\Mi...reInterface::priority() has been deprecated with message: Use annotations for more fine grained control

This method 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 method will be removed from the class and what other method or class to use instead.

Loading history...
80
    }
81
82
    /**
83
     * @param RequestInterface $request
84
     * @return CancellablePromiseInterface
85
     */
86 View Code Duplication
    public function pre(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
87
        RequestInterface $request
88
    ): CancellablePromiseInterface {
89
        $promise = resolve($request);
90
91
        $middlewares = $this->middlewares;
92
        $middlewares = $this->orderMiddlewares('pre', ...$middlewares);
93
94
        foreach ($middlewares as $middleware) {
95
            $requestMiddleware = $middleware;
96
            $promise = $promise->then(function (RequestInterface $request) use ($requestMiddleware) {
97
                return $requestMiddleware->pre($request, $this->options, $this->id);
98
            });
99
        }
100
101
        return $promise;
102
    }
103
104
    /**
105
     * @param ResponseInterface $response
106
     * @return CancellablePromiseInterface
107
     */
108 View Code Duplication
    public function post(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
109
        ResponseInterface $response
110
    ): CancellablePromiseInterface {
111
        $promise = resolve($response);
112
113
        $middlewares = $this->middlewares;
114
        $middlewares = $this->orderMiddlewares('post', ...$middlewares);
115
116
        foreach ($middlewares as $middleware) {
117
            $responseMiddleware = $middleware;
118
            $promise = $promise->then(function (ResponseInterface $response) use ($responseMiddleware) {
119
                return $responseMiddleware->post($response, $this->options, $this->id);
120
            });
121
        }
122
123
        return $promise;
124
    }
125
126
    /**
127
     * @param Throwable $throwable
128
     * @return CancellablePromiseInterface
129
     */
130 View Code Duplication
    public function error(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
131
        Throwable $throwable
132
    ): CancellablePromiseInterface {
133
        $promise = reject($throwable);
134
135
        $middlewares = $this->middlewares;
136
        $middlewares = $this->orderMiddlewares('error', ...$middlewares);
137
138
        foreach ($middlewares as $middleware) {
139
            $errorMiddleware = $middleware;
140
            $promise = $promise->then(null, function (Throwable $throwable) use ($errorMiddleware) {
141
                return reject($errorMiddleware->error($throwable, $this->options, $this->id));
142
            });
143
        }
144
145
        return $promise;
146
    }
147
}
148