Completed
Push — master ( 1de9b7...830752 )
by Kristof
38:46 queued 24:09
created

AbstractArticlePageAdminListConfigurator   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 181
Duplicated Lines 17.68 %

Coupling/Cohesion

Components 2
Dependencies 10

Importance

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

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 8 8 1
A getBundleName() 0 4 1
A getEntityName() 0 4 1
A buildFilters() 7 7 1
A buildFields() 7 7 1
A getQueryBuilder() 0 8 1
A adaptQueryBuilder() 0 12 1
A getEditUrlFor() 10 10 1
A getDeleteUrlFor() 0 10 1
A getOverviewPage() 0 11 3
A getOverviewPages() 0 4 1
A getListTemplate() 0 4 1
A getEntityClassName() 0 4 1
getOverviewPageRepository() 0 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\ArticleBundle\AdminList;
4
5
use Doctrine\ORM\EntityManager;
6
use Doctrine\ORM\QueryBuilder;
7
use Kunstmaan\AdminBundle\Helper\Security\Acl\AclHelper;
8
use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionDefinition;
9
use Kunstmaan\AdminListBundle\AdminList\Configurator\AbstractDoctrineORMAdminListConfigurator;
10
use Kunstmaan\AdminListBundle\AdminList\FilterType\ORM\BooleanFilterType;
11
use Kunstmaan\AdminListBundle\AdminList\FilterType\ORM\DateFilterType;
12
use Kunstmaan\AdminListBundle\AdminList\FilterType\ORM\StringFilterType;
13
use Kunstmaan\ArticleBundle\Entity\AbstractArticleOverviewPage;
14
use Kunstmaan\NodeBundle\Entity\Node;
15
16
/**
17
 * The AdminList configurator for the AbstractArticlePage
18
 */
19
abstract class AbstractArticlePageAdminListConfigurator extends AbstractDoctrineORMAdminListConfigurator
20
{
21
    /**
22
     * @var string
23
     */
24
    protected $locale;
25
26
    /**
27
     * @var string
28
     */
29
    protected $permission;
30
31
    /**
32
     * @param EntityManager $em         The entity manager
33
     * @param AclHelper     $aclHelper  The ACL helper
34
     * @param string        $locale     The current locale
35
     * @param string        $permission The permission
36
     */
37 View Code Duplication
    public function __construct(EntityManager $em, AclHelper $aclHelper, $locale, $permission)
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...
38
    {
39
        parent::__construct($em, $aclHelper);
40
        $this->locale = $locale;
41
        $this->setPermissionDefinition(
42
            new PermissionDefinition(array($permission), 'Kunstmaan\NodeBundle\Entity\Node', 'n')
43
        );
44
    }
45
46
    /**
47
     * Return current bundle name.
48
     *
49
     * @return string
50
     */
51
    public function getBundleName()
52
    {
53
        return 'KunstmaanArticleBundle';
54
    }
55
56
    /**
57
     * Return current entity name.
58
     *
59
     * @return string
60
     */
61
    public function getEntityName()
62
    {
63
        return 'AbstractArticlePage';
64
    }
65
66
    /**
67
     * Configure filters
68
     */
69 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...
70
    {
71
        $this->addFilter('title', new StringFilterType('title'), 'article.page.list.filter.title')
72
            ->addFilter('online', new BooleanFilterType('online'), 'article.page.list.filter.online')
73
            ->addFilter('created', new DateFilterType('created', 'nv'), 'article.page.list.filter.created_at')
74
            ->addFilter('updated', new DateFilterType('updated', 'nv'), 'article.page.list.filter.updated_at');
75
    }
76
77
    /**
78
     * Configure the visible columns
79
     */
80 View Code Duplication
    public function buildFields()
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...
81
    {
82
        $this->addField('title', 'article.page.list.header.title', true, 'KunstmaanNodeBundle:Admin:title.html.twig')
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
83
            ->addField('created', 'article.page.list.header.created_at', true)
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
84
            ->addField('updated', 'article.page.list.header.updated_at', true)
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
85
            ->addField('online', 'article.page.list.header.online', true, 'KunstmaanNodeBundle:Admin:online.html.twig');
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
86
    }
87
88
    /**
89
     * @return QueryBuilder
90
     */
91
    protected function getQueryBuilder()
92
    {
93
        $queryBuilder = $this->em
94
            ->getRepository('KunstmaanNodeBundle:NodeTranslation')
95
            ->createQueryBuilder('b');
96
97
        return $queryBuilder;
98
    }
99
100
    /**
101
     * @param QueryBuilder $queryBuilder The query builder
102
     */
103
    public function adaptQueryBuilder(QueryBuilder $queryBuilder)
104
    {
105
        parent::adaptQueryBuilder($queryBuilder);
106
107
        $queryBuilder->innerJoin('b.node', 'n', 'WITH', 'b.node = n.id');
108
        $queryBuilder->innerJoin('b.nodeVersions', 'nv', 'WITH', 'b.publicNodeVersion = nv.id');
109
        $queryBuilder->andWhere('b.lang = :lang');
110
        $queryBuilder->andWhere('n.deleted = 0');
111
        $queryBuilder->andWhere('n.refEntityName = :class');
112
        $queryBuilder->addOrderBy('b.updated', 'DESC');
113
        $queryBuilder->setParameter('lang', $this->locale);
114
    }
115
116
    /**
117
     * @param mixed $item
118
     *
119
     * @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...
120
     */
121 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...
122
    {
123
        /* @var Node $node */
124
        $node = $item->getNode();
125
126
        return array(
127
            'path' => 'KunstmaanNodeBundle_nodes_edit',
128
            'params' => array('id' => $node->getId()),
129
        );
130
    }
131
132
    /**
133
     * Get the delete url for the given $item
134
     *
135
     * @param object $item
136
     *
137
     * @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...
138
     */
139
    public function getDeleteUrlFor($item)
140
    {
141
        /* @var Node $node */
142
        $node = $item->getNode();
143
144
        return array(
145
            'path' => 'KunstmaanNodeBundle_nodes_delete',
146
            'params' => array('id' => $node->getId()),
147
        );
148
    }
149
150
    /**
151
     * Returns the OverviewPage of these articles
152
     *
153
     * @return AbstractArticleOverviewPage
154
     */
155
    public function getOverviewPage()
156
    {
157
        $repository = $this->getOverviewPageRepository();
158
        $pages = $repository->findActiveOverviewPages();
159
160
        if (isset($pages) && count($pages) > 0) {
161
            return $pages[0];
162
        }
163
164
        return null;
165
    }
166
167
    /**
168
     * Returns all overview pages
169
     *
170
     * @return mixed
171
     */
172
    public function getOverviewPages()
173
    {
174
        return $this->getOverviewPageRepository()->findActiveOverviewPages();
175
    }
176
177
    /**
178
     * @return string
179
     */
180
    public function getListTemplate()
181
    {
182
        return 'KunstmaanArticleBundle:AbstractArticlePageAdminList:list.html.twig';
183
    }
184
185
    /**
186
     * Returns the full entity class name
187
     *
188
     * @return string
189
     */
190
    public function getEntityClassName()
191
    {
192
        return $this->em->getRepository($this->getRepositoryName())->getClassName();
193
    }
194
195
    /**
196
     * @return \Doctrine\ORM\EntityRepository
197
     */
198
    abstract public function getOverviewPageRepository();
199
}
200