Passed
Push — trunk ( b03a35...e67894 )
by Christian
15:10 queued 14s
created

ThemeCompileCommand::execute()   D

Complexity

Conditions 20
Paths 5

Size

Total Lines 46
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 20
eloc 30
nc 5
nop 2
dl 0
loc 46
rs 4.1666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
2
3
namespace Shopware\Storefront\Theme\Command;
4
5
use Shopware\Core\Framework\Context;
6
use Shopware\Core\Framework\Log\Package;
7
use Shopware\Storefront\Theme\ConfigLoader\AbstractAvailableThemeProvider;
8
use Shopware\Storefront\Theme\ThemeService;
9
use Symfony\Component\Console\Attribute\AsCommand;
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Input\InputOption;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use Symfony\Component\Console\Style\SymfonyStyle;
15
16
#[AsCommand(
17
    name: 'theme:compile',
18
    description: 'Compile the theme',
19
)]
20
#[Package('storefront')]
21
class ThemeCompileCommand extends Command
22
{
23
    private SymfonyStyle $io;
24
25
    /**
26
     * @internal
27
     */
28
    public function __construct(
29
        private readonly ThemeService $themeService,
30
        private readonly AbstractAvailableThemeProvider $themeProvider
31
    ) {
32
        parent::__construct();
33
    }
34
35
    protected function configure(): void
36
    {
37
        $this
38
            ->addOption('keep-assets', 'k', InputOption::VALUE_NONE, 'Keep current assets, do not delete them')
39
            ->addOption('active-only', 'a', InputOption::VALUE_NONE, 'Compile themes only for active sales channels')
40
            ->addOption('only', 'o', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Compile themes only for given sales channels ids')
41
            ->addOption('skip', 's', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Skip compiling themes for given sales channels ids')
42
            ->addOption('only-themes', 'O', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Compile only themes for given theme ids')
43
            ->addOption('skip-themes', 'S', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Skip compiling themes for given theme ids')
44
        ;
45
    }
46
47
    protected function execute(InputInterface $input, OutputInterface $output): int
48
    {
49
        $this->io = new SymfonyStyle($input, $output);
50
        $context = Context::createDefaultContext();
51
        $this->io->writeln('Start theme compilation');
52
53
        $onlySalesChannel = ((array) $input->getOption('only')) ?: null;
54
        $skipSalesChannel = ((array) $input->getOption('skip')) ?: null;
55
        if ($onlySalesChannel !== null && $skipSalesChannel !== null
56
            && \count(array_intersect($onlySalesChannel, $skipSalesChannel)) > 0) {
57
            $this->io->error('The sales channel includes and skips contain contradicting entries:' . implode(
58
                ', ',
59
                array_intersect($onlySalesChannel, $skipSalesChannel)
60
            ));
61
62
            return self::FAILURE;
63
        }
64
65
        $onlyThemes = ((array) $input->getOption('only-themes')) ?: null;
66
        $skipThemes = ((array) $input->getOption('skip-themes')) ?: null;
67
        if ($onlyThemes !== null && $skipThemes !== null
68
            && \count(array_intersect($onlyThemes, $skipThemes)) > 0) {
69
            $this->io->error('The theme includes and skips contain contradicting entries:' . implode(
70
                ', ',
71
                array_intersect($onlyThemes, $skipThemes)
72
            ));
73
74
            return self::FAILURE;
75
        }
76
77
        foreach ($this->themeProvider->load($context, $input->getOption('active-only')) as $salesChannelId => $themeId) {
0 ignored issues
show
Deprecated Code introduced by
The function Shopware\Storefront\Them...leThemeProvider::load() has been deprecated: tag:v6.6.0 - parameter $activeOnly will be introduced in future version ( Ignorable by Annotation )

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

77
        foreach (/** @scrutinizer ignore-deprecated */ $this->themeProvider->load($context, $input->getOption('active-only')) as $salesChannelId => $themeId) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Unused Code introduced by
The call to Shopware\Storefront\Them...leThemeProvider::load() has too many arguments starting with $input->getOption('active-only'). ( Ignorable by Annotation )

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

77
        foreach ($this->themeProvider->/** @scrutinizer ignore-call */ load($context, $input->getOption('active-only')) as $salesChannelId => $themeId) {

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
78
            if ($onlySalesChannel !== null && !\in_array($salesChannelId, $onlySalesChannel, true)
79
                || $skipSalesChannel !== null && \in_array($salesChannelId, $skipSalesChannel, true)
80
                || $onlyThemes !== null && !\in_array($themeId, $onlyThemes, true)
81
                || $skipThemes !== null && \in_array($themeId, $skipThemes, true)) {
82
                continue;
83
            }
84
85
            $this->io->block(\sprintf('Compiling theme for sales channel for : %s', $salesChannelId));
86
87
            $start = microtime(true);
88
            $this->themeService->compileTheme($salesChannelId, $themeId, $context, null, !$input->getOption('keep-assets'));
89
            $this->io->note(sprintf('Took %f seconds', microtime(true) - $start));
90
        }
91
92
        return self::SUCCESS;
93
    }
94
}
95