Passed
Push — master ( 8e4ec1...86ac2d )
by Martin
02:35
created

TaskCreateCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 8
dl 0
loc 76
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A configure() 0 11 2
A execute() 0 17 3
1
<?php
2
3
namespace Todo\Cli\CliBundle\Command;
4
5
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6
use Symfony\Component\Console\Exception\InvalidArgumentException;
7
use Symfony\Component\Console\Exception\LogicException;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Todo\Application\Task\Command;
13
use Todo\Application\Task\Exception\TaskCannotBeSavedException;
14
use Todo\Domain\Exception\TaskNameIsAlreadyExistedException;
15
use Todo\Domain\Exception\TaskNameIsEmptyException;
16
17
/**
18
 * Class TaskCreateCommand
19
 *
20
 * @category None
21
 * @package  Todo\Cli\CliBundle\Command
22
 * @author   Martin Pham <[email protected]>
23
 * @license  None http://
24
 * @link     None
25
 */
26
class TaskCreateCommand extends ContainerAwareCommand
27
{
28
    /**
29
     * TaskCommand
30
     *
31
     * @var Command
32
     */
33
    protected $taskCommand;
34
35
    /**
36
     * TaskListCommand constructor
37
     *
38
     * @param Command $taskCommand Task Command
39
     *
40
     * @throws LogicException
41
     */
42
    public function __construct(Command $taskCommand)
43
    {
44
        $this->taskCommand = $taskCommand;
45
46
        try {
47
            parent::__construct();
48
        } catch (LogicException $e) {
49
            throw $e;
50
        }
51
    }
52
53
    /**
54
     * Configure
55
     *
56
     * @return void
57
     * @throws InvalidArgumentException
58
     */
59
    protected function configure()
60
    {
61
        try {
62
            $this
63
                ->setName('task:create')
64
                ->setDescription('...')
65
                ->addArgument('name', InputArgument::REQUIRED, 'Task name');
66
        } catch (InvalidArgumentException $e) {
67
            // no catch exception
68
        }
69
    }
70
71
    /**
72
     * Execute
73
     *
74
     * @param InputInterface  $input
75
     * @param OutputInterface $output
76
     *
77
     * @return void
78
     * @throws TaskNameIsEmptyException
79
     * @throws TaskNameIsAlreadyExistedException
80
     * @throws TaskCannotBeSavedException
81
     * @throws InvalidArgumentException
82
     */
83
    protected function execute(InputInterface $input, OutputInterface $output)
84
    {
85
        try {
86
            $name = $input->getArgument('name');
87
        } catch (InvalidArgumentException $e) {
88
            throw $e;
89
        }
90
91
        try {
92
            $task = $this->taskCommand->addNewTask($name);
93
94
            $output->writeln('Task created with #' . $task->getId());
95
        } catch (TaskNameIsEmptyException | TaskNameIsAlreadyExistedException | TaskCannotBeSavedException $e) {
96
            throw $e;
97
        }
98
99
    }
100
101
}
102