Completed
Push — master ( a15f89...abb079 )
by Craig
07:12 queued 01:36
created

ZikulaExtensionUninstallCommand   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
dl 0
loc 64
rs 10
c 1
b 0
f 0
wmc 11

2 Methods

Rating   Name   Duplication   Size   Complexity  
B execute() 0 52 10
A configure() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula Foundation - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\ExtensionsModule\Command;
15
16
use Symfony\Component\Console\Input\InputArgument;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Console\Style\SymfonyStyle;
20
use Zikula\ExtensionsModule\Constant;
21
22
class ZikulaExtensionUninstallCommand extends AbstractExtensionCommand
23
{
24
    protected static $defaultName = 'zikula:extension:uninstall';
25
26
    protected function configure()
27
    {
28
        $this
29
            ->setDescription('Uninstall a zikula module or theme')
30
            ->addArgument('bundle_name', InputArgument::REQUIRED, 'Bundle class name (e.g. ZikulaUsersModule)')
31
        ;
32
    }
33
34
    protected function execute(InputInterface $input, OutputInterface $output): int
35
    {
36
        $io = new SymfonyStyle($input, $output);
37
        $bundleName = $input->getArgument('bundle_name');
38
39
        if (false === $extension = $this->isInstalled($bundleName)) {
0 ignored issues
show
Bug introduced by
It seems like $bundleName can also be of type null and string[]; however, parameter $bundleName of Zikula\ExtensionsModule\...nCommand::isInstalled() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

39
        if (false === $extension = $this->isInstalled(/** @scrutinizer ignore-type */ $bundleName)) {
Loading history...
40
            if ($input->isInteractive()) {
41
                $io->error('The extension is not installed and therefore cannot be uninstalled.');
42
            }
43
44
            return 1;
45
        }
46
47
        if (Constant::STATE_MISSING === $extension->getState()) {
48
            if ($input->isInteractive()) {
49
                $io->error('The extension cannot be uninstalled because its files are missing.');
50
            }
51
52
            return 2;
53
        }
54
55
        $requiredDependents = $this->dependencyHelper->getDependentExtensions($extension);
56
        if (!empty($requiredDependents)) {
57
            if ($input->isInteractive()) {
58
                $names = implode(', ', array_map(function ($dependent) {
59
                    return $dependent->getModname();
60
                }, $requiredDependents));
61
62
                $io->error(sprintf('The extension is a required dependency of [%s]. Please uninstall these extensions first.', $names));
63
            }
64
65
            return 3;
66
        }
67
68
        $blocks = $this->blockRepository->findBy(['module' => $extension]);
69
        $this->blockRepository->remove($blocks);
70
71
        if (false === $this->extensionHelper->uninstall($extension)) {
72
            if ($input->isInteractive()) {
73
                $io->error('Could not uninstall the extension');
74
            }
75
76
            return 4;
77
        }
78
79
        $this->reSync();
80
81
        if ($input->isInteractive()) {
82
            $io->success('The extension has been uninstalled.');
83
        }
84
85
        return 0;
86
    }
87
}
88