GitHistoryMarkerFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 27
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A newHistoryMarker() 0 3 1
A newCurrentHistoryMarker() 0 12 3
1
<?php
2
3
/**
4
 * Static Analysis Results Baseliner (sarb).
5
 *
6
 * (c) Dave Liddament
7
 *
8
 * For the full copyright and licence information please view the LICENSE file distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace DaveLiddament\StaticAnalysisResultsBaseliner\Plugins\GitDiffHistoryAnalyser;
14
15
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\ProjectRoot;
16
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\HistoryAnalyser\HistoryMarker;
17
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\HistoryAnalyser\HistoryMarkerFactory;
18
use DaveLiddament\StaticAnalysisResultsBaseliner\Plugins\GitDiffHistoryAnalyser\internal\GitWrapper;
19
20
final class GitHistoryMarkerFactory implements HistoryMarkerFactory
21
{
22
    /**
23
     * GitHistoryMarkerFactory constructor.
24
     */
25
    public function __construct(
26
        private GitWrapper $gitCliWrapper,
27
    ) {
28
    }
29
30
    public function newHistoryMarker(string $historyMarkerAsString): HistoryMarker
31
    {
32
        return new GitCommit($historyMarkerAsString);
33
    }
34
35
    public function newCurrentHistoryMarker(ProjectRoot $projectRoot, bool $forceBaselineCreation): HistoryMarker
36
    {
37
        // Create gitHistoryMarker first.
38
        // This will correctly deal with issues when $projectRoot is not pointing to a valid git repo.
39
        $gitHistoryMarker = $this->gitCliWrapper->getCurrentSha($projectRoot);
40
41
        // Return it if we are forcing baseline creation or the git repo is clean (i.e. no unchanged/new files)
42
        if ($forceBaselineCreation || $this->gitCliWrapper->isClean($projectRoot)) {
43
            return $gitHistoryMarker;
44
        }
45
46
        throw new GitNotCleanException();
47
    }
48
}
49