Log::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 4
cts 4
cp 1
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 6
crap 1
1
<?php
2
3
/**
4
 * GitElephant - An abstraction layer for git written in PHP
5
 * Copyright (C) 2013  Matteo Giachino
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see [http://www.gnu.org/licenses/].
19
 */
20
21
namespace GitElephant\Objects;
22
23
use GitElephant\Command\LogCommand;
24
use GitElephant\Repository;
25
use GitElephant\Utilities;
26
27
/**
28
 * Git log abstraction object
29
 *
30
 * @author Matteo Giachino <[email protected]>
31
 * @author Dhaval Patel <[email protected]>
32
 */
33
class Log implements \ArrayAccess, \Countable, \Iterator
34
{
35
    /**
36
     * @var \GitElephant\Repository
37
     */
38
    private $repository;
39
40
    /**
41
     * the commits related to this log
42
     *
43
     * @var array
44
     */
45
    private $commits = [];
46
47
    /**
48
     * the cursor position
49
     *
50
     * @var int
51
     */
52
    private $position = 0;
53
54
    /**
55
     * static method to generate standalone log
56
     *
57
     * @param \GitElephant\Repository $repository  repo
58
     * @param array                   $outputLines output lines from command.log
59
     *
60
     * @return \GitElephant\Objects\Log
61
     */
62 1
    public static function createFromOutputLines(Repository $repository, array $outputLines): \GitElephant\Objects\Log
63
    {
64 1
        $log = new self($repository);
65 1
        $log->parseOutputLines($outputLines);
66
67 1
        return $log;
68
    }
69
70
    /**
71
     * Class constructor
72
     *
73
     * @param Repository  $repository
74
     * @param string      $ref
75
     * @param string|null $path
76
     * @param int         $limit
77
     * @param int|null    $offset
78
     * @param bool        $firstParent
79
     */
80 19
    public function __construct(
81
        Repository $repository,
82
        $ref = 'HEAD',
83
        $path = null,
84
        int $limit = 15,
85
        int $offset = null,
86
        bool $firstParent = false
87
    ) {
88 19
        $this->repository = $repository;
89 19
        $this->createFromCommand($ref, $path, $limit, $offset, $firstParent);
90 19
    }
91
92
    /**
93
     * get the commit properties from command
94
     *
95
     * @param string  $ref         treeish reference
96
     * @param string  $path        path
97
     * @param int     $limit       limit
98
     * @param int  $offset      offset
99
     * @param boolean $firstParent first parent
100
     *
101
     * @throws \RuntimeException
102
     * @throws \Symfony\Component\Process\Exception\LogicException
103
     * @throws \Symfony\Component\Process\Exception\InvalidArgumentException
104
     * @throws \Symfony\Component\Process\Exception\RuntimeException
105
     * @see ShowCommand::commitInfo
106
     */
107 19
    private function createFromCommand(
108
        $ref,
109
        $path = null,
110
        int $limit = null,
111
        int $offset = null,
112
        bool $firstParent = false
113
    ): void {
114 19
        $command = LogCommand::getInstance($this->getRepository())
115 19
            ->showLog($ref, $path, $limit, $offset, $firstParent);
116
117 19
        $outputLines = $this->getRepository()
118 19
            ->getCaller()
119 19
            ->execute($command)
120 19
            ->getOutputLines(true);
121
122 19
        $this->parseOutputLines($outputLines);
123 19
    }
124
125 19
    private function parseOutputLines(array $outputLines): void
126
    {
127 19
        $this->commits = [];
128 19
        $commits = Utilities::pregSplitFlatArray($outputLines, '/^commit (\w+)$/');
129
130 19
        foreach ($commits as $commitOutputLines) {
131 19
            $this->commits[] = Commit::createFromOutputLines($this->getRepository(), $commitOutputLines);
132
        }
133 19
    }
134
135
    /**
136
     * Get array representation
137
     *
138
     * @return array
139
     */
140 1
    public function toArray(): array
141
    {
142 1
        return $this->commits;
143
    }
144
145
    /**
146
     * Get the first commit
147
     *
148
     * @return Commit|null
149
     */
150
    public function first(): ?\GitElephant\Objects\Commit
151
    {
152
        return $this->offsetGet(0);
153
    }
154
155
    /**
156
     * Get the last commit
157
     *
158
     * @return Commit|null
159
     */
160 3
    public function last(): ?\GitElephant\Objects\Commit
161
    {
162 3
        return $this->offsetGet($this->count() - 1);
163
    }
164
165
    /**
166
     * Get commit at index
167
     *
168
     * @param int $index the commit index
169
     *
170
     * @return Commit|null
171
     */
172 1
    public function index(int $index): ?\GitElephant\Objects\Commit
173
    {
174 1
        return $this->offsetGet($index);
175
    }
176
177
    /**
178
     * ArrayAccess interface
179
     *
180
     * @param int $offset offset
181
     *
182
     * @return bool
183
     */
184
    public function offsetExists($offset): bool
185
    {
186
        return isset($this->commits[$offset]);
187
    }
188
189
    /**
190
     * ArrayAccess interface
191
     *
192
     * @param int $offset offset
193
     *
194
     * @return Commit|null
195
     */
196 14
    public function offsetGet($offset): ?\GitElephant\Objects\Commit
197
    {
198 14
        return isset($this->commits[$offset]) ? $this->commits[$offset] : null;
199
    }
200
201
    /**
202
     * ArrayAccess interface
203
     *
204
     * @param int   $offset offset
205
     * @param mixed $value  value
206
     *
207
     * @return void
208
     * @throws \RuntimeException
209
     */
210
    public function offsetSet($offset, $value)
211
    {
212
        throw new \RuntimeException('Can\'t set elements on logs');
213
    }
214
215
    /**
216
     * ArrayAccess interface
217
     *
218
     * @param int $offset offset
219
     *
220
     * @return void
221
     * @throws \RuntimeException
222
     */
223
    public function offsetUnset($offset)
224
    {
225
        throw new \RuntimeException('Can\'t unset elements on logs');
226
    }
227
228
    /**
229
     * Countable interface
230
     *
231
     * @return int
232
     */
233 8
    public function count(): int
234
    {
235 8
        return count($this->commits);
236
    }
237
238
    /**
239
     * Iterator interface
240
     *
241
     * @return Commit|null
242
     */
243
    public function current(): ?\GitElephant\Objects\Commit
244
    {
245
        return $this->offsetGet($this->position);
246
    }
247
248
    /**
249
     * Iterator interface
250
     */
251
    public function next(): void
252
    {
253
        ++$this->position;
254
    }
255
256
    /**
257
     * Iterator interface
258
     *
259
     * @return int
260
     */
261
    public function key(): int
262
    {
263
        return $this->position;
264
    }
265
266
    /**
267
     * Iterator interface
268
     *
269
     * @return bool
270
     */
271
    public function valid(): bool
272
    {
273
        return $this->offsetExists($this->position);
274
    }
275
276
    /**
277
     * Iterator interface
278
     */
279
    public function rewind(): void
280
    {
281
        $this->position = 0;
282
    }
283
284
    /**
285
     * Repository setter
286
     *
287
     * @param \GitElephant\Repository $repository the repository variable
288
     */
289
    public function setRepository(Repository $repository): void
290
    {
291
        $this->repository = $repository;
292
    }
293
294
    /**
295
     * Repository getter
296
     *
297
     * @return \GitElephant\Repository
298
     */
299 19
    public function getRepository(): \GitElephant\Repository
300
    {
301 19
        return $this->repository;
302
    }
303
}
304