1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jarobe\TaskRunnerBundle\Command; |
4
|
|
|
|
5
|
|
|
use Jarobe\TaskRunnerBundle\Entity\TaskEvent; |
6
|
|
|
use Jarobe\TaskRunnerBundle\Manager\TaskEventManager; |
7
|
|
|
use Jarobe\TaskRunnerBundle\Manager\TaskManager; |
8
|
|
|
use Jarobe\TaskRunnerBundle\Model\TaskBuilder; |
9
|
|
|
use Jarobe\TaskRunnerBundle\TaskType\TaskTypeInterface; |
10
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
use Symfony\Component\Validator\ConstraintViolationInterface; |
14
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
15
|
|
|
|
16
|
|
|
abstract class TaskCommand extends ContainerAwareCommand |
17
|
|
|
{ |
18
|
|
|
/** @var TaskEventManager */ |
19
|
|
|
private $taskEventManager; |
20
|
|
|
|
21
|
|
|
/** @var TaskManager */ |
22
|
|
|
private $taskManager; |
23
|
|
|
|
24
|
|
|
/** @var ValidatorInterface */ |
25
|
|
|
private $validator; |
26
|
|
|
|
27
|
|
|
public function initialize(InputInterface $input, OutputInterface $output) |
28
|
|
|
{ |
29
|
|
|
parent::initialize($input, $output); |
30
|
|
|
$container = $this->getContainer(); |
31
|
|
|
$this->taskEventManager = $container->get('jarobe.task_runner.task_event_manager'); |
32
|
|
|
$this->taskManager = $container->get('jarobe.task_runner.task_manager'); |
33
|
|
|
$this->validator = $container->get('validator'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param InputInterface $input |
38
|
|
|
* @param OutputInterface $output |
39
|
|
|
* @return int|null |
40
|
|
|
*/ |
41
|
|
|
public function execute(InputInterface $input, OutputInterface $output) |
42
|
|
|
{ |
43
|
|
|
$taskBuilder = new TaskBuilder(); |
44
|
|
|
|
45
|
|
|
$taskBuilder = $this->buildTaskBuilder($taskBuilder, $input); |
46
|
|
|
|
47
|
|
|
$validationErrors = $this->validateTask($taskBuilder->getTask()); |
48
|
|
|
foreach ($validationErrors as $validationError) { |
49
|
|
|
$taskBuilder->addError($validationError); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if ($taskBuilder->hasErrors()) { |
53
|
|
|
foreach ($taskBuilder->getErrors() as $error) { |
54
|
|
|
$output->writeln("<error>".$error."</error>"); |
55
|
|
|
} |
56
|
|
|
return null; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
//Create the TaskEvent |
60
|
|
|
$taskEvent = $this->taskEventManager->createTaskEvent($taskBuilder->getTask()); |
61
|
|
|
|
62
|
|
|
$this->preProcess($taskEvent, $input, $output); |
63
|
|
|
|
64
|
|
|
$taskEvent = $this->taskManager->process($taskEvent); |
65
|
|
|
|
66
|
|
|
$this->postProcess($taskEvent, $input, $output); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param TaskBuilder $taskBuilder |
71
|
|
|
* @param InputInterface $input |
72
|
|
|
* @return TaskBuilder |
73
|
|
|
*/ |
74
|
|
|
abstract protected function buildTaskBuilder(TaskBuilder $taskBuilder, InputInterface $input); |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Overwrite this function if you want to do something before we process the TaskEvent |
78
|
|
|
* @param TaskEvent $taskEvent |
79
|
|
|
* @param InputInterface $input |
80
|
|
|
* @param OutputInterface $output |
81
|
|
|
*/ |
82
|
|
|
protected function prepareTaskEvent(TaskEvent $taskEvent, InputInterface $input, OutputInterface $output) |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Overwrite this function if you want to do something before we process the TaskEvent |
88
|
|
|
* @param TaskEvent $taskEvent |
89
|
|
|
* @param InputInterface $input |
90
|
|
|
* @param OutputInterface $output |
91
|
|
|
*/ |
92
|
|
|
protected function preProcess(TaskEvent $taskEvent, InputInterface $input, OutputInterface $output) |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Overwrite this function if you want to do something before we process the TaskEvent |
98
|
|
|
* @param TaskEvent $taskEvent |
99
|
|
|
* @param InputInterface $input |
100
|
|
|
* @param OutputInterface $output |
101
|
|
|
*/ |
102
|
|
|
protected function postProcess(TaskEvent $taskEvent, InputInterface $input, OutputInterface $output) |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param TaskTypeInterface $task |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
|
|
protected function validateTask(TaskTypeInterface $task) |
111
|
|
|
{ |
112
|
|
|
$validationErrors = []; |
113
|
|
|
$validationErrorList = $this->validator->validate($task); |
114
|
|
|
/** @var ConstraintViolationInterface $error */ |
115
|
|
|
foreach($validationErrorList as $error){ |
116
|
|
|
$validationErrors[] = sprintf("%s: %s", $error->getPropertyPath(), $error->getMessage()); |
117
|
|
|
} |
118
|
|
|
return $validationErrors; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.