Completed
Push — develop ( 5f7df1...9cb564 )
by Matteo
10s
created

Log::offsetExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

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