Passed
Pull Request — master (#285)
by Fabien
02:18
created

ProcessFactory::getGitChangesCountProcessBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Churn\Process;
6
7
use Churn\File\File;
8
use Closure;
9
use Phar;
10
use Symfony\Component\Process\PhpExecutableFinder;
11
use Symfony\Component\Process\Process;
12
13
class ProcessFactory
14
{
15
16
    /**
17
     * Builder of objects implementing ChangesCountInterface.
18
     *
19
     * @var Closure
20
     */
21
    private $changesCountProcessBuilder;
22
23
    /**
24
     * Builder of objects implementing CyclomaticComplexityInterface.
25
     *
26
     * @var Closure
27
     */
28
    private $cyclomaticComplexityBuilder;
29
30
    /**
31
     * Class constructor.
32
     *
33
     * @param string $vcs Name of the version control system.
34
     * @param string $commitsSince String containing the date of when to look at commits since.
35
     */
36
    public function __construct(string $vcs, string $commitsSince)
37
    {
38
        $this->changesCountProcessBuilder = $this->getChangesCountProcessBuilder($vcs, $commitsSince);
39
        $this->cyclomaticComplexityBuilder = $this->getCyclomaticComplexityProcessBuilder();
40
    }
41
42
    /**
43
     * Creates a process that will count the number of changes for $file.
44
     *
45
     * @param File $file File that the process will execute on.
46
     */
47
    public function createChangesCountProcess(File $file): ChangesCountInterface
48
    {
49
        return ($this->changesCountProcessBuilder)($file);
50
    }
51
52
    /**
53
     * Creates a Cyclomatic Complexity Process that will run on $file.
54
     *
55
     * @param File $file File that the process will execute on.
56
     */
57
    public function createCyclomaticComplexityProcess(File $file): CyclomaticComplexityInterface
58
    {
59
        return ($this->cyclomaticComplexityBuilder)($file);
60
    }
61
62
    /**
63
     * @param string $vcs Name of the version control system.
64
     * @param string $commitsSince String containing the date of when to look at commits since.
65
     * @throws InvalidArgumentException If VCS is not supported.
66
     */
67
    private function getChangesCountProcessBuilder(string $vcs, string $commitsSince): Closure
68
    {
69
        return (new ChangesCountProcessBuilder())->getBuilder($vcs, $commitsSince);
70
    }
71
72
    /**
73
     * Returns a cyclomatic complexity builder.
74
     */
75
    private function getCyclomaticComplexityProcessBuilder(): Closure
76
    {
77
        $phpExecutable = (string)(new PhpExecutableFinder())->find();
78
        $command = \array_merge([$phpExecutable], $this->getAssessorArguments());
79
80
        return static function (File $file) use ($command): CyclomaticComplexityInterface {
81
            $command[] = $file->getFullPath();
82
            $process = new Process($command);
83
84
            return new CyclomaticComplexityProcess($file, $process);
85
        };
86
    }
87
88
    /** @return array<string> */
89
    private function getAssessorArguments(): array
90
    {
91
        if (\is_callable([Phar::class, 'running']) && '' !== Phar::running(false)) {
92
            return [Phar::running(false), 'assess-complexity'];
93
        }
94
95
        return [__DIR__ . '/../../bin/CyclomaticComplexityAssessorRunner'];
96
    }
97
}
98