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.

GuzzleClientTrait   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 87
rs 10
c 0
b 0
f 0
wmc 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getLatestHistoryResponse() 0 3 1
A getLatestHistoryRequest() 0 3 1
A hasHistory() 0 3 1
A getHistoryCount() 0 3 1
A getLatestHistory() 0 3 1
A getHistory() 0 7 2
A getHistoryList() 0 3 1
A __construct() 0 11 1
A append() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Alekseytupichenkov\GuzzleStub\Traits;
6
7
use Alekseytupichenkov\GuzzleStub\Exception\GuzzleStubException;
8
use Alekseytupichenkov\GuzzleStub\Handler\StubHandler;
9
use Alekseytupichenkov\GuzzleStub\Model\Fixture;
10
use GuzzleHttp\HandlerStack;
11
use GuzzleHttp\Middleware;
12
use Psr\Http\Message\RequestInterface;
13
use Psr\Http\Message\ResponseInterface;
14
15
trait GuzzleClientTrait
16
{
17
    /** @var HandlerStack $handlerStack */
18
    private $handlerStack;
19
    private $stubHandler;
20
    private $history = [];
21
22
    public function __construct()
23
    {
24
        call_user_func_array([$this, 'parent::__construct'], func_get_args());
25
26
        $this->handlerStack = $this->getConfig('handler');
27
        $this->handlerStack->push(Middleware::history($this->history));
28
29
        $this->stubHandler = new StubHandler();
30
        $this->handlerStack->setHandler($this->stubHandler);
31
32
        $this->loadFixtures();
33
    }
34
35
    /**
36
     * Append fixture.
37
     *
38
     * @param Fixture $fixture
39
     *
40
     * @return self
41
     */
42
    public function append(Fixture $fixture): self
43
    {
44
        $this->stubHandler->append($fixture);
45
46
        return $this;
47
    }
48
49
    abstract public function getConfig($option = null);
50
51
    abstract public function loadFixtures();
52
53
    public function getHistoryCount(): int
54
    {
55
        return count($this->history);
56
    }
57
58
    public function getHistoryList(): array
59
    {
60
        return $this->history;
61
    }
62
63
    public function hasHistory(int $index): bool
64
    {
65
        return isset($this->history[$index]);
66
    }
67
68
    /**
69
     * @throws GuzzleStubException
70
     */
71
    public function getHistory(int $index): array
72
    {
73
        if (!$this->hasHistory($index)) {
74
            throw GuzzleStubException::cantFoundHistoryWithIndex($index);
75
        }
76
77
        return $this->history[$index];
78
    }
79
80
    /**
81
     * @throws \Exception
82
     */
83
    public function getLatestHistory(): array
84
    {
85
        return $this->getHistory($this->getHistoryCount() - 1);
86
    }
87
88
    /**
89
     * @throws \Exception
90
     */
91
    public function getLatestHistoryRequest(): RequestInterface
92
    {
93
        return $this->getLatestHistory()['request'];
94
    }
95
96
    /**
97
     * @throws \Exception
98
     */
99
    public function getLatestHistoryResponse(): ResponseInterface
100
    {
101
        return $this->getLatestHistory()['response'];
102
    }
103
}
104