Completed
Push — feature/rewrite ( 88e863...f6b662 )
by Alexandre
01:55
created

CreateWebPageCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 8
nc 1
nop 3
1
<?php
2
3
namespace Black\Bundle\PageBundle\Application\Command;
4
5
use Black\Page\Domain\Model\WebPageId;
6
use Black\Page\Infrastructure\CQRS\Handler\CreateWebPageHandler;
7
use Black\DDD\CQRSinPHP\Infrastructure\CQRS\Bus;
8
use Rhumsaa\Uuid\Uuid;
9
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
/**
15
 * Class CreateWebPageCommand
16
 */
17
class CreateWebPageCommand extends ContainerAwareCommand
18
{
19
    /**
20
     * @var Bus
21
     */
22
    protected $bus;
23
24
    /**
25
     * @var CreateWebPageHandler
26
     */
27
    protected $handler;
28
29
    /**
30
     * @var
31
     */
32
    protected $commandName;
33
34
    /**
35
     * @var \Symfony\Component\Console\Helper\HelperInterface
36
     */
37
    protected $dialog;
38
39
    /**
40
     * @param Bus $bus
41
     * @param CreateWebPageHandler $handler
42
     * @param $commandName
43
     */
44
    public function __construct(
45
        Bus $bus,
46
        CreateWebPageHandler $handler,
47
        $commandName
48
    ) {
49
        $this->bus = $bus;
50
        $this->handler = $handler;
51
        $this->commandName = $commandName;
52
53
        parent::__construct();
54
    }
55
56
    /**
57
     *
58
     */
59
    protected function configure()
60
    {
61
        $this
62
            ->setName('black:page:create')
63
            ->setDescription('Create a new page')
64
            ->addArgument('author', InputArgument::OPTIONAL, 'The author')
65
            ->addArgument('name', InputArgument::OPTIONAL, 'The page name');
66
    }
67
68
    /**
69
     * @param InputInterface $input
70
     * @param OutputInterface $output
71
     */
72
    protected function interact(InputInterface $input, OutputInterface $output)
73
    {
74
        $dialog = $this->getHelperSet()->get('dialog');
75
76
        if (!$input->getArgument('author')) {
77
            $author = $dialog->askAndValidate(
78
                $output,
79
                'Please give an author name:',
80
                function ($author) {
81
                    if (empty($author)) {
82
                        throw new \InvalidArgumentException('Author cannot be empty!');
83
                    }
84
85
                    return $author;
86
                }
87
            );
88
89
            $input->setArgument('author', $author);
90
        }
91
92
        if (!$input->getArgument('name')) {
93
            $name = $dialog->askAndValidate(
94
                $output,
95
                'Please choose a name:',
96
                function ($name) {
97
                    if (empty($name)) {
98
                        throw new \InvalidArgumentException('Name cannot be empty!');
99
                    }
100
101
                    return $name;
102
                }
103
            );
104
105
            $input->setArgument('name', $name);
106
        }
107
    }
108
109
    protected function execute(InputInterface $input, OutputInterface $output)
110
    {
111
        $pageId = new WebPageId(Uuid::uuid4());
112
113
        $this->bus->register($this->commandName, $this->handler);
114
        $this->bus->handle(new $this->commandName(
115
            $pageId,
116
            $input->getArgument('author'),
117
            $input->getArgument('name')
118
        ));
119
    }
120
}
121