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\Block; |
||
15 | |||
16 | use Doctrine\ORM\Mapping\ClassMetadataInfo; |
||
17 | use Sonata\AdminBundle\Admin\AdminInterface; |
||
18 | use Sonata\AdminBundle\Form\FormMapper; |
||
19 | use Sonata\AdminBundle\Form\Type\ModelListType; |
||
20 | use Sonata\BlockBundle\Block\BlockContextInterface; |
||
21 | use Sonata\BlockBundle\Block\Service\AbstractAdminBlockService; |
||
22 | use Sonata\BlockBundle\Meta\Metadata; |
||
23 | use Sonata\BlockBundle\Model\BlockInterface; |
||
24 | use Sonata\Doctrine\Model\ManagerInterface; |
||
25 | use Sonata\Form\Type\ImmutableArrayType; |
||
26 | use Sonata\MediaBundle\Model\GalleryInterface; |
||
27 | use Sonata\MediaBundle\Model\MediaInterface; |
||
28 | use Sonata\MediaBundle\Provider\Pool; |
||
29 | use Symfony\Component\DependencyInjection\ContainerInterface; |
||
30 | use Symfony\Component\Form\Extension\Core\Type\CheckboxType; |
||
31 | use Symfony\Component\Form\Extension\Core\Type\ChoiceType; |
||
32 | use Symfony\Component\Form\Extension\Core\Type\NumberType; |
||
33 | use Symfony\Component\Form\Extension\Core\Type\TextType; |
||
34 | use Symfony\Component\HttpFoundation\Response; |
||
35 | use Symfony\Component\OptionsResolver\OptionsResolver; |
||
36 | use Symfony\Component\Templating\EngineInterface; |
||
37 | |||
38 | /** |
||
39 | * @final since sonata-project/media-bundle 3.21.0 |
||
40 | * |
||
41 | * @author Thomas Rabaix <[email protected]> |
||
42 | */ |
||
43 | class GalleryBlockService extends AbstractAdminBlockService |
||
0 ignored issues
–
show
|
|||
44 | { |
||
45 | /** |
||
46 | * @var ManagerInterface |
||
47 | */ |
||
48 | protected $galleryAdmin; |
||
49 | |||
50 | /** |
||
51 | * @var ManagerInterface |
||
52 | */ |
||
53 | protected $galleryManager; |
||
54 | |||
55 | /** |
||
56 | * @param string $name |
||
57 | */ |
||
58 | public function __construct($name, EngineInterface $templating, ContainerInterface $container, ManagerInterface $galleryManager) |
||
59 | { |
||
60 | parent::__construct($name, $templating); |
||
61 | |||
62 | $this->galleryManager = $galleryManager; |
||
63 | $this->container = $container; |
||
0 ignored issues
–
show
The property
container does not exist. Did you maybe forget to declare it?
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code: class MyClass { }
$x = new MyClass();
$x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: class MyClass {
public $foo;
}
$x = new MyClass();
$x->foo = true;
Loading history...
|
|||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @return Pool |
||
68 | */ |
||
69 | public function getMediaPool() |
||
70 | { |
||
71 | return $this->container->get('sonata.media.pool'); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @return AdminInterface |
||
76 | */ |
||
77 | public function getGalleryAdmin() |
||
78 | { |
||
79 | if (!$this->galleryAdmin) { |
||
80 | $this->galleryAdmin = $this->container->get('sonata.media.admin.gallery'); |
||
81 | } |
||
82 | |||
83 | return $this->galleryAdmin; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * {@inheritdoc} |
||
88 | */ |
||
89 | public function configureSettings(OptionsResolver $resolver): void |
||
90 | { |
||
91 | $resolver->setDefaults([ |
||
92 | 'gallery' => false, |
||
93 | 'title' => null, |
||
94 | 'translation_domain' => null, |
||
95 | 'icon' => null, |
||
96 | 'class' => null, |
||
97 | 'context' => false, |
||
98 | 'format' => false, |
||
99 | 'pauseTime' => 3000, |
||
100 | 'startPaused' => false, |
||
101 | 'template' => '@SonataMedia/Block/block_gallery.html.twig', |
||
102 | 'galleryId' => null, |
||
103 | ]); |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * {@inheritdoc} |
||
108 | */ |
||
109 | public function buildEditForm(FormMapper $formMapper, BlockInterface $block): void |
||
110 | { |
||
111 | $contextChoices = []; |
||
112 | |||
113 | foreach ($this->getMediaPool()->getContexts() as $name => $context) { |
||
114 | $contextChoices[$name] = $name; |
||
115 | } |
||
116 | |||
117 | $gallery = $block->getSetting('galleryId'); |
||
118 | |||
119 | $formatChoices = []; |
||
120 | |||
121 | if ($gallery instanceof GalleryInterface) { |
||
122 | $formats = $this->getMediaPool()->getFormatNamesByContext($gallery->getContext()); |
||
123 | |||
124 | foreach ($formats as $code => $format) { |
||
0 ignored issues
–
show
The expression
$formats of type array|null is not guaranteed to be traversable. How about adding an additional type check?
There are different options of fixing this problem.
Loading history...
|
|||
125 | $formatChoices[$code] = $code; |
||
126 | } |
||
127 | } |
||
128 | |||
129 | // simulate an association ... |
||
130 | $fieldDescription = $this->getGalleryAdmin()->getModelManager()->getNewFieldDescriptionInstance($this->getGalleryAdmin()->getClass(), 'media', [ |
||
131 | 'translation_domain' => 'SonataMediaBundle', |
||
132 | ]); |
||
133 | $fieldDescription->setAssociationAdmin($this->getGalleryAdmin()); |
||
134 | $fieldDescription->setAdmin($formMapper->getAdmin()); |
||
135 | $fieldDescription->setOption('edit', 'list'); |
||
136 | $fieldDescription->setAssociationMapping(['fieldName' => 'gallery', 'type' => ClassMetadataInfo::MANY_TO_ONE]); |
||
137 | |||
138 | $builder = $formMapper->create('galleryId', ModelListType::class, [ |
||
139 | 'sonata_field_description' => $fieldDescription, |
||
140 | 'class' => $this->getGalleryAdmin()->getClass(), |
||
141 | 'model_manager' => $this->getGalleryAdmin()->getModelManager(), |
||
142 | 'label' => 'form.label_gallery', |
||
143 | ]); |
||
144 | |||
145 | $formMapper->add('settings', ImmutableArrayType::class, [ |
||
146 | 'keys' => [ |
||
147 | ['title', TextType::class, [ |
||
148 | 'label' => 'form.label_title', |
||
149 | 'required' => false, |
||
150 | ]], |
||
151 | ['translation_domain', TextType::class, [ |
||
152 | 'label' => 'form.label_translation_domain', |
||
153 | 'required' => false, |
||
154 | ]], |
||
155 | ['icon', TextType::class, [ |
||
156 | 'label' => 'form.label_icon', |
||
157 | 'required' => false, |
||
158 | ]], |
||
159 | ['class', TextType::class, [ |
||
160 | 'label' => 'form.label_class', |
||
161 | 'required' => false, |
||
162 | ]], |
||
163 | ['context', ChoiceType::class, [ |
||
164 | 'required' => true, |
||
165 | 'choices' => $contextChoices, |
||
166 | 'label' => 'form.label_context', |
||
167 | ]], |
||
168 | ['format', ChoiceType::class, [ |
||
169 | 'required' => \count($formatChoices) > 0, |
||
170 | 'choices' => $formatChoices, |
||
171 | 'label' => 'form.label_format', |
||
172 | ]], |
||
173 | [$builder, null, []], |
||
174 | ['pauseTime', NumberType::class, [ |
||
175 | 'label' => 'form.label_pause_time', |
||
176 | ]], |
||
177 | ['startPaused', CheckboxType::class, [ |
||
178 | 'required' => false, |
||
179 | 'label' => 'form.label_start_paused', |
||
180 | ]], |
||
181 | ], |
||
182 | 'translation_domain' => 'SonataMediaBundle', |
||
183 | ]); |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * {@inheritdoc} |
||
188 | */ |
||
189 | public function execute(BlockContextInterface $blockContext, Response $response = null) |
||
190 | { |
||
191 | $gallery = $blockContext->getBlock()->getSetting('galleryId'); |
||
192 | |||
193 | return $this->renderResponse($blockContext->getTemplate(), [ |
||
194 | 'gallery' => $gallery, |
||
195 | 'block' => $blockContext->getBlock(), |
||
196 | 'settings' => $blockContext->getSettings(), |
||
197 | 'elements' => $gallery ? $this->buildElements($gallery) : [], |
||
198 | ], $response); |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * {@inheritdoc} |
||
203 | */ |
||
204 | public function load(BlockInterface $block): void |
||
205 | { |
||
206 | $gallery = $block->getSetting('galleryId'); |
||
207 | |||
208 | if ($gallery) { |
||
209 | $gallery = $this->galleryManager->findOneBy(['id' => $gallery]); |
||
210 | } |
||
211 | |||
212 | $block->setSetting('galleryId', $gallery); |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * {@inheritdoc} |
||
217 | */ |
||
218 | public function prePersist(BlockInterface $block): void |
||
219 | { |
||
220 | $block->setSetting('galleryId', \is_object($block->getSetting('galleryId')) ? $block->getSetting('galleryId')->getId() : null); |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * {@inheritdoc} |
||
225 | */ |
||
226 | public function preUpdate(BlockInterface $block): void |
||
227 | { |
||
228 | $block->setSetting('galleryId', \is_object($block->getSetting('galleryId')) ? $block->getSetting('galleryId')->getId() : null); |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * {@inheritdoc} |
||
233 | */ |
||
234 | public function getBlockMetadata($code = null) |
||
235 | { |
||
236 | return new Metadata($this->getName(), (null !== $code ? $code : $this->getName()), false, 'SonataMediaBundle', [ |
||
0 ignored issues
–
show
false is of type boolean , but the function expects a string|null .
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
Loading history...
|
|||
237 | 'class' => 'fa fa-picture-o', |
||
238 | ]); |
||
239 | } |
||
240 | |||
241 | private function buildElements(GalleryInterface $gallery): array |
||
242 | { |
||
243 | $elements = []; |
||
244 | foreach ($gallery->getGalleryItems() as $galleryItem) { |
||
245 | if (!$galleryItem->getEnabled()) { |
||
246 | continue; |
||
247 | } |
||
248 | |||
249 | $type = $this->getMediaType($galleryItem->getMedia()); |
||
0 ignored issues
–
show
It seems like
$galleryItem->getMedia() can be null ; however, getMediaType() does not accept null , maybe add an additional type check?
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: /** @return stdClass|null */
function mayReturnNull() { }
function doesNotAcceptNull(stdClass $x) { }
// With potential error.
function withoutCheck() {
$x = mayReturnNull();
doesNotAcceptNull($x); // Potential error here.
}
// Safe - Alternative 1
function withCheck1() {
$x = mayReturnNull();
if ( ! $x instanceof stdClass) {
throw new \LogicException('$x must be defined.');
}
doesNotAcceptNull($x);
}
// Safe - Alternative 2
function withCheck2() {
$x = mayReturnNull();
if ($x instanceof stdClass) {
doesNotAcceptNull($x);
}
}
Loading history...
|
|||
250 | |||
251 | if (null === $type) { |
||
252 | continue; |
||
253 | } |
||
254 | |||
255 | $elements[] = [ |
||
256 | 'title' => $galleryItem->getMedia()->getName(), |
||
257 | 'caption' => $galleryItem->getMedia()->getDescription(), |
||
258 | 'type' => $type, |
||
259 | 'media' => $galleryItem->getMedia(), |
||
260 | ]; |
||
261 | } |
||
262 | |||
263 | return $elements; |
||
264 | } |
||
265 | |||
266 | private function getMediaType(MediaInterface $media): ?string |
||
267 | { |
||
268 | if ('video/x-flv' === $media->getContentType()) { |
||
269 | return 'video'; |
||
270 | } |
||
271 | if ('image' === substr($media->getContentType(), 0, 5)) { |
||
272 | return 'image'; |
||
273 | } |
||
274 | |||
275 | return null; |
||
276 | } |
||
277 | } |
||
278 |
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.