Passed
Push — master ( f2725e...d61157 )
by Christian
09:49 queued 10s
created

ThemeCompileCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
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\DataAbstractionLayer\EntityRepositoryInterface;
7
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
8
use Shopware\Core\System\SalesChannel\SalesChannelCollection;
9
use Shopware\Storefront\Theme\ThemeCollection;
10
use Shopware\Storefront\Theme\ThemeService;
11
use Symfony\Component\Console\Command\Command;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Input\InputOption;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use Symfony\Component\Console\Style\SymfonyStyle;
16
17
class ThemeCompileCommand extends Command
18
{
19
    protected static $defaultName = 'theme:compile';
20
21
    /**
22
     * @var SymfonyStyle
23
     */
24
    private $io;
25
26
    /**
27
     * @var ThemeService
28
     */
29
    private $themeService;
30
31
    /**
32
     * @var EntityRepositoryInterface
33
     */
34
    private $salesChannelRepository;
35
36
    public function __construct(ThemeService $themeService, EntityRepositoryInterface $salesChannelRepository)
37
    {
38
        parent::__construct();
39
        $this->themeService = $themeService;
40
        $this->salesChannelRepository = $salesChannelRepository;
41
    }
42
43
    public function configure(): void
44
    {
45
        $this
46
            ->addOption('keep-assets', 'k', InputOption::VALUE_NONE, 'Keep current assets, do not delete them');
47
    }
48
49
    protected function execute(InputInterface $input, OutputInterface $output): int
50
    {
51
        $this->io = new SymfonyStyle($input, $output);
52
        $context = Context::createDefaultContext();
53
        $this->io->writeln('Start theme compilation');
54
        $start = microtime(true);
55
56
        $salesChannels = $this->getSalesChannels($context);
57
        foreach ($salesChannels as $salesChannel) {
58
            /** @var ThemeCollection|null $themes */
59
            $themes = $salesChannel->getExtensionOfType('themes', ThemeCollection::class);
60
            if (!$themes || !$theme = $themes->first()) {
61
                continue;
62
            }
63
            $this->themeService->compileTheme($salesChannel->getId(), $theme->getId(), $context, null, !$input->getOption('keep-assets'));
64
        }
65
66
        $this->io->note(sprintf('Took %f seconds', (float) microtime(true) - $start));
67
68
        return 0;
69
    }
70
71
    private function getSalesChannels(Context $context): SalesChannelCollection
72
    {
73
        $criteria = new Criteria();
74
        $criteria->addAssociation('themes');
75
76
        /** @var SalesChannelCollection $result */
77
        $result = $this->salesChannelRepository->search($criteria, $context)->getEntities();
78
79
        return $result;
80
    }
81
}
82