Passed
Pull Request — master (#280)
by Fabien
02:19
created

getCyclomaticComplexityProcessBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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