Jsonized::createCommit()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * This file is part of SebastianFeldmann\Git.
5
 *
6
 * (c) Sebastian Feldmann <[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
12
namespace SebastianFeldmann\Git\Command\Log\Commits;
13
14
use SebastianFeldmann\Cli\Command\OutputFormatter;
15
use SebastianFeldmann\Git\Log\Commit;
16
17
/**
18
 * Class Jsonized
19
 *
20
 * @package SebastianFeldmann\Git
21
 * @author  Sebastian Feldmann <[email protected]>
22
 * @link    https://github.com/sebastianfeldmann/git
23
 * @since   Class available since Release 0.9.0
24
 */
25
class Jsonized implements OutputFormatter
26
{
27
    /**
28
     * Git log format to use.
29
     *
30
     * @var string
31
     */
32
    public const FORMAT = '{"hash": "%h", "names": "%d", "subject": "%s", "date": "%ci", "author": "%an"}';
33
34
    /**
35
     * Format the output.
36
     *
37
     * @param  array $output
38
     * @return iterable
39 1
     * @throws \Exception
40
     */
41 1
    public function format(array $output): iterable
42 1
    {
43 1
        $formatted = [];
44
        foreach ($output as $row) {
45 1
            $formatted[] = $this->createCommit($row);
46
        }
47
        return $formatted;
48
    }
49
50
    /**
51
     * Create a log commit object.
52
     *
53
     * @param  string $row
54
     * @return \SebastianFeldmann\Git\Log\Commit
55 1
     * @throws \Exception
56
     */
57 1
    private function createCommit(string $row): Commit
58 1
    {
59 1
        $std   = json_decode($row);
60
        $date  = new \DateTimeImmutable($std->date);
61 1
        $names = array_map('trim', explode(',', str_replace(['(', ')'], '', $std->names)));
62
63
        return new Commit($std->hash, $names, $std->subject, '', $date, $std->author);
64
    }
65
}
66