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

ZikulaExtensionStatusCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 2
Metric Value
eloc 49
c 3
b 1
f 2
dl 0
loc 82
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A translateState() 0 18 2
A configure() 0 6 1
B execute() 0 49 6
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\Input\InputOption;
19
use Symfony\Component\Console\Output\OutputInterface;
20
use Symfony\Component\Console\Style\SymfonyStyle;
21
use Zikula\ExtensionsModule\Constant;
22
23
class ZikulaExtensionStatusCommand extends AbstractExtensionCommand
24
{
25
    protected static $defaultName = 'zikula:extension:status';
26
27
    protected function configure()
28
    {
29
        $this
30
            ->setDescription('Display status information of a Zikula extension')
31
            ->addArgument('bundle_name', InputArgument::REQUIRED, 'Bundle class name (e.g. ZikulaUsersModule)')
32
            ->addOption('get', null, InputOption::VALUE_REQUIRED, 'Which property to fetch?')
33
        ;
34
    }
35
36
    protected function execute(InputInterface $input, OutputInterface $output): int
37
    {
38
        $io = new SymfonyStyle($input, $output);
39
        $bundleName = $input->getArgument('bundle_name');
40
        $get = $input->getOption('get');
41
42
        if (!$input->isInteractive()) {
43
            $io->error('This command only runs in interactive mode.');
44
45
            return 1;
46
        }
47
48
        $this->reSync();
49
        if (null === $extension = $this->extensionRepository->findOneBy(['name' => $bundleName])) {
50
            $io->error('The extension cannot be found, please check the name.');
51
52
            return 2;
53
        }
54
55
        $status = $this->translateState($extension->getState());
56
        if (null !== $get) {
57
            if ('status' === $get) {
58
                $io->text($status);
59
60
                return 0;
61
            }
62
            $method = 'get' . ucfirst($get);
63
            try {
64
                $value = $extension->{$method}();
65
                $io->text($value);
66
            } catch (\Error $e) {
67
                $io->error(sprintf('There is no property %s', $get));
68
            }
69
70
            return 0;
71
        }
72
73
        $io->title(sprintf('Status of %s', $bundleName));
74
        $io->table(
75
            ['Item', 'Value'],
76
            [
77
                ['Name', $extension->getName()],
78
                ['Version', $extension->getVersion()],
79
                ['Status', $status],
80
                ['Description', $extension->getDescription()],
81
                ['Core Compatibility', $extension->getCoreCompatibility()],
82
        ]);
83
84
        return 0;
85
    }
86
87
    private function translateState(int $state): string
88
    {
89
        $translations = [
90
            Constant::STATE_UNINITIALISED => 'uninitialized',
91
            Constant::STATE_INACTIVE => 'inactive',
92
            Constant::STATE_ACTIVE => 'active',
93
            Constant::STATE_MISSING => 'missing',
94
            Constant::STATE_UPGRADED => 'awaiting upgrade',
95
            Constant::STATE_NOTALLOWED => 'not allowed',
96
            Constant::STATE_TRANSITIONAL => 'in process of install or uninstall',
97
            Constant::STATE_INVALID => 'invalid',
98
        ];
99
100
        if ($state > Constant::INCOMPATIBLE_CORE_SHIFT) {
101
            return 'Incompatible with current core.';
102
        }
103
104
        return $translations[$state];
105
    }
106
}
107