Passed
Pull Request — master (#299)
by Fabien
01:46
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 static function (File $file): ChangesCountInterface {
38
                    return new NoVcsChangesCountProcess($file);
39
                };
40
            default:
41
                throw new InvalidArgumentException('Unsupported VCS: ' . $vcs);
42
        }
43
    }
44
45
    /**
46
     * @param string $commitsSince String containing the date of when to look at commits since.
47
     */
48
    private function getGitChangesCountProcessBuilder(string $commitsSince): Closure
49
    {
50
        return static function (File $file) use ($commitsSince): ChangesCountInterface {
51
            return new GitChangesCountProcess($file, $commitsSince);
52
        };
53
    }
54
55
    /**
56
     * @param string $commitsSince String containing the date of when to look at commits since.
57
     */
58
    private function getSubversionChangesCountProcessBuilder(string $commitsSince): Closure
59
    {
60
        $dateRange = \sprintf(
61
            '{%s}:{%s}',
62
            \date('Y-m-d', \strtotime($commitsSince)),
63
            (new DateTime('tomorrow'))->format('Y-m-d')
64
        );
65
66
        return static function (File $file) use ($dateRange): ChangesCountInterface {
67
            return new SubversionChangesCountProcess($file, $dateRange);
68
        };
69
    }
70
71
    /**
72
     * @param string $commitsSince String containing the date of when to look at commits since.
73
     */
74
    private function getMercurialChangesCountProcessBuilder(string $commitsSince): Closure
75
    {
76
        $date = \date('Y-m-d', \strtotime($commitsSince));
77
78
        return static function (File $file) use ($date): ChangesCountInterface {
79
            return new MercurialChangesCountProcess($file, $date);
80
        };
81
    }
82
83
    /**
84
     * @param string $commitsSince String containing the date of when to look at commits since.
85
     */
86
    private function getFossilChangesCountProcessBuilder(string $commitsSince): Closure
87
    {
88
        $date = \date('Y-m-d', \strtotime($commitsSince));
89
90
        return static function (File $file) use ($date): ChangesCountInterface {
91
            return new FossilChangesCountProcess($file, $date);
92
        };
93
    }
94
}
95