Completed
Push — 3.x ( 733455...aa9981 )
by Grégoire
02:00
created

CommentAdmin::configureFormFields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 9.424
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\NewsBundle\Admin;
13
14
use Sonata\AdminBundle\Admin\AbstractAdmin;
15
use Sonata\AdminBundle\Datagrid\DatagridMapper;
16
use Sonata\AdminBundle\Datagrid\ListMapper;
17
use Sonata\AdminBundle\Form\FormMapper;
18
use Sonata\AdminBundle\Form\Type\ModelListType;
19
use Sonata\CoreBundle\Model\ManagerInterface;
20
use Sonata\NewsBundle\Form\Type\CommentStatusType;
21
use Sonata\NewsBundle\Model\CommentManagerInterface;
22
use Symfony\Component\Form\Extension\Core\Type\TextType;
23
24
class CommentAdmin extends AbstractAdmin
25
{
26
    /**
27
     * @var CommentManagerInterface
28
     */
29
    protected $commentManager;
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function getBatchActions()
35
    {
36
        $actions = parent::getBatchActions();
37
38
        $actions['enabled'] = [
39
            'label' => $this->getLabelTranslatorStrategy()->getLabel('enable', 'batch', 'comment'),
40
            'translation_domain' => $this->getTranslationDomain(),
41
            'ask_confirmation' => false,
42
        ];
43
44
        $actions['disabled'] = [
45
            'label' => $this->getLabelTranslatorStrategy()->getLabel('disable', 'batch', 'comment'),
46
            'translation_domain' => $this->getTranslationDomain(),
47
            'ask_confirmation' => false,
48
        ];
49
50
        return $actions;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function postPersist($object)
57
    {
58
        $this->updateCountsComment();
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function postRemove($object)
65
    {
66
        $this->updateCountsComment();
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function postUpdate($object)
73
    {
74
        $this->updateCountsComment();
75
    }
76
77
    /**
78
     * @param ManagerInterface $commentManager
79
     */
80
    public function setCommentManager(ManagerInterface $commentManager)
81
    {
82
        if (!$commentManager instanceof CommentManagerInterface) {
83
            @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...
84
                'Calling the '.__METHOD__.' method with a Sonata\CoreBundle\Model\ManagerInterface is deprecated'
85
                .' since version 2.4 and will be removed in 3.0.'
86
                .' Use the new signature with a Sonata\NewsBundle\Model\CommentManagerInterface instead.',
87
                E_USER_DEPRECATED
88
            );
89
        }
90
91
        $this->commentManager = $commentManager;
0 ignored issues
show
Documentation Bug introduced by
$commentManager is of type object<Sonata\CoreBundle\Model\ManagerInterface>, but the property $commentManager 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...
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    protected function configureFormFields(FormMapper $formMapper)
98
    {
99
        // define group zoning
100
        $formMapper
101
            ->with('group_comment', ['class' => 'col-md-6'])->end()
102
            ->with('group_general', ['class' => 'col-md-6'])->end()
103
        ;
104
105
        if (!$this->isChild()) {
106
            $formMapper
107
                ->with('group_general')
108
                    ->add('post', ModelListType::class)
109
                ->end()
110
            ;
111
        }
112
113
        $formMapper
114
            ->with('group_general')
115
                ->add('name')
116
                ->add('email')
117
                ->add('url', null, ['required' => false])
118
            ->end()
119
            ->with('group_comment')
120
                ->add('status', CommentStatusType::class, [
121
                    'expanded' => true,
122
                    'multiple' => false,
123
                ])
124
                ->add('message', null, ['attr' => ['rows' => 6]])
125
            ->end()
126
        ;
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
133
    {
134
        $datagridMapper
135
            ->add('name')
136
            ->add('email')
137
            ->add('message')
138
        ;
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    protected function configureListFields(ListMapper $listMapper)
145
    {
146
        $listMapper
147
            ->addIdentifier('name')
148
            ->add('getStatusCode', TextType::class, ['label' => 'status_code', 'sortable' => 'status'])
149
        ;
150
151
        if (!$this->isChild()) {
152
            $listMapper->add('post');
153
        }
154
155
        $listMapper
156
            ->add('email')
157
            ->add('url')
158
            ->add('message');
159
    }
160
161
    /**
162
     * Update the count comment.
163
     */
164
    private function updateCountsComment()
165
    {
166
        $this->commentManager->updateCommentsCount();
167
    }
168
}
169