NodeAdminListConfigurator   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 241
Duplicated Lines 12.86 %

Coupling/Cohesion

Components 3
Dependencies 11

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 20
lcom 3
cbo 11
dl 31
loc 241
ccs 0
cts 125
cp 0
rs 10
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 13 13 1
A setDomainConfiguration() 0 4 1
A setShowAddHomepage() 0 4 1
A buildListActions() 0 26 2
A buildFilters() 8 8 1
A getEditUrlFor() 10 10 1
A canAdd() 0 4 1
A canEdit() 0 4 1
A canDelete() 0 4 1
A getDeleteUrlFor() 0 4 1
A getBundleName() 0 4 1
A getEntityName() 0 4 1
A getPathByConvention() 0 8 2
A getControllerPath() 0 4 1
A adaptQueryBuilder() 0 24 3
A buildFields() 0 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Kunstmaan\NodeBundle\AdminList;
4
5
use Doctrine\ORM\EntityManager;
6
use Doctrine\ORM\QueryBuilder;
7
use Kunstmaan\AdminBundle\Helper\DomainConfigurationInterface;
8
use Kunstmaan\AdminBundle\Helper\Security\Acl\AclHelper;
9
use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionDefinition;
10
use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionMap;
11
use Kunstmaan\AdminListBundle\AdminList\Configurator\AbstractDoctrineORMAdminListConfigurator;
12
use Kunstmaan\AdminListBundle\AdminList\FilterType\ORM\BooleanFilterType;
13
use Kunstmaan\AdminListBundle\AdminList\FilterType\ORM\DateFilterType;
14
use Kunstmaan\AdminListBundle\AdminList\FilterType\ORM\StringFilterType;
15
use Kunstmaan\AdminListBundle\AdminList\ListAction\SimpleListAction;
16
use Kunstmaan\NodeBundle\Entity\Node;
17
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
18
19
/**
20
 * NodeAdminListConfigurator
21
 */
