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

FixMediaContextCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
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 Sonata\ClassificationBundle\Model\ContextManagerInterface;
18
use Sonata\MediaBundle\Model\CategoryManagerInterface;
19
use Sonata\MediaBundle\Provider\Pool;
20
use Symfony\Component\Console\Command\Command;
21
use Symfony\Component\Console\Input\InputInterface;
22
use Symfony\Component\Console\Output\OutputInterface;
23
24
/**
25
 * @final since sonata-project/media-bundle 3.21.0
26
 */
27
class FixMediaContextCommand extends Command
28
{
29
    /**
30
     * @var Pool
31
     */
32
    private $mediaPool;
33
34
    /**
35
     * @var CategoryManagerInterface|null
36
     */
37
    private $categoryManager;
38
39
    /**
40
     * @var ContextManagerInterface|null
41
     */
42
    private $contextManager;
43
44
    public function __construct(Pool $mediaPool, ?CategoryManagerInterface $categoryManager = null, ?ContextManagerInterface $contextManager = null)
45
    {
46
        parent::__construct();
47
48
        $this->mediaPool = $mediaPool;
49
        $this->categoryManager = $categoryManager;
50
        $this->contextManager = $contextManager;
51
    }
52
53
    public function configure(): void
54
    {
55
        $this->setName('sonata:media:fix-media-context');
56
        $this->setDescription('Generate the default category for each media context');
57
    }
58
59
    public function execute(InputInterface $input, OutputInterface $output): int
60
    {
61
        if (null === $this->categoryManager || null === $this->contextManager) {
62
            throw new \LogicException(
63
                'This command could not be executed since some of its dependencies is missing.'
64
                .' Are the services "sonata.media.manager.category" and "sonata.classification.manager.context" available?'
65
            );
66
        }
67
68
        foreach ($this->mediaPool->getContexts() as $context => $contextAttrs) {
69
            /** @var ContextInterface $defaultContext */
70
            $defaultContext = $this->contextManager->findOneBy([
71
                'id' => $context,
72
            ]);
73
74
            if (!$defaultContext) {
75
                $output->writeln(sprintf(" > default context for '%s' is missing, creating one", $context));
76
                $defaultContext = $this->contextManager->create();
77
                $defaultContext->setId($context);
78
                $defaultContext->setName(ucfirst($context));
79
                $defaultContext->setEnabled(true);
80
81
                $this->contextManager->save($defaultContext);
82
            }
83
84
            $defaultCategory = $this->categoryManager->getRootCategory($defaultContext);
0 ignored issues
show
Bug introduced by
It seems like $defaultContext defined by $this->contextManager->f...rray('id' => $context)) on line 70 can also be of type object<Sonata\Classifica...Model\ContextInterface>; however, Sonata\MediaBundle\Model...face::getRootCategory() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
85
86
            if (!$defaultCategory) {
87
                $output->writeln(sprintf(" > default category for '%s' is missing, creating one", $context));
88
                $defaultCategory = $this->categoryManager->create();
89
                $defaultCategory->setContext($defaultContext);
90
                $defaultCategory->setName(ucfirst($context));
91
                $defaultCategory->setEnabled(true);
92
                $defaultCategory->setPosition(0);
93
94
                $this->categoryManager->save($defaultCategory);
95
            }
96
        }
97
98
        $output->writeln('Done!');
99
100
        return 0;
101
    }
102
}
103