1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jarobe\TaskRunnerBundle\Command; |
4
|
|
|
|
5
|
|
|
use Jarobe\TaskRunnerBundle\Entity\TaskEvent; |
6
|
|
|
use Jarobe\TaskRunnerBundle\Entity\TaskEventInterface; |
7
|
|
|
use Jarobe\TaskRunnerBundle\Manager\TaskEventManager; |
8
|
|
|
use Jarobe\TaskRunnerBundle\Manager\TaskManager; |
9
|
|
|
use Jarobe\TaskRunnerBundle\Model\TaskBuilder; |
10
|
|
|
use Jarobe\TaskRunnerBundle\TaskType\TaskTypeInterface; |
11
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
12
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
14
|
|
|
use Symfony\Component\Validator\ConstraintViolationInterface; |
15
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
16
|
|
|
|
17
|
|
|
abstract class AbstractTaskCommand extends ContainerAwareCommand |
18
|
|
|
{ |
19
|
|
|
/** @var TaskEventManager */ |
20
|
|
|
protected $taskEventManager; |
21
|
|
|
|
22
|
|
|
/** @var TaskManager */ |
23
|
|
|
protected $taskManager; |
24
|
|
|
|
25
|
|
|
/** @var ValidatorInterface */ |
26
|
|
|
protected $validator; |
27
|
|
|
|
28
|
|
View Code Duplication |
public function initialize(InputInterface $input, OutputInterface $output) |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
parent::initialize($input, $output); |
31
|
|
|
$container = $this->getContainer(); |
32
|
|
|
$this->taskEventManager = $container->get('jarobe.task_runner.task_event_manager'); |
33
|
|
|
$this->taskManager = $container->get('jarobe.task_runner.task_manager'); |
34
|
|
|
$this->validator = $container->get('validator'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param TaskEventInterface $taskEvent |
39
|
|
|
* @param InputInterface $input |
40
|
|
|
* @param OutputInterface $output |
41
|
|
|
* @return TaskEvent |
42
|
|
|
*/ |
43
|
|
|
protected function process(TaskEventInterface $taskEvent, InputInterface $input, OutputInterface $output) |
44
|
|
|
{ |
45
|
|
|
$this->preProcess($taskEvent, $input, $output); |
46
|
|
|
|
47
|
|
|
$taskEvent = $this->taskManager->process($taskEvent); |
|
|
|
|
48
|
|
|
|
49
|
|
|
$this->postProcess($taskEvent, $input, $output); |
50
|
|
|
|
51
|
|
|
return $taskEvent; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Overwrite this function if you want to do something before we process the TaskEvent |
56
|
|
|
* @param TaskEventInterface $taskEvent |
57
|
|
|
* @param InputInterface $input |
58
|
|
|
* @param OutputInterface $output |
59
|
|
|
*/ |
60
|
|
|
protected function preProcess(TaskEventInterface $taskEvent, InputInterface $input, OutputInterface $output) |
61
|
|
|
{ |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Overwrite this function if you want to do something before we process the TaskEvent |
66
|
|
|
* @param TaskEventInterface $taskEvent |
67
|
|
|
* @param InputInterface $input |
68
|
|
|
* @param OutputInterface $output |
69
|
|
|
*/ |
70
|
|
|
protected function postProcess(TaskEventInterface $taskEvent, InputInterface $input, OutputInterface $output) |
71
|
|
|
{ |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param TaskTypeInterface $task |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
|
|
protected function validateTask(TaskTypeInterface $task) |
79
|
|
|
{ |
80
|
|
|
$validationErrors = []; |
81
|
|
|
$validationErrorList = $this->validator->validate($task); |
82
|
|
|
/** @var ConstraintViolationInterface $error */ |
83
|
|
|
foreach ($validationErrorList as $error) { |
84
|
|
|
$validationErrors[] = sprintf("%s: %s", $error->getPropertyPath(), $error->getMessage()); |
85
|
|
|
} |
86
|
|
|
return $validationErrors; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.