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\WriteWebPageHandler; |
7
|
|
|
use Black\Page\Infrastructure\Service\WebPageReadService; |
8
|
|
|
use Black\DDD\CQRSinPHP\Infrastructure\CQRS\Bus; |
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 WriteWebPageCommand |
16
|
|
|
*/ |
17
|
|
|
class WriteWebPageCommand extends ContainerAwareCommand |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var Bus |
21
|
|
|
*/ |
22
|
|
|
protected $bus; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var WriteWebPageHandler |
26
|
|
|
*/ |
27
|
|
|
protected $handler; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var WebPageReadService |
31
|
|
|
*/ |
32
|
|
|
protected $service; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var |
36
|
|
|
*/ |
37
|
|
|
protected $commandName; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param Bus $bus |
41
|
|
|
* @param WriteWebPageHandler $handler |
42
|
|
|
* @param WebPageReadService $service |
43
|
|
|
* @param $commandName |
44
|
|
|
*/ |
45
|
|
|
public function __construct( |
46
|
|
|
Bus $bus, |
47
|
|
|
WriteWebPageHandler $handler, |
48
|
|
|
WebPageReadService $service, |
49
|
|
|
$commandName |
50
|
|
|
) { |
51
|
|
|
$this->bus = $bus; |
52
|
|
|
$this->handler = $handler; |
53
|
|
|
$this->service = $service; |
54
|
|
|
$this->commandName = $commandName; |
55
|
|
|
|
56
|
|
|
parent::__construct(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* |
61
|
|
|
*/ |
62
|
|
|
protected function configure() |
63
|
|
|
{ |
64
|
|
|
$this |
65
|
|
|
->setName('black:page:write') |
66
|
|
|
->setDescription('Write a new page') |
67
|
|
|
->addArgument('id', InputArgument::OPTIONAL, 'The page identifier') |
68
|
|
|
->addArgument('headline', InputArgument::OPTIONAL, 'The headline') |
69
|
|
|
->addArgument('about', InputArgument::OPTIONAL, 'The about ') |
70
|
|
|
->addArgument('text', InputArgument::OPTIONAL, 'The text') |
71
|
|
|
; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param InputInterface $input |
76
|
|
|
* @param OutputInterface $output |
77
|
|
|
*/ |
78
|
|
|
protected function interact(InputInterface $input, OutputInterface $output) |
79
|
|
|
{ |
80
|
|
|
$dialog = $this->getHelperSet()->get('dialog'); |
81
|
|
|
|
82
|
|
|
if (!$input->getArgument('id')) { |
83
|
|
|
$id = $dialog->askAndValidate( |
84
|
|
|
$output, |
85
|
|
|
'Please give an id:', |
86
|
|
|
function ($id) { |
87
|
|
|
if (empty($id)) { |
88
|
|
|
throw new \InvalidArgumentException('Id cannot be empty!'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $id; |
92
|
|
|
} |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
$input->setArgument('id', $id); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if (!$input->getArgument('headline')) { |
99
|
|
|
$headline = $dialog->askAndValidate( |
100
|
|
|
$output, |
101
|
|
|
'Please give an headline:', |
102
|
|
|
function ($headline) { |
103
|
|
|
if (empty($headline)) { |
104
|
|
|
throw new \InvalidArgumentException('Headline cannot be empty!'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $headline; |
108
|
|
|
} |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
$input->setArgument('headline', $headline); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if (!$input->getArgument('about')) { |
115
|
|
|
$about = $dialog->askAndValidate( |
116
|
|
|
$output, |
117
|
|
|
'Please give an about:', |
118
|
|
|
function ($about) { |
119
|
|
|
if (empty($about)) { |
120
|
|
|
throw new \InvalidArgumentException('About cannot be empty!'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $about; |
124
|
|
|
} |
125
|
|
|
); |
126
|
|
|
|
127
|
|
|
$input->setArgument('about', $about); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if (!$input->getArgument('text')) { |
131
|
|
|
$text = $dialog->askAndValidate( |
132
|
|
|
$output, |
133
|
|
|
'Please give an text:', |
134
|
|
|
function ($text) { |
135
|
|
|
if (empty($text)) { |
136
|
|
|
throw new \InvalidArgumentException('Text cannot be empty!'); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $text; |
140
|
|
|
} |
141
|
|
|
); |
142
|
|
|
|
143
|
|
|
$input->setArgument('text', $text); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
148
|
|
|
{ |
149
|
|
|
$pageId = new WebPageId($input->getArgument('id')); |
150
|
|
|
$page = $this->service->read($pageId); |
151
|
|
|
|
152
|
|
|
if ($page) { |
153
|
|
|
$this->bus->register($this->commandName, $this->handler); |
154
|
|
|
$this->bus->handle(new $this->commandName( |
155
|
|
|
$page, |
156
|
|
|
$input->getArgument('headline'), |
157
|
|
|
$input->getArgument('about'), |
158
|
|
|
$input->getArgument('text') |
159
|
|
|
)); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|