Passed
Push — master ( 391bf3...b0a800 )
by Christian
10:31 queued 10s
created

SalesChannelMaintenanceEnableCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 12
rs 9.9666
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\System\SalesChannel\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 Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
class SalesChannelMaintenanceEnableCommand extends Command
15
{
16
    protected static $defaultName = 'sales-channel:maintenance:enable';
17
18
    /**
19
     * @var bool
20
     */
21
    protected $setMaintenanceMode = true;
22
23
    /**
24
     * @var EntityRepositoryInterface
25
     */
26
    private $salesChannelRepository;
27
28
    public function __construct(
29
        EntityRepositoryInterface $salesChannelRepository
30
    ) {
31
        $this->salesChannelRepository = $salesChannelRepository;
32
33
        parent::__construct();
34
    }
35
36
    protected function configure(): void
37
    {
38
        $this->addArgument(
39
            'ids',
40
            InputArgument::IS_ARRAY | InputArgument::OPTIONAL,
41
            'Which Sales Channels do you want to update maintenance mode for? (Optional when --all flag is used)',
42
            []
43
        )->addOption(
44
            'all',
45
            'a',
46
            InputOption::VALUE_NONE,
47
            'Set maintenance mode for all sales channels'
48
        );
49
    }
50
51
    protected function execute(InputInterface $input, OutputInterface $output): int
52
    {
53
        $context = Context::createDefaultContext();
54
        $criteria = new Criteria();
55
56
        if (!$input->getOption('all')) {
57
            $ids = $input->getArgument('ids');
58
            if ($ids === null || $ids === []) {
59
                $output->write('No sales channels were updated. Provide id(s) or run with --all option.');
60
61
                return 0;
62
            }
63
64
            if (\is_array($ids)) {
65
                $criteria->setIds($ids);
66
            } else {
67
                $criteria->setIds([$ids]);
68
            }
69
        }
70
        $salesChannelIds = $this->salesChannelRepository->searchIds($criteria, $context)->getIds();
71
72
        if (empty($salesChannelIds)) {
73
            $output->write(sprintf('No sales channels were updated'));
74
75
            return 0;
76
        }
77
78
        $update = array_map(function (string $id) {
79
            return [
80
                'id' => $id,
81
                'maintenance' => $this->setMaintenanceMode,
82
            ];
83
        }, $salesChannelIds);
84
85
        $this->salesChannelRepository->update($update, $context);
86
87
        $output->write(sprintf('Updated maintenance mode for %s sales channel(s)', count($salesChannelIds)));
88
89
        return 0;
90
    }
91
}
92