Passed
Push — master ( d5a474...bae138 )
by Fabien
02:01
created

ChangesCountProcessBuilder   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 30
c 1
b 0
f 0
dl 0
loc 83
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getMercurialChangesCountProcessBuilder() 0 6 1
A getBuilder() 0 15 6
A getNoVcsChangesCountProcessBuilder() 0 4 1
A getFossilChangesCountProcessBuilder() 0 6 1
A getGitChangesCountProcessBuilder() 0 4 1
A getSubversionChangesCountProcessBuilder() 0 10 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