CrudRepository::findPaginatedBy()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 11
cts 11
cp 1
rs 9.536
c 0
b 0
f 0
cc 4
nc 4
nop 4
crap 4
1
<?php
2
3
namespace Dontdrinkandroot\Repository;
4
5
use Doctrine\ORM\EntityRepository;
6
use Doctrine\ORM\QueryBuilder;
7
use Doctrine\ORM\Tools\Pagination\Paginator;
8
use Dontdrinkandroot\Entity\EntityInterface;
9
10
/**
11
 * @author Philip Washington Sorst <[email protected]>
12
 */
13
class CrudRepository extends EntityRepository implements CrudRepositoryInterface
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: find, findAll, findBy, findOneBy, getClassName
Loading history...
14
{
15
    /**
16
     * {@inheritdoc}
17
     */
18 2
    public function persist(object $entity, bool $flush = true): object
19
    {
20 2
        $this->getEntityManager()->persist($entity);
0 ignored issues
show
Bug introduced by
The method getEntityManager() does not seem to exist on object<Dontdrinkandroot\...ository\CrudRepository>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
21
22 2
        if ($flush) {
23 2
            $this->flush($entity);
24
        }
25
26 2
        return $entity;
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 4
    public function merge(object $entity, bool $flush = false): object
33
    {
34 4
        $entity = $this->getEntityManager()->merge($entity);
0 ignored issues
show
Bug introduced by
The method getEntityManager() does not seem to exist on object<Dontdrinkandroot\...ository\CrudRepository>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
35
36 4
        if ($flush) {
37 4
            $this->flush($entity);
38
        }
39
40 4
        return $entity;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 8
    public function flush(object $entity = null): void
47
    {
48 8
        $this->getEntityManager()->flush($entity);
0 ignored issues
show
Bug introduced by
The method getEntityManager() does not seem to exist on object<Dontdrinkandroot\...ository\CrudRepository>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
49 8
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function detach(object $entity): void
55
    {
56
        $this->getEntityManager()->detach($entity);
0 ignored issues
show
Bug introduced by
The method getEntityManager() does not seem to exist on object<Dontdrinkandroot\...ository\CrudRepository>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function removeById($id, bool $flush = false): void
63
    {
64
        /** @var EntityInterface $entity */
65
        $entity = $this->find($id);
66
        $this->remove($entity, $flush);
0 ignored issues
show
Documentation introduced by
$entity is of type object<Dontdrinkandroot\Entity\EntityInterface>, but the function expects a object<Dontdrinkandroot\Repository\object>.

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...
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function remove(object $entity, bool $flush = false): void
73
    {
74
        $this->getEntityManager()->remove($entity);
0 ignored issues
show
Bug introduced by
The method getEntityManager() does not seem to exist on object<Dontdrinkandroot\...ository\CrudRepository>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
75
76
        if ($flush) {
77
            $this->flush($entity);
78
        }
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84 4
    public function removeAll(bool $flush = false, bool $iterate = true): void
85
    {
86 4
        if ($iterate) {
87 2
            $this->removeAllByIterating();
88
        } else {
89 2
            $this->removeAllByQuery();
90
        }
91
92 4
        if ($flush) {
93
            $this->flush();
94
        }
95 4
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100 2
    public function findPaginatedBy(
101
        int $page = 1,
102
        int $perPage = 10,
103
        array $criteria = [],
104
        array $orderBy = null
105
    ): Paginator {
106 2
        $queryBuilder = $this->createQueryBuilder('entity');
0 ignored issues
show
Bug introduced by
The method createQueryBuilder() does not seem to exist on object<Dontdrinkandroot\...ository\CrudRepository>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
107
108 2
        foreach ($criteria as $field => $value) {
109 2
            $queryBuilder->andWhere('entity.' . $field . ' = :' . $field);
110 2
            $queryBuilder->setParameter($field, $value);
111
        }
112
113 2
        if (null !== $orderBy) {
114 2
            foreach ($orderBy as $field => $order) {
115 2
                $queryBuilder->addOrderBy('entity.' . $field, $order);
116
            }
117
        }
118
119 2
        $queryBuilder->setFirstResult(($page - 1) * $perPage);
120 2
        $queryBuilder->setMaxResults($perPage);
121
122 2
        return new Paginator($queryBuilder);
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128 6
    public function countAll(): int
129
    {
130 6
        $queryBuilder = $this->getEntityManager()->createQueryBuilder();
0 ignored issues
show
Bug introduced by
The method getEntityManager() does not seem to exist on object<Dontdrinkandroot\...ository\CrudRepository>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
131
        $queryBuilder
132 6
            ->select('count(entity)')
133 6
            ->from($this->getClassName(), 'entity');
134
135 6
        return $queryBuilder->getQuery()->getSingleScalarResult();
136
    }
137
138 2
    protected function removeAllByIterating(int $batchSize = 100): void
139
    {
140 2
        $entities = $this->findAll();
141 2
        $count = 0;
142 2
        foreach ($entities as $entity) {
143 2
            $this->remove($entity, false);
144 2
            $count++;
145 2
            if ($count >= $batchSize) {
146
                $this->getEntityManager()->flush();
0 ignored issues
show
Bug introduced by
The method getEntityManager() does not seem to exist on object<Dontdrinkandroot\...ository\CrudRepository>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
147
                $count = 0;
148
            }
149
        }
150 2
    }
151
152 2
    protected function removeAllByQuery(): void
153
    {
154 2
        $queryBuilder = $this->createQueryBuilder('entity');
0 ignored issues
show
Bug introduced by
The method createQueryBuilder() does not seem to exist on object<Dontdrinkandroot\...ository\CrudRepository>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
155 2
        $queryBuilder->delete();
156 2
        $query = $queryBuilder->getQuery();
157 2
        $query->execute();
158 2
    }
159
160
    protected function createBlankQueryBuilder(): QueryBuilder
161
    {
162
        return $this->getEntityManager()->createQueryBuilder();
0 ignored issues
show
Bug introduced by
The method getEntityManager() does not seem to exist on object<Dontdrinkandroot\...ository\CrudRepository>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
163
    }
164
}
165