Journal::count()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Ivory Http Adapter package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\HttpAdapter\Event\History;
13
14
use Ivory\HttpAdapter\Message\InternalRequestInterface;
15
use Ivory\HttpAdapter\Message\ResponseInterface;
16
17
/**
18
 * @author GeLo <[email protected]>
19
 */
20
class Journal implements JournalInterface
21
{
22
    /**
23
     * @var JournalEntryFactoryInterface
24
     */
25
    private $journalEntryFactory;
26
27
    /**
28
     * @var array
29
     */
30
    private $entries = [];
31
32
    /**
33
     * @var int
34
     */
35
    private $limit = 10;
36
37
    /**
38
     * @param JournalEntryFactoryInterface|null $journalEntryFactory
39
     */
40 108
    public function __construct(JournalEntryFactoryInterface $journalEntryFactory = null)
41
    {
42 108
        $this->setJournalEntryFactory($journalEntryFactory ?: new JournalEntryFactory());
43 108
    }
44
45
    /**
46
     * @return JournalEntryFactoryInterface
47
     */
48 18
    public function getJournalEntryFactory()
49
    {
50 18
        return $this->journalEntryFactory;
51
    }
52
53
    /**
54
     * @param JournalEntryFactoryInterface $journalEntryFactory
55
     */
56 108
    public function setJournalEntryFactory(JournalEntryFactoryInterface $journalEntryFactory)
57
    {
58 108
        $this->journalEntryFactory = $journalEntryFactory;
59 108
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 9
    public function record(InternalRequestInterface $request, ResponseInterface $response)
65
    {
66 9
        $this->addEntry($this->journalEntryFactory->create($request, $response));
67 9
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 36
    public function clear()
73
    {
74 36
        $this->entries = [];
75 36
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80 72
    public function hasEntries()
81
    {
82 72
        return !empty($this->entries);
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88 81
    public function getEntries()
89
    {
90 81
        return $this->entries;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 36
    public function setEntries(array $entries)
97
    {
98 36
        $this->clear();
99 36
        $this->addEntries($entries);
100 36
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 36
    public function addEntries(array $entries)
106
    {
107 36
        foreach ($entries as $entry) {
108 36
            $this->addEntry($entry);
109 28
        }
110 36
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115 9
    public function removeEntries(array $entries)
116
    {
117 9
        foreach ($entries as $entry) {
118 9
            $this->removeEntry($entry);
119 7
        }
120 9
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125 72
    public function hasEntry(JournalEntryInterface $entry)
126
    {
127 72
        return array_search($entry, $this->entries, true) !== false;
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133 72
    public function addEntry(JournalEntryInterface $entry)
134
    {
135 72
        if (!$this->hasEntry($entry)) {
136 72
            $this->entries[] = $entry;
137 72
            $this->entries = array_slice($this->entries, $this->limit * -1);
138 56
        }
139 72
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144 18
    public function removeEntry(JournalEntryInterface $entry)
145
    {
146 18
        if ($this->hasEntry($entry)) {
147 18
            unset($this->entries[array_search($entry, $this->entries, true)]);
148 18
            $this->entries = array_values($this->entries);
149 14
        }
150 18
    }
151
152
    /**
153
     * {@inheritdoc}
154
     */
155 27
    public function getLimit()
156
    {
157 27
        return $this->limit;
158
    }
159
160
    /**
161
     * {@inheritdoc}
162
     */
163 9
    public function setLimit($limit)
164
    {
165 9
        $this->limit = $limit;
166 9
    }
167
168
    /**
169
     * {@inheritdoc}
170
     */
171 72
    public function count()
172
    {
173 72
        return count($this->entries);
174
    }
175
176
    /**
177
     * {@inheritdoc}
178
     */
179 72
    public function getIterator()
180
    {
181 72
        return new \ArrayIterator(array_reverse($this->entries));
182
    }
183
}
184