Completed
Push — master ( ae5e03...0447ee )
by Jeroen
10:35 queued 04:37
created

AbstractArticlePageAdminListConfigurator   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 181
Duplicated Lines 13.81 %

Coupling/Cohesion

Components 2
Dependencies 10

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
lcom 2
cbo 10
dl 25
loc 181
ccs 57
cts 57
cp 1
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
getOverviewPageRepository() 0 1 ?
A __construct() 8 8 1
A getBundleName() 0 4 1
A getEntityName() 0 4 1
A buildFilters() 7 7 1
A buildFields() 0 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

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
use Kunstmaan\NodeBundle\Entity\NodeTranslation;
16
17
/**
18
 * The AdminList configurator for the AbstractArticlePage
19
 */
20
abstract class AbstractArticlePageAdminListConfigurator extends AbstractDoctrineORMAdminListConfigurator
21
{
22
    /**
23
     * @var string
24
     */
25
    protected $locale;
26
27
    /**
28
     * @var string
29
     */
30
    protected $permission;
31
32
    /**
33
     * @param EntityManager $em         The entity manager
34
     * @param AclHelper     $aclHelper  The ACL helper
35
     * @param string        $locale     The current locale
36
     * @param string        $permission The permission
37
     */
38 6 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...
39
    {
40 6
        parent::__construct($em, $aclHelper);
41 6
        $this->locale = $locale;
42 6
        $this->setPermissionDefinition(
43 6
            new PermissionDefinition(array($permission), 'Kunstmaan\NodeBundle\Entity\Node', 'n')
44
        );
45 6
    }
46
47
    /**
48
     * Return current bundle name.
49
     *
50
     * @return string
51
     */
52 2
    public function getBundleName()
53
    {
54 2
        return 'KunstmaanArticleBundle';
55
    }
56
57
    /**
58
     * Return current entity name.
59
     *
60
     * @return string
61
     */
62 2
    public function getEntityName()
63
    {
64 2
        return 'AbstractArticlePage';
65
    }
66
67
    /**
68
     * Configure filters
69
     */
70 1 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...
71
    {
72 1
        $this->addFilter('title', new StringFilterType('title'), 'article.page.list.filter.title')
73 1
            ->addFilter('online', new BooleanFilterType('online'), 'article.page.list.filter.online')
74 1
            ->addFilter('created', new DateFilterType('created', 'nv'), 'article.page.list.filter.created_at')
75 1
            ->addFilter('updated', new DateFilterType('updated', 'nv'), 'article.page.list.filter.updated_at');
76 1
    }
77
78
    /**
79
     * Configure the visible columns
80
     */
81 1
    public function buildFields()
82
    {
83 1
        $this->addField('title', 'article.page.list.header.title', true, '@KunstmaanNode/Admin/title.html.twig')
84 1
            ->addField('created', 'article.page.list.header.created_at', true)
85 1
            ->addField('updated', 'article.page.list.header.updated_at', true)
86 1
            ->addField('online', 'article.page.list.header.online', true, '@KunstmaanNode/Admin/online.html.twig');
87 1
    }
88
89
    /**
90
     * @return QueryBuilder
91
     */
92 1
    protected function getQueryBuilder()
93
    {
94 1
        $queryBuilder = $this->em
95 1
            ->getRepository(NodeTranslation::class)
96 1
            ->createQueryBuilder('b');
97
98 1
        return $queryBuilder;
99
    }
100
101
    /**
102
     * @param QueryBuilder $queryBuilder The query builder
103
     */
104 1
    public function adaptQueryBuilder(QueryBuilder $queryBuilder)
105
    {
106 1
        parent::adaptQueryBuilder($queryBuilder);
107
108 1
        $queryBuilder->innerJoin('b.node', 'n', 'WITH', 'b.node = n.id');
109 1
        $queryBuilder->innerJoin('b.nodeVersions', 'nv', 'WITH', 'b.publicNodeVersion = nv.id');
110 1
        $queryBuilder->andWhere('b.lang = :lang');
111 1
        $queryBuilder->andWhere('n.deleted = 0');
112 1
        $queryBuilder->andWhere('n.refEntityName = :class');
113 1
        $queryBuilder->addOrderBy('b.updated', 'DESC');
114 1
        $queryBuilder->setParameter('lang', $this->locale);
115 1
    }
116
117
    /**
118
     * @param mixed $item
119
     *
120
     * @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...
121
     */
122 1 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...
123
    {
124
        /* @var Node $node */
125 1
        $node = $item->getNode();
126
127
        return array(
128 1
            'path' => 'KunstmaanNodeBundle_nodes_edit',
129 1
            'params' => array('id' => $node->getId()),
130
        );
131
    }
132
133
    /**
134
     * Get the delete url for the given $item
135
     *
136
     * @param object $item
137
     *
138
     * @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...
139
     */
140 1
    public function getDeleteUrlFor($item)
141
    {
142
        /* @var Node $node */
143 1
        $node = $item->getNode();
144
145
        return array(
146 1
            'path' => 'KunstmaanNodeBundle_nodes_delete',
147 1
            'params' => array('id' => $node->getId()),
148
        );
149
    }
150
151
    /**
152
     * Returns the OverviewPage of these articles
153
     *
154
     * @return AbstractArticleOverviewPage
155
     */
156 1
    public function getOverviewPage()
157
    {
158 1
        $repository = $this->getOverviewPageRepository();
159 1
        $pages = $repository->findActiveOverviewPages();
160
161 1
        if (isset($pages) && \count($pages) > 0) {
162 1
            return $pages[0];
163
        }
164
165 1
        return null;
166
    }
167
168
    /**
169
     * Returns all overview pages
170
     *
171
     * @return mixed
172
     */
173 1
    public function getOverviewPages()
174
    {
175 1
        return $this->getOverviewPageRepository()->findActiveOverviewPages();
176
    }
177
178
    /**
179
     * @return string
180
     */
181 1
    public function getListTemplate()
182
    {
183 1
        return '@KunstmaanArticle/AbstractArticlePageAdminList/list.html.twig';
184
    }
185
186
    /**
187
     * Returns the full entity class name
188
     *
189
     * @return string
190
     */
191 1
    public function getEntityClassName()
192
    {
193 1
        return $this->em->getRepository($this->getRepositoryName())->getClassName();
194
    }
195
196
    /**
197
     * @return \Doctrine\ORM\EntityRepository
198
     */
199
    abstract public function getOverviewPageRepository();
200
}
201