GalleryListBlockService   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 223
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Importance

Changes 0
Metric Value
wmc 15
lcom 2
cbo 8
dl 0
loc 223
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B buildEditForm() 0 61 2
A execute() 0 25 1
A configureSettings() 0 15 1
A getBlockMetadata() 0 6 2
A buildCreateForm() 0 4 1
A prePersist() 0 3 1
A postPersist() 0 3 1
A preUpdate() 0 3 1
A postUpdate() 0 3 1
A preRemove() 0 3 1
A postRemove() 0 3 1
A validateBlock() 0 3 1
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 Sonata\AdminBundle\Form\FormMapper;
17
use Sonata\BlockBundle\Block\BlockContextInterface;
18
use Sonata\BlockBundle\Block\Service\AbstractBlockService;
19
use Sonata\BlockBundle\Meta\Metadata;
20
use Sonata\BlockBundle\Model\BlockInterface;
21
use Sonata\Form\Type\ImmutableArrayType;
22
use Sonata\Form\Validator\ErrorElement;
23
use Sonata\MediaBundle\Model\GalleryManagerInterface;
24
use Sonata\MediaBundle\Provider\Pool;
25
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
26
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
27
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
28
use Symfony\Component\Form\Extension\Core\Type\TextType;
29
use Symfony\Component\HttpFoundation\Response;
30
use Symfony\Component\OptionsResolver\OptionsResolver;
31
use Twig\Environment;
32
33
/**
34
 * @final since sonata-project/media-bundle 3.21.0
35
 */
