|
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); |
|
|
|
|
|
|
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
|
|
|
|
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:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.