Completed
Push — develop ( 5247fd...3c6b2e )
by
unknown
9s
created

LogRange::createFromCommand()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 25
ccs 14
cts 14
cp 1
rs 8.8571
cc 1
eloc 19
nc 1
nop 6
crap 1
1
<?php
2
3
/**
4
 * This file is part of the GitElephant package.
5
 *
6
 * (c) Matteo Giachino <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @package GitElephant\Objects
12
 *
13
 * Just for fun...
14
 */
15
16
namespace GitElephant\Objects;
17
18
use \GitElephant\Repository;
19
use \GitElephant\Command\LogRangeCommand;
20
21
/**
22
 * Git range log abstraction object
23
 *
24
 * @author Matteo Giachino <[email protected]>
25
 * @author John Cartwright <[email protected]>
26
 * @author Dhaval Patel <[email protected]>
27
 */
28
class LogRange implements \ArrayAccess, \Countable, \Iterator
29
{
30
    /**
31
     * @var \GitElephant\Repository
32
     */
33
    private $repository;
34
35
    /**
36
     * the commits related to this log
37
     *
38
     * @var array
39
     */
40
    private $rangeCommits = [];
41
42
    /**
43
     * the cursor position
44
     *
45
     * @var int
46
     */
47
    private $position = 0;
48
49
    /**
50
     * Class constructor
51
     *
52
     * @param \GitElephant\Repository $repository  repo
53
     * @param string                  $refStart    starting reference (excluded from the range)
54
     * @param string                  $refEnd      ending reference
55
     * @param null                    $path        path
56
     * @param int                     $limit       limit
57
     * @param null                    $offset      offset
58
     * @param boolean                 $firstParent first parent
59
     *
60
     * @throws \RuntimeException
61
     * @throws \Symfony\Component\Process\Exception\RuntimeException
62
     */
63 6
    public function __construct(
64
        Repository $repository,
65
        $refStart,
66
        $refEnd,
67
        $path = null,
68
        int $limit = 15,
69
        int $offset = null,
70
        bool $firstParent = false
71
    )
72
    {
0 ignored issues
show
Coding Style introduced by
The closing parenthesis and the opening brace of a multi-line function declaration must be on the same line
Loading history...
73 6
        $this->repository = $repository;
74 6
        $this->createFromCommand($refStart, $refEnd, $path, $limit, $offset, $firstParent);
75 6
    }
76
77
    /**
78
     * get the commit properties from command
79
     *
80
     * @param string  $refStart    treeish reference
81
     * @param string  $refEnd      treeish reference
82
     * @param string  $path        path
83
     * @param int     $limit       limit
84
     * @param string  $offset      offset
85
     * @param boolean $firstParent first parent
86
     *
87
     * @throws \RuntimeException
88
     * @throws \Symfony\Component\Process\Exception\LogicException
89
     * @throws \Symfony\Component\Process\Exception\InvalidArgumentException
90
     * @throws \Symfony\Component\Process\Exception\RuntimeException
91
     * @see ShowCommand::commitInfo
92
     */
93 6
    private function createFromCommand(
94
        $refStart,
95
        $refEnd,
96
        $path = null,
97
        int $limit = 15,
98
        int $offset = null,
99
        bool $firstParent = false
100
    )
101
    {
0 ignored issues
show
Coding Style introduced by
The closing parenthesis and the opening brace of a multi-line function declaration must be on the same line
Loading history...
102 6
        $command = LogRangeCommand::getInstance($this->getRepository())->showLog(
103 6
            $refStart,
104 6
            $refEnd,
105 6
            $path,
106 6
            $limit,
107 6
            $offset,
108 6
            $firstParent
109
        );
110
111 6
        $outputLines = $this->getRepository()
112 6
            ->getCaller()
113 6
            ->execute($command, true, $this->getRepository()->getPath())
114 6
            ->getOutputLines(true);
115
116 6
        $this->parseOutputLines($outputLines);
117 6
    }
118
119 6
    private function parseOutputLines(array $outputLines)
120
    {
121 6
        $commitLines = null;
122 6
        $this->rangeCommits = [];
123 6
        foreach ($outputLines as $line) {
124 6
            if (preg_match('/^commit (\w+)$/', $line) > 0) {
125 6
                if (null !== $commitLines) {
126 6
                    $this->rangeCommits[] = Commit::createFromOutputLines($this->getRepository(), $commitLines);
127
                }
128 6
                $commitLines = [];
129
            }
130 6
            $commitLines[] = $line;
131
        }
132
133 6
        if (null !== $commitLines && count($commitLines) > 0) {
134 6
            $this->rangeCommits[] = Commit::createFromOutputLines($this->getRepository(), $commitLines);
135
        }
136 6
    }
137
138
    /**
139
     * Get array representation
140
     *
141
     * @return array
142
     */
143 1
    public function toArray()
144
    {
145 1
        return $this->rangeCommits;
146
    }
147
148
    /**
149
     * Get the first commit
150
     *
151
     * @return Commit|null
152
     */
153 1
    public function first()
154
    {
155 1
        return $this->offsetGet(0);
156
    }
157
158
    /**
159
     * Get the last commit
160
     *
161
     * @return Commit|null
162
     */
163 1
    public function last()
164
    {
165 1
        return $this->offsetGet($this->count() - 1);
166
    }
167
168
    /**
169
     * Get commit at index
170
     *
171
     * @param int $index the commit index
172
     *
173
     * @return Commit|null
174
     */
175 1
    public function index(int $index)
176
    {
177 1
        return $this->offsetGet($index);
178
    }
179
180
    /**
181
     * ArrayAccess interface
182
     *
183
     * @param int $offset offset
184
     *
185
     * @return bool
186
     */
187 1
    public function offsetExists($offset)
188
    {
189 1
        return isset($this->rangeCommits[$offset]);
190
    }
191
192
    /**
193
     * ArrayAccess interface
194
     *
195
     * @param int $offset offset
196
     *
197
     * @return Commit|null
198
     */
199 2
    public function offsetGet($offset)
200
    {
201 2
        return isset($this->rangeCommits[$offset]) ? $this->rangeCommits[$offset] : null;
202
    }
203
204
    /**
205
     * ArrayAccess interface
206
     *
207
     * @param int   $offset offset
208
     * @param mixed $value  value
209
     *
210
     * @throws \RuntimeException
211
     */
212 1
    public function offsetSet($offset, $value)
213
    {
214 1
        throw new \RuntimeException('Can\'t set elements on logs');
215
    }
216
217
    /**
218
     * ArrayAccess interface
219
     *
220
     * @param int $offset offset
221
     *
222
     * @throws \RuntimeException
223
     */
224 1
    public function offsetUnset($offset)
225
    {
226 1
        throw new \RuntimeException('Can\'t unset elements on logs');
227
    }
228
229
    /**
230
     * Countable interface
231
     *
232
     * @return int|void
233
     */
234 1
    public function count()
235
    {
236 1
        return count($this->rangeCommits);
237
    }
238
239
    /**
240
     * Iterator interface
241
     *
242
     * @return Commit|null
243
     */
244 1
    public function current()
245
    {
246 1
        return $this->offsetGet($this->position);
247
    }
248
249
    /**
250
     * Iterator interface
251
     */
252 1
    public function next()
253
    {
254 1
        ++$this->position;
255 1
    }
256
257
    /**
258
     * Iterator interface
259
     *
260
     * @return int
261
     */
262 1
    public function key()
263
    {
264 1
        return $this->position;
265
    }
266
267
    /**
268
     * Iterator interface
269
     *
270
     * @return bool
271
     */
272 1
    public function valid()
273
    {
274 1
        return $this->offsetExists($this->position);
275
    }
276
277
    /**
278
     * Iterator interface
279
     */
280 1
    public function rewind()
281
    {
282 1
        $this->position = 0;
283 1
    }
284
285
    /**
286
     * Repository setter
287
     *
288
     * @param \GitElephant\Repository $repository the repository variable
289
     */
290 1
    public function setRepository(Repository $repository)
291
    {
292 1
        $this->repository = $repository;
293 1
    }
294
295
    /**
296
     * Repository getter
297
     *
298
     * @return \GitElephant\Repository
299
     */
300 6
    public function getRepository()
301
    {
302 6
        return $this->repository;
303
    }
304
}
305