Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
18 / 18
CRAP
100.00% covered (success)
100.00%
1 / 1
Commit
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
18 / 18
22
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 getId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addFile
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getMessage
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setMessage
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFiles
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 removeFile
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getAuthor
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setAuthor
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getBranch
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setBranch
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getTag
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setTag
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDate
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setDate
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getRepository
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setRepository
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addChanges
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2/**
3 * Jingga
4 *
5 * PHP Version 8.1
6 *
7 * @package   phpOMS\Utils\Git
8 * @copyright Dennis Eichhorn
9 * @license   OMS License 2.0
10 * @version   1.0.0
11 * @link      https://jingga.app
12 */
13declare(strict_types=1);
14
15namespace phpOMS\Utils\Git;
16
17/**
18 * Gray encoding class
19 *
20 * @package phpOMS\Utils\Git
21 * @license OMS License 2.0
22 * @link    https://jingga.app
23 * @since   1.0.0
24 */
25class Commit
26{
27    /**
28     * Hash.
29     *
30     * @var string
31     * @since 1.0.0
32     */
33    public string $id = '';
34
35    /**
36     * Author.
37     *
38     * @var Author
39     * @since 1.0.0
40     */
41    private Author $author;
42
43    /**
44     * Branch.
45     *
46     * @var Branch
47     * @since 1.0.0
48     */
49    private Branch $branch;
50
51    /**
52     * Tag.
53     *
54     * @var Tag
55     * @since 1.0.0
56     */
57    private Tag $tag;
58
59    /**
60     * Commit date.
61     *
62     * @var null|\DateTime
63     * @since 1.0.0
64     */
65    private ?\DateTime $date = null;
66
67    /**
68     * Repository.
69     *
70     * @var Repository
71     * @since 1.0.0
72     */
73    private Repository $repository;
74
75    /**
76     * Commit message.
77     *
78     * @var string
79     * @since 1.0.0
80     */
81    private string $message = '';
82
83    /**
84     * Files.
85     *
86     * @var array<string, array<int, array{old:string, new:string}>>
87     * @since 1.0.0
88     */
89    private array $files = [];
90
91    /**
92     * Constructor
93     *
94     * @param string $id Commit hash
95     *
96     * @since 1.0.0
97     */
98    public function __construct(string $id = '')
99    {
100        $this->id         = $id;
101        $this->author     = new Author();
102        $this->branch     = new Branch();
103        $this->tag        = new Tag();
104        $this->repository = new Repository();
105    }
106
107    /**
108     * Get commit id.
109     *
110     * @return string
111     *
112     * @since 1.0.0
113     */
114    public function getId() : string
115    {
116        return $this->id;
117    }
118
119    /**
120     * Add file to commit.
121     *
122     * @param string $path File path
123     *
124     * @return bool
125     *
126     * @since 1.0.0
127     */
128    public function addFile(string $path) : bool
129    {
130        if (!isset($this->files[$path])) {
131            $this->files[$path] = [];
132
133            return true;
134        }
135
136        return false;
137    }
138
139    /**
140     * Get commit message.
141     *
142     * @return string
143     *
144     * @since 1.0.0
145     */
146    public function getMessage() : string
147    {
148        return $this->message;
149    }
150
151    /**
152     * Set commit message.
153     *
154     * @param string $message Commit message
155     *
156     * @return void
157     *
158     * @since 1.0.0
159     */
160    public function setMessage(string $message) : void
161    {
162        $this->message = $message;
163    }
164
165    /**
166     * Get files of this commit.
167     *
168     * @return array<string, array<int, array{old:string, new:string}>>
169     *
170     * @since 1.0.0
171     */
172    public function getFiles() : array
173    {
174        return $this->files;
175    }
176
177    /**
178     * Get files of this commit.
179     *
180     * @param string $path File path
181     *
182     * @return bool
183     *
184     * @since 1.0.0
185     */
186    public function removeFile(string $path) : bool
187    {
188        if (isset($this->files[$path])) {
189            unset($this->files[$path]);
190
191            return true;
192        }
193
194        return false;
195    }
196
197    /**
198     * Get commit author.
199     *
200     * @return Author
201     *
202     * @since 1.0.0
203     */
204    public function getAuthor() : Author
205    {
206        return $this->author;
207    }
208
209    /**
210     * Set commit author.
211     *
212     * @param Author $author Commit author
213     *
214     * @return void
215     *
216     * @since 1.0.0
217     */
218    public function setAuthor(Author $author) : void
219    {
220        $this->author = $author;
221    }
222
223    /**
224     * Get commit branch.
225     *
226     * @return Branch
227     *
228     * @since 1.0.0
229     */
230    public function getBranch() : Branch
231    {
232        return $this->branch;
233    }
234
235    /**
236     * Set commit branch.
237     *
238     * @param Branch $branch Commit branch
239     *
240     * @return void
241     *
242     * @since 1.0.0
243     */
244    public function setBranch(Branch $branch) : void
245    {
246        $this->branch = $branch;
247    }
248
249    /**
250     * Get commit tag.
251     *
252     * @return Tag
253     *
254     * @since 1.0.0
255     */
256    public function getTag() : Tag
257    {
258        return $this->tag;
259    }
260
261    /**
262     * Set commit tag.
263     *
264     * @param Tag $tag Commit tag
265     *
266     * @return void
267     *
268     * @since 1.0.0
269     */
270    public function setTag(Tag $tag) : void
271    {
272        $this->tag = $tag;
273    }
274
275    /**
276     * Get commit date.
277     *
278     * @return \DateTime
279     *
280     * @since 1.0.0
281     */
282    public function getDate() : \DateTime
283    {
284        return $this->date ?? new \DateTime('now');
285    }
286
287    /**
288     * Set commit date.
289     *
290     * @param \DateTime $date Commit date
291     *
292     * @return void
293     *
294     * @since 1.0.0
295     */
296    public function setDate(\DateTime $date) : void
297    {
298        $this->date = $date;
299    }
300
301    /**
302     * Get commit repository.
303     *
304     * @return Repository
305     *
306     * @since 1.0.0
307     */
308    public function getRepository() : Repository
309    {
310        return $this->repository;
311    }
312
313    /**
314     * Set commit repository.
315     *
316     * @param Repository $repository Commit repository
317     *
318     * @return void
319     *
320     * @since 1.0.0
321     */
322    public function setRepository(Repository $repository) : void
323    {
324        $this->repository = $repository;
325    }
326
327    /**
328     * Add change.
329     *
330     * @param string $path File path
331     * @param int    $line Line number
332     * @param string $old  Old line
333     * @param string $new  New line
334     *
335     * @return void
336     *
337     * @throws \Exception
338     *
339     * @since 1.0.0
340     */
341    public function addChanges(string $path, int $line, string $old, string $new) : void
342    {
343        if (!isset($this->files[$path])) {
344            $this->files[$path] = [];
345        }
346
347        if (!isset($this->files[$path][$line])) {
348            $this->files[$path][$line] = ['old' => $old, 'new' => $new];
349        } else {
350            throw new \Exception();
351        }
352    }
353}