Completed
Pull Request — master (#11)
by Jim
02:53
created

AbstractTaskCommand::preProcess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 3
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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);
0 ignored issues
show
Compatibility introduced by
$taskEvent of type object<Jarobe\TaskRunner...ity\TaskEventInterface> is not a sub-type of object<Jarobe\TaskRunnerBundle\Entity\TaskEvent>. It seems like you assume a concrete implementation of the interface Jarobe\TaskRunnerBundle\Entity\TaskEventInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
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