|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package: chapi |
|
4
|
|
|
* |
|
5
|
|
|
* @author: msiebeneicher |
|
6
|
|
|
* @since: 2015-09-20 |
|
7
|
|
|
* |
|
8
|
|
|
* @link: http:// |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Chapi\Commands; |
|
12
|
|
|
|
|
13
|
|
|
use Chapi\Commands\AbstractCommand; |
|
14
|
|
|
use Chapi\Component\Command\JobUtils; |
|
15
|
|
|
use Chapi\Entity\Chronos\ChronosJobEntity; |
|
16
|
|
|
use Chapi\Service\JobRepository\JobRepositoryInterface; |
|
17
|
|
|
use Chapi\Service\JobValidator\JobValidatorServiceInterface; |
|
18
|
|
|
|
|
19
|
|
|
class ValidationCommand extends AbstractCommand |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var array[] |
|
24
|
|
|
*/ |
|
25
|
|
|
private $invalidJobs = []; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Configures the current command. |
|
29
|
|
|
*/ |
|
30
|
3 |
|
protected function configure() |
|
31
|
|
|
{ |
|
32
|
3 |
|
$this->setName('validate') |
|
33
|
3 |
|
->setDescription('Validate local jobs') |
|
34
|
|
|
; |
|
35
|
|
|
|
|
36
|
3 |
|
JobUtils::configureJobNamesArgument($this, 'Jobs to validate'); |
|
37
|
3 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @inheritDoc |
|
41
|
|
|
*/ |
|
42
|
3 |
|
protected function process() |
|
43
|
|
|
{ |
|
44
|
3 |
|
$jobNames = JobUtils::getJobNames($this->input, $this); |
|
45
|
3 |
|
$jobsToValidate = (JobUtils::isWildcard($jobNames)) |
|
46
|
2 |
|
? $this->getLocalJobs() |
|
47
|
3 |
|
: $jobNames |
|
48
|
|
|
; |
|
49
|
|
|
|
|
50
|
3 |
|
if ($this->hasInvalidJobs($jobsToValidate)) { |
|
51
|
1 |
|
$this->output->writeln("<comment>Found invalid jobs:</comment>\n"); |
|
52
|
|
|
|
|
53
|
1 |
|
foreach ($this->getInvalidJobsByJobNames($jobsToValidate) as $jobName => $invalidProperties) { |
|
54
|
1 |
|
$this->printInvalidJobProperties($jobName, $invalidProperties); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
1 |
|
return 1; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
//else |
|
61
|
2 |
|
$this->output->writeln('<info>All checked jobs look valid</info>'); |
|
62
|
2 |
|
return 0; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param string[] $jobs |
|
67
|
|
|
* @return bool |
|
68
|
|
|
*/ |
|
69
|
3 |
|
private function hasInvalidJobs(array $jobs) |
|
70
|
|
|
{ |
|
71
|
3 |
|
$invalidJobs = $this->getInvalidJobsByJobNames($jobs); |
|
72
|
3 |
|
return (count($invalidJobs) > 0); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param array $jobs |
|
77
|
|
|
* @return array |
|
78
|
|
|
*/ |
|
79
|
3 |
|
private function getInvalidJobsByJobNames(array $jobs) |
|
80
|
|
|
{ |
|
81
|
3 |
|
$key = md5(implode('.', $jobs)); |
|
82
|
|
|
|
|
83
|
3 |
|
if (isset($this->invalidJobs[$key])) { |
|
84
|
1 |
|
return $this->invalidJobs[$key]; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
3 |
|
$invalidJobs = []; |
|
88
|
|
|
|
|
89
|
|
|
/** @var JobValidatorServiceInterface $jobEntityValidationService */ |
|
90
|
3 |
|
$jobEntityValidationService = $this->getContainer()->get(JobValidatorServiceInterface::DIC_NAME); |
|
91
|
|
|
|
|
92
|
|
|
/** @var JobRepositoryInterface $jobRepositoryLocal */ |
|
93
|
3 |
|
$jobRepositoryLocal = $this->getContainer()->get(JobRepositoryInterface::DIC_NAME_FILESYSTEM_CHRONOS); |
|
94
|
|
|
|
|
95
|
3 |
|
foreach ($jobs as $jobName) { |
|
96
|
3 |
|
$jobEntity = $jobRepositoryLocal->getJob($jobName); |
|
97
|
|
|
|
|
98
|
3 |
|
if (false === $jobEntityValidationService->isEntityValid($jobEntity)) { |
|
99
|
3 |
|
$invalidJobs[$jobName] = $jobEntityValidationService->getInvalidProperties($jobEntity); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
3 |
|
return $this->invalidJobs[$key] = $invalidJobs; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param string $jobName |
|
108
|
|
|
* @param string[] $invalidProperties |
|
109
|
|
|
*/ |
|
110
|
1 |
|
private function printInvalidJobProperties($jobName, array $invalidProperties) |
|
111
|
|
|
{ |
|
112
|
1 |
|
$formatJobName = "\t<fg=red>%s:</>"; |
|
113
|
1 |
|
$formatErrorMessage = "\t\t<fg=red>%s</>"; |
|
114
|
|
|
|
|
115
|
1 |
|
$this->output->writeln(sprintf($formatJobName, $jobName)); |
|
116
|
1 |
|
foreach ($invalidProperties as $errorMessage) { |
|
117
|
1 |
|
$this->output->writeln(sprintf($formatErrorMessage, $errorMessage)); |
|
118
|
|
|
} |
|
119
|
1 |
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @return string[] |
|
123
|
|
|
*/ |
|
124
|
2 |
|
private function getLocalJobs() |
|
125
|
|
|
{ |
|
126
|
2 |
|
$jobNames = []; |
|
127
|
|
|
|
|
128
|
|
|
/** @var JobRepositoryInterface $jobRepositoryLocal */ |
|
129
|
2 |
|
$jobRepositoryLocal = $this->getContainer()->get(JobRepositoryInterface::DIC_NAME_FILESYSTEM_CHRONOS); |
|
130
|
|
|
|
|
131
|
|
|
/** @var ChronosJobEntity $jobEntity */ |
|
132
|
2 |
|
foreach ($jobRepositoryLocal->getJobs() as $jobEntity) { |
|
133
|
2 |
|
$jobNames[] = $jobEntity->name; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
2 |
|
return $jobNames; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|