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.

StubHandler   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 86
rs 10
c 0
b 0
f 0
wmc 20

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 7 1
A array_diff_recursive() 0 22 6
A isSuitableBody() 0 3 1
A isSuitableHeaders() 0 3 1
A isSuitableString() 0 3 2
A isSuitableMethod() 0 3 1
A getSuitableResponse() 0 17 6
A isSuitableUri() 0 3 1
A append() 0 3 1
1
<?php
2
3
namespace Alekseytupichenkov\GuzzleStub\Handler;
4
5
use Alekseytupichenkov\GuzzleStub\Exception\GuzzleStubException;
6
use Alekseytupichenkov\GuzzleStub\Model\Fixture;
7
use GuzzleHttp\Promise\PromiseInterface;
8
use Psr\Http\Message\RequestInterface;
9
use Psr\Http\Message\ResponseInterface;
10
11
class StubHandler
12
{
13
    private $fixtures = [];
14
15
    public function __invoke(RequestInterface $request, array $options): PromiseInterface
16
    {
17
        $response = \GuzzleHttp\Promise\promise_for($this->getSuitableResponse($request));
18
19
        return $response->then(
20
            function ($value) {
21
                return $value;
22
            }
23
        );
24
    }
25
26
    public function append(Fixture $fixture)
27
    {
28
        $this->fixtures[] = $fixture;
29
    }
30
31
    private function getSuitableResponse(RequestInterface $request): ResponseInterface
32
    {
33
        foreach (array_reverse($this->fixtures) as $fixture) {
34
            /** @var Fixture $fixture */
35
            if (
36
                !$this->isSuitableMethod($fixture->getRequest(), $request) ||
37
                !$this->isSuitableUri($fixture->getRequest(), $request) ||
38
                !$this->isSuitableBody($fixture->getRequest(), $request) ||
39
                !$this->isSuitableHeaders($fixture->getRequest(), $request)
40
            ) {
41
                continue;
42
            }
43
44
            return $fixture->getResponse();
45
        }
46
47
        throw GuzzleStubException::responseNotFound($request);
48
    }
49
50
    private function isSuitableMethod(RequestInterface $expectedRequest, RequestInterface $actualRequest): bool
51
    {
52
        return $this->isSuitableString($expectedRequest->getMethod(), $actualRequest->getMethod());
53
    }
54
55
    private function isSuitableUri(RequestInterface $expectedRequest, RequestInterface $actualRequest): bool
56
    {
57
        return $this->isSuitableString(urldecode($expectedRequest->getUri()->__toString()), urldecode($actualRequest->getUri()->__toString()));
58
    }
59
60
    private function isSuitableBody(RequestInterface $expectedRequest, RequestInterface $actualRequest): bool
61
    {
62
        return $this->isSuitableString($expectedRequest->getBody()->__toString(), $actualRequest->getBody()->__toString());
63
    }
64
65
    private function isSuitableHeaders(RequestInterface $expectedRequest, RequestInterface $actualRequest): bool
66
    {
67
        return empty($this->array_diff_recursive($expectedRequest->getHeaders(), $actualRequest->getHeaders()));
68
    }
69
70
    private function array_diff_recursive($a, $b)
71
    {
72
        $result = [];
73
74
        foreach ($a as $key => $value) {
75
            if (array_key_exists($key, $b)) {
76
                if (is_array($value)) {
77
                    $diff = $this->array_diff_recursive($value, $b[$key]);
78
                    if (count($diff)) {
79
                        $result[$key] = $diff;
80
                    }
81
                } else {
82
                    if (!$this->isSuitableString($value, $b[$key])) {
83
                        $result[$key] = $value;
84
                    }
85
                }
86
            } else {
87
                $result[$key] = $value;
88
            }
89
        }
90
91
        return $result;
92
    }
93
94
    private function isSuitableString(string $expected, string $actual): bool
95
    {
96
        return 0 === strcasecmp($expected, $actual) || @preg_match('/^'.$expected.'$/i', $actual);
97
    }
98
}
99