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
|
|
|
/** |
19
|
|
|
* @param InputInterface $input |
20
|
|
|
* @param OutputInterface $output |
21
|
|
|
* @return int|null |
22
|
|
|
*/ |
23
|
|
|
public function execute(InputInterface $input, OutputInterface $output) |
24
|
|
|
{ |
25
|
|
|
$taskBuilder = new TaskBuilder(); |
26
|
|
|
$taskBuilder = $this->buildTaskBuilder($taskBuilder, $input); |
27
|
|
|
|
28
|
|
|
$validationErrors = $this->validateTask($taskBuilder->getTask()); |
29
|
|
|
foreach ($validationErrors as $validationError) { |
30
|
|
|
$taskBuilder->addError($validationError); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
if ($taskBuilder->hasErrors()) { |
34
|
|
|
foreach ($taskBuilder->getErrors() as $error) { |
35
|
|
|
$output->writeln("<error>".$error."</error>"); |
36
|
|
|
} |
37
|
|
|
return null; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
//Create the TaskEvent |
41
|
|
|
$taskEvent = new TaskEvent(); |
42
|
|
|
$taskEvent = $this->taskEventManager->createTaskEvent($taskEvent, $taskBuilder->getTask()); |
43
|
|
|
$taskEvent = $this->process($taskEvent, $input, $output); |
44
|
|
|
return $taskEvent->getCompletedAt() !== null; |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param TaskBuilder $taskBuilder |
49
|
|
|
* @param InputInterface $input |
50
|
|
|
* @return TaskBuilder |
51
|
|
|
*/ |
52
|
|
|
abstract protected function buildTaskBuilder(TaskBuilder $taskBuilder, InputInterface $input); |
53
|
|
|
} |
54
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.