Completed
Push — master ( a3f6a1...e8d2d6 )
by
unknown
01:57
created

RecentPostsBlockService::buildEditForm()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 24
nc 1
nop 2
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\NewsBundle\Block;
15
16
use Sonata\AdminBundle\Admin\Pool;
17
use Sonata\AdminBundle\Form\FormMapper;
18
use Sonata\BlockBundle\Block\BlockContextInterface;
19
use Sonata\BlockBundle\Block\Service\AbstractAdminBlockService;
20
use Sonata\BlockBundle\Model\BlockInterface;
21
use Sonata\CoreBundle\Form\Type\ImmutableArrayType;
22
use Sonata\CoreBundle\Model\ManagerInterface;
23
use Sonata\CoreBundle\Model\Metadata;
24
use Sonata\NewsBundle\Model\PostManagerInterface;
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
32
/**
33
 * @author Thomas Rabaix <[email protected]>
34
 */
35
class RecentPostsBlockService extends AbstractAdminBlockService
36
{
37
    /**
38
     * @var PostManagerInterface
39
     */
40
    protected $manager;
41
42
    /**
43
     * @var Pool
44
     */
45
    private $adminPool;
46
47
    /**
48
     * @param string           $name
49
     * @param EngineInterface  $templating
50
     * @param ManagerInterface $postManager
51
     * @param Pool             $adminPool
52
     */
53
    public function __construct($name, EngineInterface $templating, ManagerInterface $postManager, Pool $adminPool = null)
54
    {
55
        if (!$postManager instanceof PostManagerInterface) {
56
            @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
57
                'Calling the '.__METHOD__.' method with a Sonata\CoreBundle\Model\ManagerInterface is deprecated'
58
                .' since version 2.4 and will be removed in 3.0.'
59
                .' Use the new signature with a Sonata\NewsBundle\Model\PostManagerInterface instead.',
60
                E_USER_DEPRECATED
61
            );
62
        }
63
64
        $this->manager = $postManager;
0 ignored issues
show
Documentation Bug introduced by
$postManager is of type object<Sonata\CoreBundle\Model\ManagerInterface>, but the property $manager was declared to be of type object<Sonata\NewsBundle...l\PostManagerInterface>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
65
        $this->adminPool = $adminPool;
66
67
        parent::__construct($name, $templating);
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function execute(BlockContextInterface $blockContext, Response $response = null)
74
    {
75
        $criteria = [
76
            'mode' => $blockContext->getSetting('mode'),
77
        ];
78
79
        $parameters = [
80
            'context' => $blockContext,
81
            'settings' => $blockContext->getSettings(),
82
            'block' => $blockContext->getBlock(),
83
            'pager' => $this->manager->getPager($criteria, 1, $blockContext->getSetting('number')),
84
            'admin_pool' => $this->adminPool,
85
        ];
86
87
        if ('admin' === $blockContext->getSetting('mode')) {
88
            return $this->renderPrivateResponse($blockContext->getTemplate(), $parameters, $response);
89
        }
90
91
        return $this->renderResponse($blockContext->getTemplate(), $parameters, $response);
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function buildEditForm(FormMapper $formMapper, BlockInterface $block): void
98
    {
99
        $formMapper->add('settings', ImmutableArrayType::class, [
100
            'keys' => [
101
                ['number', IntegerType::class, [
102
                    'required' => true,
103
                    'label' => 'form.label_number',
104
                ]],
105
                ['title', TextType::class, [
106
                    'label' => 'form.label_title',
107
                    'required' => false,
108
                ]],
109
                ['translation_domain', TextType::class, [
110
                    'label' => 'form.label_translation_domain',
111
                    'required' => false,
112
                ]],
113
                ['icon', TextType::class, [
114
                    'label' => 'form.label_icon',
115
                    'required' => false,
116
                ]],
117
                ['class', TextType::class, [
118
                    'label' => 'form.label_class',
119
                    'required' => false,
120
                ]],
121
                ['mode', ChoiceType::class, [
122
                    'choices' => [
123
                        'public' => 'form.label_mode_public',
124
                        'admin' => 'form.label_mode_admin',
125
                    ],
126
                    'label' => 'form.label_mode',
127
                ]],
128
            ],
129
            'translation_domain' => 'SonataNewsBundle',
130
        ]);
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136
    public function configureSettings(OptionsResolver $resolver): void
137
    {
138
        $resolver->setDefaults([
139
            'number' => 5,
140
            'mode' => 'public',
141
            'title' => null,
142
            'translation_domain' => null,
143
            'icon' => 'fa fa-pencil',
144
            'class' => null,
145
            'template' => '@SonataNews/Block/recent_posts.html.twig',
146
        ]);
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152
    public function getBlockMetadata($code = null)
153
    {
154
        return new Metadata($this->getName(), (null !== $code ? $code : $this->getName()), false, 'SonataNewsBundle', [
155
            'class' => 'fa fa-pencil',
156
        ]);
157
    }
158
}
159