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 (#11)
by Cees-Jan
08:30
created

MiddlewareRunner::error()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 17
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 17
loc 17
rs 9.4285
c 0
b 0
f 0
ccs 8
cts 8
cp 1
cc 2
eloc 11
nc 2
nop 1
crap 2
1
<?php declare(strict_types=1);
2
3
namespace ApiClients\Foundation\Middleware;
4
5
use ApiClients\Foundation\Middleware\Annotation\PriorityInterface;
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 1
     */
30
    private $annotationReader;
31 1
32 1
    /**
33 1
     * @var string
34
     */
35
    private $id;
36
37
    /**
38
     * MiddlewareRunner constructor.
39
     * @param array                 $options
40
     * @param MiddlewareInterface[] $middlewares
41 1
     */
42
    public function __construct(array $options, MiddlewareInterface ...$middlewares)
43
    {
44 1
        $this->options = $options;
45 1
        $this->id = bin2hex(random_bytes(32));
46
        $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...
47 1
        $this->annotationReader = new AnnotationReader();
48
    }
49
50
    /**
51
     * @param  RequestInterface            $request
52
     * @return CancellablePromiseInterface
53
     */
54 1 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...
55
        RequestInterface $request
56
    ): CancellablePromiseInterface {
57 1
        $promise = resolve($request);
58
59 1
        $middlewares = $this->middlewares;
60 1
        $middlewares = $this->orderMiddlewares('pre', ...$middlewares);
61
62 1
        foreach ($middlewares as $middleware) {
63 1
            $requestMiddleware = $middleware;
64
            $promise = $promise->then(function (RequestInterface $request) use ($requestMiddleware) {
65
                return $requestMiddleware->pre($request, $this->options, $this->id);
66 1
            });
67
        }
68
69
        return $promise;
70
    }
71
72
    /**
73 1
     * @param  ResponseInterface           $response
74
     * @return CancellablePromiseInterface
75
     */
76 1 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...
77
        ResponseInterface $response
78 1
    ): CancellablePromiseInterface {
79
        $promise = resolve($response);
80 1
81 1
        $middlewares = $this->middlewares;
82
        $middlewares = $this->orderMiddlewares('post', ...$middlewares);
83 1
84 1
        foreach ($middlewares as $middleware) {
85
            $responseMiddleware = $middleware;
86
            $promise = $promise->then(function (ResponseInterface $response) use ($responseMiddleware) {
87 1
                return $responseMiddleware->post($response, $this->options, $this->id);
88
            });
89
        }
90
91
        return $promise;
92
    }
93
94 1
    /**
95
     * @param  Throwable                   $throwable
96
     * @return CancellablePromiseInterface
97
     */
98 1 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...
99
        Throwable $throwable
100 1
    ): CancellablePromiseInterface {
101
        $promise = reject($throwable);
102 1
103 1
        $middlewares = $this->middlewares;
104 1
        $middlewares = $this->orderMiddlewares('error', ...$middlewares);
105 1
106 1
        foreach ($middlewares as $middleware) {
107
            $errorMiddleware = $middleware;
108
            $promise = $promise->then(null, function (Throwable $throwable) use ($errorMiddleware) {
109 1
                return reject($errorMiddleware->error($throwable, $this->options, $this->id));
110
            });
111
        }
112
113
        return $promise;
114
    }
115
116
    /**
117
     * Sort the middlewares by priority.
118
     *
119
     * @param  string                $method
120
     * @param  MiddlewareInterface[] $middlewares
121
     * @return array
122
     */
123
    protected function orderMiddlewares(string $method, MiddlewareInterface ...$middlewares): array
124
    {
125
        usort($middlewares, function (MiddlewareInterface $left, MiddlewareInterface $right) use ($method) {
126
            return $this->getPriority($method, $right) <=> $this->getPriority($method, $left);
127
        });
128
129
        return $middlewares;
130
    }
131
132
    private function getPriority(string $method, MiddlewareInterface $middleware): int
133
    {
134
        $methodReflection = new ReflectionMethod($middleware, $method);
135
        $annotations = $this->annotationReader->getMethodAnnotations($methodReflection);
136
137
        foreach ($annotations as $annotation) {
138
            if (!is_subclass_of($annotation, PriorityInterface::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \ApiClients\Foundation\M...riorityInterface::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
139
                continue;
140
            }
141
142
            return $annotation->priority();
143
        }
144
145
        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...
146
    }
147
}
148