Passed
Pull Request — master (#299)
by Fabien
02:12
created

getSubversionChangesCountProcessBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 10
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 Churn\Process\ChangesCount\FossilChangesCountProcess;
9
use Churn\Process\ChangesCount\GitChangesCountProcess;
10
use Churn\Process\ChangesCount\MercurialChangesCountProcess;
11
use Churn\Process\ChangesCount\NoVcsChangesCountProcess;
12
use Churn\Process\ChangesCount\SubversionChangesCountProcess;
13
use Closure;
14
use DateTime;
15
use InvalidArgumentException;
16
17
class ChangesCountProcessBuilder
18
{
19
20
    /**
21
     * @param string $vcs Name of the version control system.
22
     * @param string $commitsSince String containing the date of when to look at commits since.
23
     * @throws InvalidArgumentException If VCS is not supported.
24
     */
25
    public function getBuilder(string $vcs, string $commitsSince): Closure
26
    {
27
        switch ($vcs) {
28
            case 'git':
29
                return $this->getGitChangesCountProcessBuilder($commitsSince);
30
            case 'subversion':
31
                return $this->getSubversionChangesCountProcessBuilder($commitsSince);
32
            case 'mercurial':
33
                return $this->getMercurialChangesCountProcessBuilder($commitsSince);
34
            case 'fossil':
35
                return $this->getFossilChangesCountProcessBuilder($commitsSince);
36
            case 'none':
37
                return $this->getNoVcsChangesCountProcessBuilder();
38
            default:
39
                throw new InvalidArgumentException('Unsupported VCS: ' . $vcs);
40
        }
41
    }
42
43
    /**
44
     * @param string $commitsSince String containing the date of when to look at commits since.
45
     */
46
    private function getGitChangesCountProcessBuilder(string $commitsSince): Closure
47
    {
48
        return static function (File $file) use ($commitsSince): ChangesCountInterface {
49
            return new GitChangesCountProcess($file, $commitsSince);
50
        };
51
    }
52
53
    /**
54
     * @param string $commitsSince String containing the date of when to look at commits since.
55
     */
56
    private function getSubversionChangesCountProcessBuilder(string $commitsSince): Closure
57
    {
58
        $dateRange = \sprintf(
59
            '{%s}:{%s}',
60
            \date('Y-m-d', \strtotime($commitsSince)),
61
            (new DateTime('tomorrow'))->format('Y-m-d')
62
        );
63
64
        return static function (File $file) use ($dateRange): ChangesCountInterface {
65
            return new SubversionChangesCountProcess($file, $dateRange);
66
        };
67
    }
68
69
    /**
70
     * @param string $commitsSince String containing the date of when to look at commits since.
71
     */
72
    private function getMercurialChangesCountProcessBuilder(string $commitsSince): Closure
73
    {
74
        $date = \date('Y-m-d', \strtotime($commitsSince));
75
76
        return static function (File $file) use ($date): ChangesCountInterface {
77
            return new MercurialChangesCountProcess($file, $date);
78
        };
79
    }
80
81
    /**
82
     * @param string $commitsSince String containing the date of when to look at commits since.
83
     */
84
    private function getFossilChangesCountProcessBuilder(string $commitsSince): Closure
85
    {
86
        $date = \date('Y-m-d', \strtotime($commitsSince));
87
88
        return static function (File $file) use ($date): ChangesCountInterface {
89
            return new FossilChangesCountProcess($file, $date);
90
        };
91
    }
92
93
    /**
94
     * Returns a builder for NoVcsChangesCountProcess.
95
     */
96
    private function getNoVcsChangesCountProcessBuilder(): Closure
97
    {
98
        return static function (File $file): ChangesCountInterface {
99
            return new NoVcsChangesCountProcess($file);
100
        };
101
    }
102
}
103