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

TaskUpdateCommand::__construct()   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 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
use Todo\Domain\Exception\TaskNotFoundException;
17
18
/**
19
 * Class TaskUpdateCommand
20
 *
21
 * @category None
22
 * @package  Todo\Cli\CliBundle\Command
23
 * @author   Martin Pham <[email protected]>
24
 * @license  None http://
25
 * @link     None
26
 */
27
class TaskUpdateCommand extends ContainerAwareCommand
28
{
29
    /**
30
     * TaskCommand
31
     *
32
     * @var Command
33
     */
34
    protected $taskCommand;
35
36
    /**
37
     * TaskListCommand constructor
38
     *
39
     * @param Command $taskCommand Task Command
40
     *
41
     * @throws LogicException
42
     */
43
    public function __construct(Command $taskCommand)
44
    {
45
        $this->taskCommand = $taskCommand;
46
47
        try {
48
            parent::__construct();
49
        } catch (LogicException $e) {
50
            throw $e;
51
        }
52
    }
53
54
    /**
55
     * Configure
56
     *
57
     * @return void
58
     * @throws InvalidArgumentException
59
     */
60
    protected function configure()
61
    {
62
        try {
63
            $this
64
                ->setName('task:update')
65
                ->setDescription('...')
66
                ->addArgument('id', InputArgument::REQUIRED, 'Task ID')
67
                ->addArgument('name', InputArgument::REQUIRED, 'Task name')
68
                ->addArgument('status', InputArgument::REQUIRED, 'Task status');
69
        } catch (InvalidArgumentException $e) {
70
            // no catch exception
71
        }
72
    }
73
74
    /**
75
     * Execute
76
     *
77
     * @param InputInterface  $input
78
     * @param OutputInterface $output
79
     *
80
     * @return void
81
     * @throws TaskNameIsEmptyException
82
     * @throws TaskNameIsAlreadyExistedException
83
     * @throws TaskCannotBeSavedException
84
     * @throws TaskNotFoundException
85
     * @throws InvalidArgumentException
86
     */
87
    protected function execute(InputInterface $input, OutputInterface $output)
88
    {
89
        try {
90
            $taskId = $input->getArgument('id');
91
            $name = $input->getArgument('name');
92
            $status = $input->getArgument('status');
93
        } catch (InvalidArgumentException $e) {
94
            throw $e;
95
        }
96
97
        try {
98
            $this->taskCommand->editTask(
99
                $taskId,
100
                [
101
                    'name' => $name,
102
                    'status' => $status
103
                ]
104
            );
105
106
            $output->writeln('Task is updated');
107
        } catch (TaskNotFoundException | TaskNameIsEmptyException | TaskNameIsAlreadyExistedException | TaskCannotBeSavedException $e) {
108
            throw $e;
109
        }
110
111
    }
112
113
}
114