|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Churn\Process; |
|
4
|
|
|
|
|
5
|
|
|
use Churn\Values\File; |
|
6
|
|
|
use Symfony\Component\Process\Exception\ProcessFailedException; |
|
7
|
|
|
use Symfony\Component\Process\Process; |
|
8
|
|
|
|
|
9
|
|
|
class ChurnProcess |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* The file the process will be executed on. |
|
13
|
|
|
* @var File |
|
14
|
|
|
*/ |
|
15
|
|
|
private $file; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* The type of process. |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
private $type; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* The Symfony Process Component. |
|
25
|
|
|
* @var Process |
|
26
|
|
|
*/ |
|
27
|
|
|
private $process; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* GitCommitCountProcess constructor. |
|
31
|
|
|
* @param File $file The file the process is being executed on. |
|
32
|
|
|
* @param Process $process The process being executed on the file. |
|
33
|
|
|
* @param string $type The type of process. |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(File $file, Process $process, string $type) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->file = $file; |
|
38
|
|
|
$this->process = $process; |
|
39
|
|
|
$this->type = $type; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Start the process. |
|
44
|
|
|
* @return void |
|
45
|
|
|
*/ |
|
46
|
|
|
public function start(): void |
|
47
|
|
|
{ |
|
48
|
|
|
$this->process->start(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Determines if the process was successful. |
|
53
|
|
|
* @return boolean |
|
54
|
|
|
* @throws ProcessFailedException If the process failed. |
|
55
|
|
|
*/ |
|
56
|
|
|
public function isSuccessful(): bool |
|
57
|
|
|
{ |
|
58
|
|
|
$exitCode = $this->process->getExitCode(); |
|
59
|
|
|
if ($exitCode > 0) { |
|
60
|
|
|
throw new ProcessFailedException($this->process); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $exitCode === 0; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Gets the output of the process. |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getOutput(): string |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->process->getOutput(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Gets the file name of the file the process |
|
77
|
|
|
* is being executed on. |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getFilename(): string |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->file->getDisplayPath(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Gets the file the process is being executed on. |
|
87
|
|
|
* @return File |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getFile(): File |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->file; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Gets a unique key used for storing the process in data structures. |
|
96
|
|
|
* @return string |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getKey(): string |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->getType() . $this->file->getFullPath(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Get the type of this process. |
|
105
|
|
|
* @return string |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getType(): string |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->type; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|