Completed
Push — develop ( 5784a0...825bf8 )
by Christian
02:52
created

ToggleBlockCommand::execute()   B

Complexity

Conditions 6
Paths 13

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.8657
c 0
b 0
f 0
cc 6
nc 13
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace N98\Magento\Command\Cms;
6
7
use Magento\Cms\Api\BlockRepositoryInterface;
8
use Magento\Framework\Exception\NoSuchEntityException;
9
use Magento\Framework\Exception\LocalizedException;
10
use N98\Magento\Command\AbstractMagentoCommand;
11
use Symfony\Component\Console\Input\InputArgument;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
15
use function is_string;
16
use function sprintf;
17
18
/**
19
 * Class ToggleBlockCommand
20
 * @package N98\Magento\Command\Cms
21
 */
22
class ToggleBlockCommand extends AbstractMagentoCommand
23
{
24
    protected const BLOCK_ID_ARGUMENT = 'blockId';
25
26
    protected $blockRepository;
27
28
    public function inject(BlockRepositoryInterface $blockRepository): void
29
    {
30
        $this->blockRepository = $blockRepository;
31
    }
32
33
    protected function configure(): void
34
    {
35
        $this
36
            ->setName('cms:block:toggle')
37
            ->addArgument(self::BLOCK_ID_ARGUMENT, InputArgument::REQUIRED, 'Block identifier')
38
            ->setDescription('Toggle Cms Block status');
39
    }
40
41
    protected function execute(InputInterface $input, OutputInterface $output): void
42
    {
43
        $this->detectMagento($output, true);
44
        if (!$this->initMagento()) {
45
            return;
46
        }
47
48
        $blockId = $input->getArgument(self::BLOCK_ID_ARGUMENT);
49
        if (!is_string($blockId)) {
50
            $output->writeln('Block Identifier is a required argument. Use --help for more information.');
51
            return;
52
        }
53
54
        try {
55
            $block = $this->blockRepository->getById($blockId);
56
            $newStatus = !$block->isActive();
57
            $block->setIsActive($newStatus);
58
            $this->blockRepository->save($block);
59
            $output->writeln(
60
                sprintf('Block status has been changed to <info>%s</info>.', $newStatus ? 'Enabled' : 'Disabled')
61
            );
62
        } catch (NoSuchEntityException $e) {
0 ignored issues
show
Bug introduced by
The class Magento\Framework\Exception\NoSuchEntityException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
63
            $output->writeln(sprintf('Block with ID <info>%s</info> does not exist.', $blockId));
64
        } catch (LocalizedException $e) {
0 ignored issues
show
Bug introduced by
The class Magento\Framework\Exception\LocalizedException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
65
            $output->writeln('Something went wrong while editing the block status.');
66
        }
67
    }
68
}
69