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

ZikulaExtensionUpgradeCommand   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 28
dl 0
loc 59
rs 10
c 2
b 0
f 1
wmc 12

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 5 1
B execute() 0 34 8
A isUpgradeable() 0 11 3
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 ZikulaExtensionUpgradeCommand extends AbstractExtensionCommand
23
{
24
    protected static $defaultName = 'zikula:extension:upgrade';
25
26
    protected function configure()
27
    {
28
        $this
29
            ->setDescription('Upgrade 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 === $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 === $this->isInstalled(/** @scrutinizer ignore-type */ $bundleName)) {
Loading history...
40
            if ($input->isInteractive()) {
41
                $io->error('The extension is not installed and therefore cannot be upgraded.');
42
            }
43
44
            return 1;
45
        }
46
47
        if (false !== $extension = $this->isUpgradeable($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\...ommand::isUpgradeable() 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

47
        if (false !== $extension = $this->isUpgradeable(/** @scrutinizer ignore-type */ $bundleName)) {
Loading history...
48
            if ($input->isInteractive()) {
49
                $io->error('The extension cannot be upgraded because its version number has not changed.');
50
            }
51
52
            return 2;
53
        }
54
55
        if (!$this->extensionHelper->upgrade($extension)) {
56
            if ($input->isInteractive()) {
57
                $io->error('The extension could not be upgraded.');
58
            }
59
60
            return 3;
61
        }
62
63
        if ($input->isInteractive()) {
64
            $io->success('The extension has been upgraded.');
65
        }
66
67
        return 0;
68
    }
69
70
    private function isUpgradeable(string $bundleName)
71
    {
72
        $extensionsInFileSystem = $this->bundleSyncHelper->scanForBundles();
73
        $this->bundleSyncHelper->syncExtensions($extensionsInFileSystem);
74
        if (null !== $extension = $this->extensionRepository->findOneBy(['name' => $bundleName])) {
75
            if (Constant::STATE_UPGRADED === $extension->getState()) {
76
                return $extension;
77
            }
78
        }
79
80
        return false;
81
    }
82
}
83