Passed
Push — trunk ( 36db4a...0fa3f8 )
by Christian
16:05 queued 14s
created

UpdatePathCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 60
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A execute() 0 37 5
A configure() 0 4 1
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Content\Media\Infrastructure\Command;
4
5
use Shopware\Core\Content\Media\Core\Application\MediaPathUpdater;
6
use Shopware\Core\Framework\DataAbstractionLayer\Dbal\Common\IteratorFactory;
7
use Shopware\Core\Framework\Log\Package;
8
use Symfony\Component\Console\Attribute\AsCommand;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Helper\ProgressBar;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
/**
15
 * @internal
16
 */
17
#[AsCommand(
18
    name: 'media:update-path',
19
    description: 'Iterates over the media and updates the path column.',
20
)]
21
#[Package('content')]
22
class UpdatePathCommand extends Command
23
{
24
    /**
25
     * @internal
26
     */
27
    public function __construct(
28
        private readonly MediaPathUpdater $updater,
29
        private readonly IteratorFactory $factory
30
    ) {
31
        parent::__construct();
32
    }
33
34
    protected function configure(): void
35
    {
36
        $this->setDescription('Update media paths')
37
            ->addOption('force', 'f', null, 'Force update of all media paths');
38
    }
39
40
    protected function execute(InputInterface $input, OutputInterface $output): int
41
    {
42
        $output->writeln('Updating media paths...');
43
44
        $iterator = $this->factory->createIterator('media');
45
        if (!$input->getOption('force')) {
46
            $iterator->getQuery()->andWhere('path IS NULL');
47
        }
48
49
        $progressBar = new ProgressBar($output, $iterator->fetchCount());
50
        $progressBar->start();
51
52
        while ($ids = $iterator->fetch()) {
53
            $this->updater->updateMedia($ids);
54
            $progressBar->advance(\count($ids));
55
        }
56
        $progressBar->finish();
57
58
        $output->writeln('');
59
        $output->writeln('Updating thumbnail paths...');
60
        $output->writeln('');
61
62
        $iterator = $this->factory->createIterator('media_thumbnail');
63
64
        if (!$input->getOption('force')) {
65
            $iterator->getQuery()->andWhere('path IS NULL');
66
        }
67
        $progressBar = new ProgressBar($output, $iterator->fetchCount());
68
69
        $progressBar->start();
70
        while ($ids = $iterator->fetch()) {
71
            $this->updater->updateThumbnails($ids);
72
            $progressBar->advance(\count($ids));
73
        }
74
        $progressBar->finish();
75
76
        return 0;
77
    }
78
}
79