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
|
|
|
use Webmozart\Assert\Assert; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @internal |
20
|
|
|
*/ |
21
|
|
|
class ChangesCountProcessBuilder |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @param string $vcs Name of the version control system. |
25
|
|
|
* @param string $commitsSince String containing the date of when to look at commits since. |
26
|
|
|
* @return Closure(File):ChangesCountInterface |
27
|
|
|
* @throws InvalidArgumentException If VCS is not supported. |
28
|
|
|
*/ |
29
|
|
|
public function getBuilder(string $vcs, string $commitsSince): Closure |
30
|
|
|
{ |
31
|
|
|
switch ($vcs) { |
32
|
|
|
case 'git': |
33
|
|
|
return $this->getGitChangesCountProcessBuilder($commitsSince); |
34
|
|
|
case 'subversion': |
35
|
|
|
return $this->getSubversionChangesCountProcessBuilder($commitsSince); |
36
|
|
|
case 'mercurial': |
37
|
|
|
return $this->getMercurialChangesCountProcessBuilder($commitsSince); |
38
|
|
|
case 'fossil': |
39
|
|
|
return $this->getFossilChangesCountProcessBuilder($commitsSince); |
40
|
|
|
case 'none': |
41
|
|
|
return $this->getNoVcsChangesCountProcessBuilder(); |
42
|
|
|
default: |
43
|
|
|
throw new InvalidArgumentException('Unsupported VCS: ' . $vcs); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $commitsSince String containing the date of when to look at commits since. |
49
|
|
|
* @return Closure(File):ChangesCountInterface |
50
|
|
|
*/ |
51
|
|
|
private function getGitChangesCountProcessBuilder(string $commitsSince): Closure |
52
|
|
|
{ |
53
|
|
|
return static function (File $file) use ($commitsSince): ChangesCountInterface { |
54
|
|
|
return new GitChangesCountProcess($file, $commitsSince); |
55
|
|
|
}; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $commitsSince String containing the date of when to look at commits since. |
60
|
|
|
* @return Closure(File):ChangesCountInterface |
61
|
|
|
*/ |
62
|
|
|
private function getSubversionChangesCountProcessBuilder(string $commitsSince): Closure |
63
|
|
|
{ |
64
|
|
|
$dateRange = \sprintf( |
65
|
|
|
'{%s}:{%s}', |
66
|
|
|
\date('Y-m-d', $this->toTime($commitsSince)), |
67
|
|
|
(new DateTime('tomorrow'))->format('Y-m-d') |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
return static function (File $file) use ($dateRange): ChangesCountInterface { |
71
|
|
|
return new SubversionChangesCountProcess($file, $dateRange); |
72
|
|
|
}; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $commitsSince String containing the date of when to look at commits since. |
77
|
|
|
* @return Closure(File):ChangesCountInterface |
78
|
|
|
*/ |
79
|
|
|
private function getMercurialChangesCountProcessBuilder(string $commitsSince): Closure |
80
|
|
|
{ |
81
|
|
|
$date = \date('Y-m-d', $this->toTime($commitsSince)); |
82
|
|
|
|
83
|
|
|
return static function (File $file) use ($date): ChangesCountInterface { |
84
|
|
|
return new MercurialChangesCountProcess($file, $date); |
85
|
|
|
}; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $commitsSince String containing the date of when to look at commits since. |
90
|
|
|
* @return Closure(File):ChangesCountInterface |
91
|
|
|
*/ |
92
|
|
|
private function getFossilChangesCountProcessBuilder(string $commitsSince): Closure |
93
|
|
|
{ |
94
|
|
|
$date = \date('Y-m-d', $this->toTime($commitsSince)); |
95
|
|
|
|
96
|
|
|
return static function (File $file) use ($date): ChangesCountInterface { |
97
|
|
|
return new FossilChangesCountProcess($file, $date); |
98
|
|
|
}; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Returns a builder for NoVcsChangesCountProcess. |
103
|
|
|
* |
104
|
|
|
* @return Closure(File):ChangesCountInterface |
105
|
|
|
*/ |
106
|
|
|
private function getNoVcsChangesCountProcessBuilder(): Closure |
107
|
|
|
{ |
108
|
|
|
return static function (File $file): ChangesCountInterface { |
109
|
|
|
return new NoVcsChangesCountProcess($file); |
110
|
|
|
}; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Convert a string like "3 years ago" to timestamp. |
115
|
|
|
* |
116
|
|
|
* @param string $commitsSince String containing the date of when to look at commits since. |
117
|
|
|
*/ |
118
|
|
|
private function toTime(string $commitsSince): int |
119
|
|
|
{ |
120
|
|
|
$time = \strtotime($commitsSince); |
121
|
|
|
Assert::positiveInteger($time, 'Commits since can not be converted to timestamp'); |
122
|
|
|
|
123
|
|
|
return $time; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|