36
class GalleryListBlockService extends AbstractBlockService
37
{
38
    /**
39
     * @var GalleryManagerInterface
40
     */
41
    protected $galleryManager;
42
43
    /**
44
     * @var Pool
45
     */
46
    protected $pool;
47
48
    /**
49
     * NEXT_MAJOR: Remove `$templating` argument.
50
     *
51
     * @param Environment|string $twigOrName
52
     */
53
    public function __construct($twigOrName, ?EngineInterface $templating, GalleryManagerInterface $galleryManager, Pool $pool)
54
    {
55
        parent::__construct($twigOrName, $templating);
56
57
        $this->galleryManager = $galleryManager;
58
        $this->pool = $pool;
59
    }
60
61
    /**
62
     * NEXT_MAJOR: Remove this method.
63
     *
64
     * @deprecated since sonata-project/media-bundle 3.25, to be removed in 4.0. You should use
65
     *             `Sonata\BlockBundle\Block\Service\EditableBlockService` interface instead.
66
     */
67
    public function buildEditForm(FormMapper $formMapper, BlockInterface $block): void
0 ignored issues
show
Unused Code introduced by
The parameter $block is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
68
    {
69
        $contextChoices = [];
70
71
        foreach ($this->pool->getContexts() as $name => $context) {
72
            $contextChoices[$name] = $name;
73
        }
74
75
        $formMapper->add('settings', ImmutableArrayType::class, [
76
            'keys' => [
77
                ['title', TextType::class, [
78
                    'label' => 'form.label_title',
79
                    'required' => false,
80
                ]],
81
                ['translation_domain', TextType::class, [
82
                    'label' => 'form.label_translation_domain',
83
                    'required' => false,
84
                ]],
85
                ['icon', TextType::class, [
86
                    'label' => 'form.label_icon',
87
                    'required' => false,
88
                ]],
89
                ['class', TextType::class, [
90
                    'label' => 'form.label_class',
91
                    'required' => false,
92
                ]],
93
                ['number', IntegerType::class, [
94
                    'label' => 'form.label_number',
95
                    'required' => true,
96
                ]],
97
                ['context', ChoiceType::class, [
98
                    'required' => true,
99
                    'label' => 'form.label_context',
100
                    'choices' => $contextChoices,
101
                ]],
102
                ['mode', ChoiceType::class, [
103
                    'label' => 'form.label_mode',
104
                    'choices' => [
105
                        'public' => 'form.label_mode_public',
106
                        'admin' => 'form.label_mode_admin',
107
                    ],
108
                ]],
109
                ['order', ChoiceType::class,  [
110
                    'label' => 'form.label_order',
111
                    'choices' => [
112
                        'name' => 'form.label_order_name',
113
                        'createdAt' => 'form.label_order_created_at',
114
                        'updatedAt' => 'form.label_order_updated_at',
115
                    ],
116
                ]],
117
                ['sort', ChoiceType::class, [
118
                    'label' => 'form.label_sort',
119
                    'choices' => [
120
                        'desc' => 'form.label_sort_desc',
121
                        'asc' => 'form.label_sort_asc',
122
                    ],
123
                ]],
124
            ],
125
            'translation_domain' => 'SonataMediaBundle',
126
        ]);
127
    }
128
129
    public function execute(BlockContextInterface $blockContext, ?Response $response = null)
130
    {
131
        $context = $blockContext->getBlock()->getSetting('context');
132
133
        $criteria = [
134
            'mode' => $blockContext->getSetting('mode'),
135
            'context' => $context,
136
        ];
137
138
        $order = [
139
            $blockContext->getSetting('order') => $blockContext->getSetting('sort'),
140
        ];
141
142
        return $this->renderResponse($blockContext->getTemplate(), [
143
            'context' => $blockContext,
144
            'settings' => $blockContext->getSettings(),
145
            'block' => $blockContext->getBlock(),
146
            'pager' => $this->galleryManager->getPager(
147
                $criteria,
148
                1,
149
                $blockContext->getSetting('number'),
150
                $order
151
            ),
152
        ], $response);
153
    }
154
155
    public function configureSettings(OptionsResolver $resolver): void
156
    {
157
        $resolver->setDefaults([
158
            'number' => 15,
159
            'mode' => 'public',
160
            'order' => 'createdAt',
161
            'sort' => 'desc',
162
            'context' => false,
163
            'title' => null,
164
            'translation_domain' => null,
165
            'icon' => 'fa fa-images',
166
            'class' => null,
167
            'template' => '@SonataMedia/Block/block_gallery_list.html.twig',
168
        ]);
169
    }
170
171
    /**
172
     * NEXT_MAJOR: Remove this method.
173
     *
174
     * @deprecated since sonata-project/media-bundle 3.25, to be removed in 4.0. You should use
175
     *             `Sonata\BlockBundle\Block\Service\EditableBlockService` interface instead.
176
     */
177
    public function getBlockMetadata($code = null)
178
    {
179
        return new Metadata($this->getName(), (null !== $code ? $code : $this->getName()), false, 'SonataMediaBundle', [
180
            'class' => 'fa fa-picture-o',
181
        ]);
182
    }
183
184
    /**
185
     * NEXT_MAJOR: Remove this method.
186
     *
187
     * @deprecated since sonata-project/media-bundle 3.25, to be removed in 4.0. You should use
188
     *             `Sonata\BlockBundle\Block\Service\EditableBlockService` interface instead.
189
     */
190
    public function buildCreateForm(FormMapper $formMapper, BlockInterface $block)
191
    {
192
        $this->buildEditForm($formMapper, $block);
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\MediaBundle\Block...ervice::buildEditForm() has been deprecated with message: since sonata-project/media-bundle 3.25, to be removed in 4.0. You should use `Sonata\BlockBundle\Block\Service\EditableBlockService` interface instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

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

Loading history...
193
    }
194
195
    /**
196
     * NEXT_MAJOR: Remove this method.
197
     *
198
     * @deprecated since sonata-project/media-bundle 3.25, to be removed in 4.0.
199
     */
200
    public function prePersist(BlockInterface $block)
0 ignored issues
show
Unused Code introduced by
The parameter $block is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
201
    {
202
    }
203
204
    /**
205
     * NEXT_MAJOR: Remove this method.
206
     *
207
     * @deprecated since sonata-project/media-bundle 3.25, to be removed in 4.0.
208
     */
209
    public function postPersist(BlockInterface $block)
0 ignored issues
show
Unused Code introduced by
The parameter $block is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
210
    {
211
    }
212
213
    /**
214
     * NEXT_MAJOR: Remove this method.
215
     *
216
     * @deprecated since sonata-project/media-bundle 3.25, to be removed in 4.0.
217
     */
218
    public function preUpdate(BlockInterface $block)
0 ignored issues
show
Unused Code introduced by
The parameter $block is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
219
    {
220
    }
221
222
    /**
223
     * NEXT_MAJOR: Remove this method.
224
     *
225
     * @deprecated since sonata-project/media-bundle 3.25, to be removed in 4.0.
226
     */
227
    public function postUpdate(BlockInterface $block)
0 ignored issues
show
Unused Code introduced by
The parameter $block is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
228
    {
229
    }
230
231
    /**
232
     * NEXT_MAJOR: Remove this method.
233
     *
234
     * @deprecated since sonata-project/media-bundle 3.25, to be removed in 4.0.
235
     */
236
    public function preRemove(BlockInterface $block)
0 ignored issues
show
Unused Code introduced by
The parameter $block is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
237
    {
238
    }
239
240
    /**
241
     * NEXT_MAJOR: Remove this method.
242
     *
243
     * @deprecated since sonata-project/media-bundle 3.25, to be removed in 4.0.
244
     */
245
    public function postRemove(BlockInterface $block)
0 ignored issues
show
Unused Code introduced by
The parameter $block is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
246
    {
247
    }
248
249
    /**
250
     * NEXT_MAJOR: Remove this method.
251
     *
252
     * @deprecated since sonata-project/media-bundle 3.25, to be removed in 4.0. You should use
253
     *             `Sonata\BlockBundle\Block\Service\EditableBlockService` interface instead.
254
     */
255
    public function validateBlock(ErrorElement $errorElement, BlockInterface $block)
0 ignored issues
show
Unused Code introduced by
The parameter $errorElement is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $block is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
256
    {
257
    }
258
}
259