Passed
Push — master ( ab28ee...e2645a )
by Tobias
02:45
created

Journal::getLimit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Buzz\Middleware\History;
6
7
use Psr\Http\Message\RequestInterface;
8
use Psr\Http\Message\ResponseInterface;
9
10
class Journal implements \Countable, \IteratorAggregate
11
{
12
    private $entries = [];
13
    private $limit = 10;
14
15 3
    public function __construct(int $limit = 10)
16
    {
17 3
        $this->limit = $limit;
18 3
    }
19
20
    /**
21
     * Records an entry in the journal.
22
     *
23
     * @param RequestInterface  $request  The request
24
     * @param ResponseInterface $response The response
25
     * @param float|null        $duration The duration in seconds
26
     */
27 3
    public function record(RequestInterface $request, ResponseInterface $response, float $duration = null): void
28
    {
29 3
        $this->addEntry(new Entry($request, $response, $duration));
30 3
    }
31
32 3
    public function addEntry(Entry $entry): void
33
    {
34 3
        array_push($this->entries, $entry);
35 3
        $this->entries = \array_slice($this->entries, $this->getLimit() * -1);
36 3
        end($this->entries);
37 3
    }
38
39
    /**
40
     * @return Entry[]
41
     */
42
    public function getEntries(): array
43
    {
44
        return $this->entries;
45
    }
46
47 4
    public function getLast(): ?Entry
48
    {
49 4
        $entry = end($this->entries);
50
51 4
        return false === $entry ? null : $entry;
52
    }
53
54 1
    public function getLastRequest(): ?RequestInterface
55
    {
56 1
        $entry = $this->getLast();
57 1
        if (null === $entry) {
58
            return null;
59
        }
60
61 1
        return $entry->getRequest();
62
    }
63
64 1
    public function getLastResponse(): ?ResponseInterface
65
    {
66 1
        $entry = $this->getLast();
67 1
        if (null === $entry) {
68
            return null;
69
        }
70
71 1
        return $entry->getResponse();
72
    }
73
74 1
    public function clear(): void
75
    {
76 1
        $this->entries = [];
77 1
    }
78
79 3
    public function count(): int
80
    {
81 3
        return \count($this->entries);
82
    }
83
84 1
    public function setLimit(int $limit): void
85
    {
86 1
        $this->limit = $limit;
87 1
    }
88
89 3
    public function getLimit(): int
90
    {
91 3
        return $this->limit;
92
    }
93
94 1
    public function getIterator(): \ArrayIterator
95
    {
96 1
        return new \ArrayIterator(array_reverse($this->entries));
97
    }
98
}
99