Completed
Pull Request — master (#96)
by
unknown
35:02
created

createCyclomaticComplexityProcess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php declare(strict_types = 1);
2
3
namespace Churn\Factories;
4
5
use Churn\Processes\ChurnProcess;
6
use Churn\Values\Config;
7
use Churn\Values\File;
8
use Symfony\Component\Process\Process;
9
use Symfony\Component\Process\ProcessBuilder;
10
use Symfony\Component\Process\ProcessUtils;
11
12
class ProcessFactory
13
{
14
    /**
15
     * The config values.
16
     * @var Config
17
     */
18
    private $config;
19
20
    /**
21
     * ProcessFactory constructor.
22
     * @param Config $config Configuration Settings.
23
     */
24
    public function __construct(Config $config)
25
    {
26
        $this->config = $config;
27
    }
28
29
    /**
30
     * Creates a VCS Commit Process that will run on $file.
31
     * @param File $file File that the process will execute on.
32
     * @return ChurnProcess
33
     */
34
    public function createVcsCommitProcess(File $file): ChurnProcess
35
    {
36
        $process = new Process($this->getVscCommand($file) . ' | sort | uniq -c | sort -nr');
37
38
        return new ChurnProcess($file, $process, 'VcsCommitProcess');
39
    }
40
41
    /**
42
     * Creates a Cyclomatic Complexity Process that will run on $file.
43
     * @param File $file File that the process will execute on.
44
     * @return ChurnProcess
45
     */
46
    public function createCyclomaticComplexityProcess(File $file): ChurnProcess
47
    {
48
        $rootFolder = __DIR__ . '/../../';
49
50
        $process = new Process(
51
            "php {$rootFolder}CyclomaticComplexityAssessorRunner {$file->getFullPath()}"
52
        );
53
54
        return new ChurnProcess($file, $process, 'CyclomaticComplexityProcess');
55
    }
56
57
    private function getVscCommand(File $file): string
58
    {
59
        $path = $file->getFullPath();
60
        while ($dir = dirname($path)) {
61
            if ($dir == $path) {
62
                break;
63
            }
64
65
            if (is_dir($dir . '/.git')) {
66
                return 'git log --since="' . $this->config->getCommitsSince() . '"  --name-only --pretty=format: ' . $file->getFullPath();
67
            } elseif (is_dir($dir . '/.hg')) {
68
                $since = date('Y-m-d', strtotime($this->config->getCommitsSince()));
69
                return 'hg log ' . $file->getFullPath() . ' --date "' . $since . ' to now" --template "' . $file->getDisplayPath() . '\n"';
70
            }
71
72
            $path = $dir;
73
        }
74
75
        throw new \InvalidArgumentException($file->getFullPath() . ' is not located in a known VCS');
76
    }
77
}
78