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

TaskCommand::validateTask()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
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)
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...
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());
0 ignored issues
show
Bug introduced by
The call to createTaskEvent() misses a required argument $task.

This check looks for function calls that miss required arguments.

Loading history...
Documentation introduced by
$taskBuilder->getTask() is of type object<Jarobe\TaskRunner...Type\TaskTypeInterface>, but the function expects a object<Jarobe\TaskRunner...ity\TaskEventInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
60
        $taskEvent = $this->process($taskEvent, $input, $output);
0 ignored issues
show
Unused Code introduced by
$taskEvent is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

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