RecentCommentsBlockService   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 6
dl 0
loc 110
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 2
A execute() 0 20 2
A buildEditForm() 0 35 1
A configureSettings() 0 12 1
A getBlockMetadata() 0 6 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\Meta\Metadata;
21
use Sonata\BlockBundle\Model\BlockInterface;
22
use Sonata\Doctrine\Model\ManagerInterface;
23
use Sonata\Form\Type\ImmutableArrayType;
24
use Sonata\NewsBundle\Model\CommentManagerInterface;
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 RecentCommentsBlockService extends AbstractAdminBlockService
0 ignored issues
show
Deprecated Code introduced by
The class Sonata\BlockBundle\Block...stractAdminBlockService has been deprecated with message: since sonata-project/block-bundle 3.16 without any replacement

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.

Loading history...
36
{
37
    /**
38
     * @var CommentManagerInterface
39
     */
40
    protected $manager;
41
42
    /**
43
     * @var Pool
44
     */
45
    protected $adminPool;
46
47
    /**
48
     * @param string $name
49
     * @param Pool   $adminPool
50
     */
51
    public function __construct($name, EngineInterface $templating, ManagerInterface $commentManager, ?Pool $adminPool = null)
52
    {
53
        if (!$commentManager instanceof CommentManagerInterface) {
54
            @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...
55
                'Calling the '.__METHOD__.' method with a Sonata\Doctrine\Model\ManagerInterface is deprecated'
56
                .' since version 2.4 and will be removed in 3.0.'
57
                .' Use the new signature with a Sonata\NewsBundle\Model\CommentManagerInterface instead.',
58
                E_USER_DEPRECATED
59
            );
60
        }
61
62
        $this->manager = $commentManager;
0 ignored issues
show
Documentation Bug introduced by
$commentManager is of type object<Sonata\Doctrine\Model\ManagerInterface>, but the property $manager was declared to be of type object<Sonata\NewsBundle...ommentManagerInterface>. 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...
63
        $this->adminPool = $adminPool;
64
65
        parent::__construct($name, $templating);
66
    }
67
68
    public function execute(BlockContextInterface $blockContext, ?Response $response = null)
69
    {
70
        $criteria = [
71
            'mode' => $blockContext->getSetting('mode'),
72
        ];
73
74
        $parameters = [
75
            'context' => $blockContext,
76
            'settings' => $blockContext->getSettings(),
77
            'block' => $blockContext->getBlock(),
78
            'pager' => $this->manager->getPager($criteria, 1, $blockContext->getSetting('number')),
79
            'admin_pool' => $this->adminPool,
80
        ];
81
82
        if ('admin' === $blockContext->getSetting('mode')) {
83
            return $this->renderPrivateResponse($blockContext->getTemplate(), $parameters, $response);
84
        }
85
86
        return $this->renderResponse($blockContext->getTemplate(), $parameters, $response);
87
    }
88
89
    public function buildEditForm(FormMapper $formMapper, BlockInterface $block): void
90
    {
91
        $formMapper->add('settings', ImmutableArrayType::class, [
92
            'keys' => [
93
                ['number', IntegerType::class, [
94
                    'required' => true,
95
                    'label' => 'form.label_number',
96
                ]],
97
                ['title', TextType::class, [
98
                    'label' => 'form.label_title',
99
                    'required' => false,
100
                ]],
101
                ['translation_domain', TextType::class, [
102
                    'label' => 'form.label_translation_domain',
103
                    'required' => false,
104
                ]],
105
                ['icon', TextType::class, [
106
                    'label' => 'form.label_icon',
107
                    'required' => false,
108
                ]],
109
                ['class', TextType::class, [
110
                    'label' => 'form.label_class',
111
                    'required' => false,
112
                ]],
113
                ['mode', ChoiceType::class, [
114
                    'choices' => [
115
                        'form.label_mode_public' => 'public',
116
                        'form.label_mode_admin' => 'admin',
117
                    ],
118
                    'label' => 'form.label_mode',
119
                ]],
120
            ],
121
            'translation_domain' => 'SonataNewsBundle',
122
        ]);
123
    }
124
125
    public function configureSettings(OptionsResolver $resolver): void
126
    {
127
        $resolver->setDefaults([
128
            'number' => 5,
129
            'mode' => 'public',
130
            'title' => null,
131
            'translation_domain' => null,
132
            'icon' => 'fa fa-comments',
133
            'class' => null,
134
            'template' => '@SonataNews/Block/recent_comments.html.twig',
135
        ]);
136
    }
137
138
    public function getBlockMetadata($code = null)
139
    {
140
        return new Metadata($this->getName(), (null !== $code ? $code : $this->getName()), false, 'SonataNewsBundle', [
141
            'class' => 'fa fa-comments-o',
142
        ]);
143
    }
144
}
145