22
class NodeAdminListConfigurator extends AbstractDoctrineORMAdminListConfigurator
23
{
24
    /**
25
     * @var string
26
     */
27
    protected $locale;
28
29
    /**
30
     * @var string
31
     */
32
    protected $permission;
33
34
    /**
35
     * @var DomainConfigurationInterface
36
     */
37
    protected $domainConfiguration;
38
39
    /**
40
     * @var bool
41
     */
42
    protected $showAddHomepage;
43
44
    /**
45
     * @var AuthorizationCheckerInterface
46
     */
47
    protected $authorizationChecker;
48
49
    /**
50
     * @param EntityManager $em         The entity
51
     *                                  manager
52
     * @param AclHelper     $aclHelper  The ACL helper
53
     * @param string        $locale     The current
54
     *                                  locale
55
     * @param string        $permission The permission
56
     */
57 View Code Duplication
    public function __construct(EntityManager $em, AclHelper $aclHelper, $locale, $permission, AuthorizationCheckerInterface $authorizationChecker)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
    {
59
        parent::__construct($em, $aclHelper);
60
        $this->locale = $locale;
61
        $this->authorizationChecker = $authorizationChecker;
62
        $this->setPermissionDefinition(
63
            new PermissionDefinition(
64
                array($permission),
65
                'Kunstmaan\NodeBundle\Entity\Node',
66
                'n'
67
            )
68
        );
69
    }
70
71
    /**
72
     * @param \Kunstmaan\AdminBundle\Helper\DomainConfigurationInterface $domainConfiguration
73
     */
74
    public function setDomainConfiguration(DomainConfigurationInterface $domainConfiguration)
75
    {
76
        $this->domainConfiguration = $domainConfiguration;
77
    }
78
79
    /**
80
     * @param bool $showAddHomepage
81
     */
82
    public function setShowAddHomepage($showAddHomepage)
83
    {
84
        $this->showAddHomepage = $showAddHomepage;
85
    }
86
87
    /**
88
     * Build list actions ...
89
     */
90
    public function buildListActions()
91
    {
92
        if (!$this->showAddHomepage) {
93
            return;
94
        }
95
96
        $addHomepageRoute = array(
97
            'path' => '',
98
            'attributes' => array(
99
                'class' => 'btn btn-default btn--raise-on-hover',
100
                'data-target' => '#add-homepage-modal',
101
                'data-keyboard' => 'true',
102
                'data-toggle' => 'modal',
103
                'type' => 'button',
104
            ),
105
        );
106
107
        $this->addListAction(
108
            new SimpleListAction(
109
                $addHomepageRoute,
110
                'kuma_node.modal.add_homepage.h',
111
                null,
112
                '@KunstmaanNode/Admin/list_action_button.html.twig'
113
            )
114
        );
115
    }
116
117
    /**
118
     * Configure filters
119
     */
120 View Code Duplication
    public function buildFilters()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
121
    {
122
        $this
123
            ->addFilter('title', new StringFilterType('title'), 'kuma_node.admin.list.filter.title')
124
            ->addFilter('created', new DateFilterType('created'), 'kuma_node.admin.list.filter.created_at')
125
            ->addFilter('updated', new DateFilterType('updated'), 'kuma_node.admin.list.filter.updated_at')
126
            ->addFilter('online', new BooleanFilterType('online'), 'kuma_node.admin.list.filter.online');
127
    }
128
129
    /**
130
     * Configure the visible columns
131
     */
132
    public function buildFields()
133
    {
134
        $this
135
            ->addField('title', 'kuma_node.admin.list.header.title', true, '@KunstmaanNode/Admin/title.html.twig')
136
            ->addField('created', 'kuma_node.admin.list.header.created_at', true)
137
            ->addField('updated', 'kuma_node.admin.list.header.updated_at', true)
138
            ->addField('online', 'kuma_node.admin.list.header.online', true, '@KunstmaanNode/Admin/online.html.twig');
139
    }
140
141
    /**
142
     * @param mixed $item
143
     *
144
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,string|array<string,integer>>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
145
     */
146 View Code Duplication
    public function getEditUrlFor($item)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
147
    {
148
        /* @var Node $node */
149
        $node = $item->getNode();
150
151
        return array(
152
            'path' => 'KunstmaanNodeBundle_nodes_edit',
153
            'params' => array('id' => $node->getId()),
154
        );
155
    }
156
157
    /**
158
     * @return bool
159
     */
160
    public function canAdd()
161
    {
162
        return false;
163
    }
164
165
    public function canEdit($item)
166
    {
167
        return $this->authorizationChecker->isGranted(PermissionMap::PERMISSION_EDIT, $item->getNode());
168
    }
169
170
    /**
171
     * Return if current user can delete the specified item
172
     *
173
     * @param array|object $item
174
     *
175
     * @return bool
176
     */
177
    public function canDelete($item)
178
    {
179
        return false;
180
    }
181
182
    /**
183
     * @param object $item
184
     *
185
     * @return array
186
     */
187
    public function getDeleteUrlFor($item)
188
    {
189
        return array();
190
    }
191
192
    /**
193
     * @return string
194
     */
195
    public function getBundleName()
196
    {
197
        return 'KunstmaanNodeBundle';
198
    }
199
200
    /**
201
     * @return string
202
     */
203
    public function getEntityName()
204
    {
205
        return 'NodeTranslation';
206
    }
207
208
    /**
209
     * Override path convention (because settings is a virtual admin subtree)
210
     *
211
     * @param string $suffix
0 ignored issues
show
Documentation introduced by
Should the type for parameter $suffix not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
212
     *
213
     * @return string
214
     */
215
    public function getPathByConvention($suffix = null)
216
    {
217
        if (empty($suffix)) {
218
            return sprintf('%s_nodes', $this->getBundleName());
219
        }
220
221
        return sprintf('%s_nodes_%s', $this->getBundleName(), $suffix);
222
    }
223
224
    /**
225
     * Override controller path (because actions for different entities are
226
     * defined in a single Settings controller).
227
     *
228
     * @return string
229
     */
230
    public function getControllerPath()
231
    {
232
        return 'KunstmaanNodeBundle:NodeAdmin';
233
    }
234
235
    /**
236
     * @param QueryBuilder $queryBuilder The query builder
237
     */
238
    public function adaptQueryBuilder(QueryBuilder $queryBuilder)
239
    {
240
        parent::adaptQueryBuilder($queryBuilder);
241
242
        $queryBuilder
243
            ->select('b,n')
244
            ->innerJoin('b.node', 'n', 'WITH', 'b.node = n.id')
245
            ->andWhere('b.lang = :lang')
246
            ->andWhere('n.deleted = 0')
247
            ->addOrderBy('b.updated', 'DESC')
248
            ->setParameter('lang', $this->locale);
249
250
        if (!$this->domainConfiguration) {
251
            return;
252
        }
253
254
        $rootNode = $this->domainConfiguration->getRootNode();
255
        if (!\is_null($rootNode)) {
256
            $queryBuilder->andWhere('n.lft >= :left')
257
                ->andWhere('n.rgt <= :right')
258
                ->setParameter('left', $rootNode->getLeft())
259
                ->setParameter('right', $rootNode->getRight());
260
        }
261
    }
262
}
263