Completed
Push — master ( d66330...eb9ee0 )
by Grégoire
02:15
created

src/Command/FixMediaContextCommand.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
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 Sonata\MediaBundle\Command;
15
16
use Sonata\ClassificationBundle\Model\ContextInterface;
17
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Output\OutputInterface;
20
21
/**
22
 * @final since sonata-project/media-bundle 3.21.0
23
 */
24
class FixMediaContextCommand extends ContainerAwareCommand
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...d\ContainerAwareCommand has been deprecated with message: since Symfony 4.2, use {@see Command} instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
25
{
26
    public function configure(): void
27
    {
28
        $this->setName('sonata:media:fix-media-context');
29
        $this->setDescription('Generate the default category for each media context');
30
    }
31
32
    public function execute(InputInterface $input, OutputInterface $output): int
33
    {
34
        if (!$this->getContainer()->has('sonata.media.manager.category')) {
35
            throw new \LogicException('The classification feature is disabled.');
36
        }
37
38
        $pool = $this->getContainer()->get('sonata.media.pool');
39
        $contextManager = $this->getContainer()->get('sonata.classification.manager.context');
40
        $categoryManager = $this->getContainer()->get('sonata.media.manager.category');
41
42
        foreach ($pool->getContexts() as $context => $contextAttrs) {
43
            /** @var ContextInterface $defaultContext */
44
            $defaultContext = $contextManager->findOneBy([
45
                'id' => $context,
46
            ]);
47
48
            if (!$defaultContext) {
49
                $output->writeln(sprintf(" > default context for '%s' is missing, creating one", $context));
50
                $defaultContext = $contextManager->create();
51
                $defaultContext->setId($context);
52
                $defaultContext->setName(ucfirst($context));
53
                $defaultContext->setEnabled(true);
54
55
                $contextManager->save($defaultContext);
56
            }
57
58
            $defaultCategory = $categoryManager->getRootCategory($defaultContext);
59
60
            if (!$defaultCategory) {
61
                $output->writeln(sprintf(" > default category for '%s' is missing, creating one", $context));
62
                $defaultCategory = $categoryManager->create();
63
                $defaultCategory->setContext($defaultContext);
64
                $defaultCategory->setName(ucfirst($context));
65
                $defaultCategory->setEnabled(true);
66
                $defaultCategory->setPosition(0);
67
68
                $categoryManager->save($defaultCategory);
69
            }
70
        }
71
72
        $output->writeln('Done!');
73
74
        return 0;
75
    }
76
}
77