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

PublishWebPageCommand::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 10
loc 10
rs 9.4285
cc 2
eloc 6
nc 2
nop 2
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\PublishWebPageHandler;
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 PublishWebPageCommand
16
 */
17 View Code Duplication
class PublishWebPageCommand extends ContainerAwareCommand
0 ignored issues
show
Duplication introduced by
This class 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...
18
{
19
    /**
20
     * @var Bus
21
     */
22
    protected $bus;
23
24
    /**
25
     * @var PublishWebPageHandler
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 PublishWebPageHandler $handler
42
     * @param WebPageReadService $service
43
     * @param $commandName
44
     */
45
    public function __construct(
46
        Bus $bus,
47
        PublishWebPageHandler $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:publish')
66
            ->setDescription('Publish a new page')
67
            ->addArgument('id', InputArgument::OPTIONAL, 'The page identifier')
68
        ;
69
    }
70
71
    /**
72
     * @param InputInterface $input
73
     * @param OutputInterface $output
74
     */
75
    protected function interact(InputInterface $input, OutputInterface $output)
76
    {
77
        $dialog = $this->getHelperSet()->get('dialog');
78
79
        if (!$input->getArgument('id')) {
80
            $id = $dialog->askAndValidate(
81
                $output,
82
                'Please give an id:',
83
                function ($id) {
84
                    if (empty($id)) {
85
                        throw new \InvalidArgumentException('Id cannot be empty!');
86
                    }
87
88
                    return $id;
89
                }
90
            );
91
92
            $input->setArgument('id', $id);
93
        }
94
    }
95
96
    protected function execute(InputInterface $input, OutputInterface $output)
97
    {
98
        $pageId = new WebPageId($input->getArgument('id'));
99
        $page = $this->service->read($pageId);
100
101
        if ($page) {
102
            $this->bus->register($this->commandName, $this->handler);
103
            $this->bus->handle(new $this->commandName($page));
104
        }
105
    }
106
}
107