Completed
Push — develop ( 3c1b70...55bb1b )
by Tom
03:03
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 function is_string;
8
use Magento\Cms\Api\BlockRepositoryInterface;
9
use Magento\Framework\Exception\LocalizedException;
10
use Magento\Framework\Exception\NoSuchEntityException;
11
use N98\Magento\Command\AbstractMagentoCommand;
12
use function sprintf;
13
use Symfony\Component\Console\Input\InputArgument;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
/**
18
 * Class ToggleBlockCommand
19
 * @package N98\Magento\Command\Cms
20
 */
21
class ToggleBlockCommand extends AbstractMagentoCommand
22
{
23
    protected const BLOCK_ID_ARGUMENT = 'blockId';
24
25
    protected $blockRepository;
26
27
    public function inject(BlockRepositoryInterface $blockRepository): void
28
    {
29
        $this->blockRepository = $blockRepository;
30
    }
31
32
    protected function configure(): void
33
    {
34
        $this
35
            ->setName('cms:block:toggle')
36
            ->addArgument(self::BLOCK_ID_ARGUMENT, InputArgument::REQUIRED, 'Block identifier')
37
            ->setDescription('Toggle Cms Block status');
38
    }
39
40
    protected function execute(InputInterface $input, OutputInterface $output): void
41
    {
42
        $this->detectMagento($output, true);
43
        if (!$this->initMagento()) {
44
            return;
45
        }
46
47
        $blockId = $input->getArgument(self::BLOCK_ID_ARGUMENT);
48
        if (!is_string($blockId)) {
49
            $output->writeln('Block Identifier is a required argument. Use --help for more information.');
50
            return;
51
        }
52
53
        try {
54
            $block = $this->blockRepository->getById($blockId);
55
            $newStatus = !$block->isActive();
56
            $block->setIsActive($newStatus);
57
            $this->blockRepository->save($block);
58
            $output->writeln(
59
                sprintf('Block status has been changed to <info>%s</info>.', $newStatus ? 'Enabled' : 'Disabled')
60
            );
61
        } 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...
62
            $output->writeln(sprintf('Block with ID <info>%s</info> does not exist.', $blockId));
63
        } 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...
64
            $output->writeln('Something went wrong while editing the block status.');
65
        }
66
    }
67
}
68