Passed
Pull Request — master (#47)
by Baptiste
03:48 queued 01:30
created

History   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
dl 0
loc 45
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A addSuccess() 0 3 1
A getIterator() 0 5 1
A reset() 0 3 1
A getLastResponse() 0 11 2
A addFailure() 0 7 2
1
<?php declare(strict_types=1);
2
namespace Behapi\HttpHistory;
3
4
use IteratorAggregate;
5
6
use Psr\Http\Message\RequestInterface;
7
use Psr\Http\Message\ResponseInterface;
8
9
use Http\Client\Common\Plugin\Journal;
10
11
use Http\Client\Exception;
12
use Http\Client\Exception\HttpException;
13
14
use function end;
15
use function reset;
16
use function count;
17
18
final class History implements Journal, IteratorAggregate
19
{
20
    /** @var Tuple[] */
21
    private $tuples = [];
22
23
    /** {@inheritDoc} */
24
    public function addSuccess(RequestInterface $request, ResponseInterface $response)
25
    {
26
        $this->tuples[] = new Tuple($request, $response);
0 ignored issues
show
Bug introduced by
The type Behapi\HttpHistory\Tuple was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
    }
28
29
    /** {@inheritDoc} */
30
    public function addFailure(RequestInterface $request, Exception $exception)
31
    {
32
        $response = $exception instanceof HttpException
33
            ? $exception->getResponse()
34
            : null;
35
36
        $this->tuples[] = new Tuple($request, $response);
37
    }
38
39
    public function getLastResponse(): ?ResponseInterface
40
    {
41
        if (1 > count($this->tuples)) {
42
            return null;
43
        }
44
45
        /** @var Tuple $tuple */
46
        $tuple = end($this->tuples);
47
        reset($this->tuples);
48
49
        return $tuple->getResponse();
50
    }
51
52
    /** @return iterable<Tuple> */
53
    public function getIterator(): iterable
54
    {
55
        yield from $this->tuples;
56
57
        return count($this->tuples);
1 ignored issue
show
Bug Best Practice introduced by
The expression return count($this->tuples) returns the type integer which is incompatible with the type-hinted return iterable.
Loading history...
58
    }
59
60
    public function reset(): void
61
    {
62
        $this->tuples = [];
63
    }
64
}
65