Completed
Pull Request — develop (#119)
by
unknown
03:16
created

LogRange::offsetGet()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 1
crap 2
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  = array();
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
        $limit = 15,
69
        $offset = null,
70
        $firstParent = false
71
    ) {
72 6
        $this->repository = $repository;
73 6
        $this->createFromCommand($refStart, $refEnd, $path, $limit, $offset, $firstParent);
74 6
    }
75
76
    /**
77
     * get the commit properties from command
78
     *
79
     * @param string  $refStart    treeish reference
80
     * @param string  $refEnd      treeish reference
81
     * @param string  $path        path
82
     * @param int     $limit       limit
83
     * @param string  $offset      offset
84
     * @param boolean $firstParent first parent
85
     *
86
     * @throws \RuntimeException
87
     * @throws \Symfony\Component\Process\Exception\LogicException
88
     * @throws \Symfony\Component\Process\Exception\InvalidArgumentException
89
     * @throws \Symfony\Component\Process\Exception\RuntimeException
90
     * @see ShowCommand::commitInfo
91
     */
92 6
    private function createFromCommand($refStart, $refEnd, $path, $limit, $offset, $firstParent)
93
    {
94 6
        $command = LogRangeCommand::getInstance($this->getRepository())->showLog($refStart, $refEnd, $path, $limit, $offset, $firstParent);
95 6
        $outputLines = $this->getRepository()->getCaller()->execute(
96 6
            $command,
97 6
            true,
98 6
            $this->getRepository()->getPath()
99 6
        )->getOutputLines(true);
100 6
        $this->parseOutputLines($outputLines);
101 6
    }
102
103 6
    private function parseOutputLines($outputLines)
104
    {
105 6
        $commitLines = null;
106 6
        $this->rangeCommits = array();
107 6
        foreach ($outputLines as $line) {
108 6
            if (preg_match('/^commit (\w+)$/', $line) > 0) {
109 6
                if (null !== $commitLines) {
110 6
                    $this->rangeCommits[] = Commit::createFromOutputLines($this->getRepository(), $commitLines);
111 6
                }
112 6
                $commitLines = array();
113 6
            }
114 6
            $commitLines[] = $line;
115 6
        }
116 6
        if (null !== $commitLines && count($commitLines) > 0) {
117 6
            $this->rangeCommits[] = Commit::createFromOutputLines($this->getRepository(), $commitLines);
118 6
        }
119 6
    }
120
121
    /**
122
     * Get array representation
123
     *
124
     * @return array
125
     */
126 1
    public function toArray()
127
    {
128 1
        return $this->rangeCommits;
129
    }
130
131
    /**
132
     * Get the first commit
133
     *
134
     * @return Commit|null
135
     */
136 1
    public function first()
137
    {
138 1
        return $this->offsetGet(0);
139
    }
140
141
    /**
142
     * Get the last commit
143
     *
144
     * @return Commit|null
145
     */
146 1
    public function last()
147
    {
148 1
        return $this->offsetGet($this->count() - 1);
149
    }
150
151
    /**
152
     * Get commit at index
153
     *
154
     * @param int $index the commit index
155
     *
156
     * @return Commit|null
157
     */
158 1
    public function index($index)
159
    {
160 1
        return $this->offsetGet($index);
161
    }
162
163
    /**
164
     * ArrayAccess interface
165
     *
166
     * @param int $offset offset
167
     *
168
     * @return bool
169
     */
170 1
    public function offsetExists($offset)
171
    {
172 1
        return isset($this->rangeCommits[$offset]);
173
    }
174
175
    /**
176
     * ArrayAccess interface
177
     *
178
     * @param int $offset offset
179
     *
180
     * @return Commit|null
181
     */
182 2
    public function offsetGet($offset)
183
    {
184 2
        return isset($this->rangeCommits[$offset]) ? $this->rangeCommits[$offset] : null;
185
    }
186
187
    /**
188
     * ArrayAccess interface
189
     *
190
     * @param int   $offset offset
191
     * @param mixed $value  value
192
     *
193
     * @throws \RuntimeException
194
     */
195 1
    public function offsetSet($offset, $value)
196
    {
197 1
        throw new \RuntimeException('Can\'t set elements on logs');
198
    }
199
200
    /**
201
     * ArrayAccess interface
202
     *
203
     * @param int $offset offset
204
     *
205
     * @throws \RuntimeException
206
     */
207 1
    public function offsetUnset($offset)
208
    {
209 1
        throw new \RuntimeException('Can\'t unset elements on logs');
210
    }
211
212
    /**
213
     * Countable interface
214
     *
215
     * @return int|void
216
     */
217 1
    public function count()
218
    {
219 1
        return count($this->rangeCommits);
220
    }
221
222
    /**
223
     * Iterator interface
224
     *
225
     * @return Commit|null
226
     */
227 1
    public function current()
228
    {
229 1
        return $this->offsetGet($this->position);
230
    }
231
232
    /**
233
     * Iterator interface
234
     */
235 1
    public function next()
236
    {
237 1
        ++$this->position;
238 1
    }
239
240
    /**
241
     * Iterator interface
242
     *
243
     * @return int
244
     */
245 1
    public function key()
246
    {
247 1
        return $this->position;
248
    }
249
250
    /**
251
     * Iterator interface
252
     *
253
     * @return bool
254
     */
255 1
    public function valid()
256
    {
257 1
        return $this->offsetExists($this->position);
258
    }
259
260
    /**
261
     * Iterator interface
262
     */
263 1
    public function rewind()
264
    {
265 1
        $this->position = 0;
266 1
    }
267
268
    /**
269
     * Repository setter
270
     *
271
     * @param \GitElephant\Repository $repository the repository variable
272
     */
273 1
    public function setRepository($repository)
274
    {
275 1
        $this->repository = $repository;
276 1
    }
277
278
    /**
279
     * Repository getter
280
     *
281
     * @return \GitElephant\Repository
282
     */
283 6
    public function getRepository()
284
    {
285 6
        return $this->repository;
286
    }
287
}
288