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 AbstractTaskCommand |
17
|
|
|
{ |
18
|
|
|
/** @var TaskEventManager */ |
19
|
|
|
private $taskEventManager; |
20
|
|
|
|
21
|
|
|
/** @var TaskManager */ |
22
|
|
|
private $taskManager; |
23
|
|
|
|
24
|
|
|
/** @var ValidatorInterface */ |
25
|
|
|
private $validator; |
26
|
|
|
|
27
|
|
View Code Duplication |
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
|
|
|
$taskBuilder = $this->buildTaskBuilder($taskBuilder, $input); |
45
|
|
|
|
46
|
|
|
$validationErrors = $this->validateTask($taskBuilder->getTask()); |
47
|
|
|
foreach ($validationErrors as $validationError) { |
48
|
|
|
$taskBuilder->addError($validationError); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if ($taskBuilder->hasErrors()) { |
52
|
|
|
foreach ($taskBuilder->getErrors() as $error) { |
53
|
|
|
$output->writeln("<error>".$error."</error>"); |
54
|
|
|
} |
55
|
|
|
return null; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
//Create the TaskEvent |
59
|
|
|
$taskEvent = $this->taskEventManager->createTaskEvent($taskBuilder->getTask()); |
|
|
|
|
60
|
|
|
$taskEvent = $this->process($taskEvent, $input, $output); |
|
|
|
|
61
|
|
|
return 1; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param TaskBuilder $taskBuilder |
66
|
|
|
* @param InputInterface $input |
67
|
|
|
* @return TaskBuilder |
68
|
|
|
*/ |
69
|
|
|
abstract protected function buildTaskBuilder(TaskBuilder $taskBuilder, InputInterface $input); |
70
|
|
|
} |
71
|
|
|
|
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.