Completed
Pull Request — master (#166)
by
unknown
112:06 queued 110:39
created

Log::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

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
nc 1
nop 1
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 string  $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
            if (is_string($commitOutputLines)) {
132
                $this->commits[] = Commit::createFromOutputLines($this->getRepository(), [$commitOutputLines]);
133 19
            } elseif (is_iterable($commitOutputLines)) {
134 19
                $this->commits[] = Commit::createFromOutputLines($this->getRepository(), $commitOutputLines);
135
            }
136
        }
137 19
    }
138
139
    /**
140
     * Get array representation
141
     *
142
     * @return array
143
     */
144 1
    public function toArray(): array
145
    {
146 1
        return $this->commits;
147
    }
148
149
    /**
150
     * Get the first commit
151
     *
152
     * @return Commit|null
153
     */
154
    public function first(): ?\GitElephant\Objects\Commit
155
    {
156
        return $this->offsetGet(0);
157
    }
158
159
    /**
160
     * Get the last commit
161
     *
162
     * @return Commit|null
163
     */
164 3
    public function last(): ?\GitElephant\Objects\Commit
165
    {
166 3
        return $this->offsetGet($this->count() - 1);
167
    }
168
169
    /**
170
     * Get commit at index
171
     *
172
     * @param int $index the commit index
173
     *
174
     * @return Commit|null
175
     */
176 1
    public function index(int $index): ?\GitElephant\Objects\Commit
177
    {
178 1
        return $this->offsetGet($index);
179
    }
180
181
    /**
182
     * ArrayAccess interface
183
     *
184
     * @param int $offset offset
185
     *
186
     * @return bool
187
     */
188
    public function offsetExists($offset): bool
189
    {
190
        return isset($this->commits[$offset]);
191
    }
192
193
    /**
194
     * ArrayAccess interface
195
     *
196
     * @param int $offset offset
197
     *
198
     * @return Commit|null
199
     */
200 14
    public function offsetGet($offset): ?\GitElephant\Objects\Commit
201
    {
202 14
        return isset($this->commits[$offset]) ? $this->commits[$offset] : null;
203
    }
204
205
    /**
206
     * ArrayAccess interface
207
     *
208
     * @param int   $offset offset
209
     * @param mixed $value  value
210
     *
211
     * @throws \RuntimeException
212
     */
213
    public function offsetSet($offset, $value)
214
    {
215
        throw new \RuntimeException('Can\'t set elements on logs');
216
    }
217
218
    /**
219
     * ArrayAccess interface
220
     *
221
     * @param int $offset offset
222
     *
223
     * @throws \RuntimeException
224
     */
225
    public function offsetUnset($offset)
226
    {
227
        throw new \RuntimeException('Can\'t unset elements on logs');
228
    }
229
230
    /**
231
     * Countable interface
232
     *
233
     * @return int
234
     */
235 8
    public function count(): int
236
    {
237 8
        return count($this->commits);
238
    }
239
240
    /**
241
     * Iterator interface
242
     *
243
     * @return Commit|null
244
     */
245
    public function current(): ?\GitElephant\Objects\Commit
246
    {
247
        return $this->offsetGet($this->position);
248
    }
249
250
    /**
251
     * Iterator interface
252
     */
253
    public function next(): void
254
    {
255
        ++$this->position;
256
    }
257
258
    /**
259
     * Iterator interface
260
     *
261
     * @return int
262
     */
263
    public function key(): int
264
    {
265
        return $this->position;
266
    }
267
268
    /**
269
     * Iterator interface
270
     *
271
     * @return bool
272
     */
273
    public function valid(): bool
274
    {
275
        return $this->offsetExists($this->position);
276
    }
277
278
    /**
279
     * Iterator interface
280
     */
281
    public function rewind(): void
282
    {
283
        $this->position = 0;
284
    }
285
286
    /**
287
     * Repository setter
288
     *
289
     * @param \GitElephant\Repository $repository the repository variable
290
     */
291
    public function setRepository(Repository $repository): void
292
    {
293
        $this->repository = $repository;
294
    }
295
296
    /**
297
     * Repository getter
298
     *
299
     * @return \GitElephant\Repository
300
     */
301 19
    public function getRepository(): \GitElephant\Repository
302
    {
303 19
        return $this->repository;
304
    }
305
}
306