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

AbstractDoctrineORMAdminListConfigurator.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\AdminListBundle\AdminList\Configurator;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Doctrine\ORM\Query;
7
use Doctrine\ORM\QueryBuilder;
8
use Kunstmaan\AdminBundle\Helper\Security\Acl\AclHelper;
9
use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionDefinition;
10
use Kunstmaan\AdminListBundle\AdminList\Filter;
11
use Kunstmaan\AdminListBundle\AdminList\FilterType\ORM\AbstractORMFilterType;
12
use Kunstmaan\AdminListBundle\AdminList\SortableInterface;
13
use Pagerfanta\Adapter\DoctrineORMAdapter;
14
use Pagerfanta\Pagerfanta;
15
use Traversable;
16
17
/**
18
 * An abstract admin list configurator that can be used with the orm query builder
19
 */
20
abstract class AbstractDoctrineORMAdminListConfigurator extends AbstractAdminListConfigurator
21
{
22
    /**
23
     * @var EntityManagerInterface
24
     */
25
    protected $em;
26
27
    /**
28
     * @var Query
29
     */
30
    private $query;
31
32
    /**
33
     * @var Pagerfanta
34
     */
35
    private $pagerfanta;
36
37
    /**
38
     * @var PermissionDefinition
39
     */
40
    private $permissionDef;
41
42
    /**
43
     * @var AclHelper
44
     */
45
    protected $aclHelper;
46
47
    /**
48
     * AbstractDoctrineORMAdminListConfigurator constructor.
49
     *
50
     * @param EntityManagerInterface $em
51
     * @param AclHelper|null         $aclHelper
52
     */
53 31
    public function __construct(EntityManagerInterface $em, AclHelper $aclHelper = null)
54
    {
55 31
        $this->em = $em;
56 31
        $this->aclHelper = $aclHelper;
57 31
    }
58
59
    /**
60
     * Return the url to edit the given $item
61
     *
62
     * @param object $item
63
     *
64
     * @return array
65
     */
66 1 View Code Duplication
    public function getEditUrlFor($item)
67
    {
68 1
        $params = array('id' => $item->getId());
69 1
        $params = array_merge($params, $this->getExtraParameters());
70
71
        return array(
72 1
            'path' => $this->getPathByConvention($this::SUFFIX_EDIT),
73 1
            'params' => $params,
74
        );
75
    }
76
77
    /**
78
     * Get the delete url for the given $item
79
     *
80
     * @param object $item
81
     *
82
     * @return array
83
     */
84 1 View Code Duplication
    public function getDeleteUrlFor($item)
85
    {
86 1
        $params = array('id' => $item->getId());
87 1
        $params = array_merge($params, $this->getExtraParameters());
88
89
        return array(
90 1
            'path' => $this->getPathByConvention($this::SUFFIX_DELETE),
91 1
            'params' => $params,
92
        );
93
    }
94
95
    /**
96
     * @return Pagerfanta
97
     */
98 1 View Code Duplication
    public function getPagerfanta()
99
    {
100 1
        if (\is_null($this->pagerfanta)) {
101 1
            $adapter = new DoctrineORMAdapter($this->getQuery());
0 ignored issues
show
It seems like $this->getQuery() can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
102 1
            $this->pagerfanta = new Pagerfanta($adapter);
103 1
            $this->pagerfanta->setNormalizeOutOfRangePages(true);
104 1
            $this->pagerfanta->setMaxPerPage($this->getLimit());
105 1
            $this->pagerfanta->setCurrentPage($this->getPage());
106
        }
107
108 1
        return $this->pagerfanta;
109
    }
110
111
    /**
112
     * @param QueryBuilder $queryBuilder
113
     */
114 8
    public function adaptQueryBuilder(QueryBuilder $queryBuilder)
115
    {
116 8
        $queryBuilder->where('1=1');
117 8
    }
118
119
    /**
120
     * @return int
121
     */
122
    public function getCount()
123
    {
124
        return $this->getPagerfanta()->getNbResults();
125
    }
126
127
    /**
128
     * @return array|Traversable
129
     */
130
    public function getItems()
131
    {
132
        return $this->getPagerfanta()->getCurrentPageResults();
133
    }
134
135
    /**
136
     * Return an iterator for all items that matches the current filtering
137
     *
138
     * @return \Iterator
139
     */
140 1
    public function getIterator()
141
    {
142 1
        return $this->getQuery()->iterate();
143
    }
144
145
    /**
146
     * @return Query|null
147
     */
148 5
    public function getQuery()
149
    {
150 5
        if (\is_null($this->query)) {
151 5
            $queryBuilder = $this->getQueryBuilder();
152 5
            $this->adaptQueryBuilder($queryBuilder);
153
154
            // Apply filters
155 5
            $filters = $this->getFilterBuilder()->getCurrentFilters();
156
            /* @var Filter $filter */
157 5
            foreach ($filters as $filter) {
158
                /* @var AbstractORMFilterType $type */
159 1
                $type = $filter->getType();
160 1
                $type->setQueryBuilder($queryBuilder);
161 1
                $filter->apply();
162
            }
163
164
            // Apply sorting
165 5
            if (!empty($this->orderBy)) {
166 1
                $orderBy = $this->orderBy;
167 1
                if ($this->getEntityManager()->getClassMetadata($this->getRepositoryName())->hasAssociation($this->orderBy)) {
168
                    $queryBuilder->leftJoin('b.' . $orderBy, 'A' . $orderBy);
169
                    $orderBy = 'A' . $orderBy . '.id';
170 1
                } elseif (!strpos($orderBy, '.')) {
171 1
                    $orderBy = 'b.' . $orderBy;
172
                }
173 1
                $queryBuilder->orderBy($orderBy, ($this->orderDirection == 'DESC' ? 'DESC' : 'ASC'));
174
            }
175
176
            // Apply other changes
177 5
            $this->finishQueryBuilder($queryBuilder);
178
179
            // Apply ACL restrictions (if applicable)
180 5
            if (!\is_null($this->permissionDef) && !\is_null($this->aclHelper)) {
181
                $this->query = $this->aclHelper->apply($queryBuilder, $this->permissionDef);
182
            } else {
183 5
                $this->query = $queryBuilder->getQuery();
184
            }
185
        }
186
187 5
        return $this->query;
188
    }
189
190
    /**
191
     * @param QueryBuilder $queryBuilder
192
     */
193 5
    protected function finishQueryBuilder(QueryBuilder $queryBuilder)
194
    {
195 5
        if ($this instanceof SortableInterface) {
196
            $queryBuilder->addOrderBy('b.' . $this->getSortableField());
197
        }
198 5
    }
199
200
    /**
201
     * @return QueryBuilder
202
     */
203 5
    protected function getQueryBuilder()
204
    {
205 5
        $queryBuilder = $this->em
206 5
            ->getRepository($this->getRepositoryName())
207 5
            ->createQueryBuilder('b');
208
209 5
        return $queryBuilder;
210
    }
211
212
    /**
213
     * Get current permission definition.
214
     *
215
     * @return PermissionDefinition|null
216
     */
217 1
    public function getPermissionDefinition()
218
    {
219 1
        return $this->permissionDef;
220
    }
221
222
    /**
223
     * Set permission definition.
224
     *
225
     * @param PermissionDefinition $permissionDef
226
     *
227
     * @return AbstractDoctrineORMAdminListConfigurator
228
     */
229 13
    public function setPermissionDefinition(PermissionDefinition $permissionDef)
230
    {
231 13
        $this->permissionDef = $permissionDef;
232
233 13
        return $this;
234
    }
235
236
    /**
237
     * @param EntityManagerInterface $em
238
     *
239
     * @return AbstractDoctrineORMAdminListConfigurator
240
     */
241 1
    public function setEntityManager(EntityManagerInterface $em)
242
    {
243 1
        $this->em = $em;
244
245 1
        return $this;
246
    }
247
248
    /**
249
     * @return EntityManagerInterface
250
     */
251 2
    public function getEntityManager()
252
    {
253 2
        return $this->em;
254
    }
255
}
256