EntityRepository::getQueryBuilder()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Loevgaard\PakkelabelsBundle\Entity;
4
5
use Doctrine\Common\Persistence\ManagerRegistry;
6
use Doctrine\Common\Persistence\ObjectManager;
7
use Doctrine\ORM\EntityRepository as DoctrineEntityRepository;
8
use Doctrine\ORM\QueryBuilder;
9
use Knp\Component\Pager\Pagination\PaginationInterface;
10
use Knp\Component\Pager\PaginatorInterface;
11
12
/**
13
 * This entity repository is implemented using the principles described here:
14
 * https://www.tomasvotruba.cz/blog/2017/10/16/how-to-use-repository-with-doctrine-as-service-in-symfony/.
15
 *
16
 * @todo this class should probably be in a separate library
17
 *
18
 * @method null|object find($id)
19
 * @method array findBy(array $criteria, array $orderBy = null, int $limit = null, int $offset = null)
20
 * @method null|object findOneBy(array $criteria)
21
 * @method array findAll()
22
 * @method persist($object)
23
 * @method flush()
24
 * @method remove($object)
25
 */
26
abstract class EntityRepository
27
{
28
    /**
29
     * @var ObjectManager
30
     */
31
    protected $manager;
32
33
    /**
34
     * @var DoctrineEntityRepository
35
     */
36
    protected $repository;
37
38
    /**
39
     * @var PaginatorInterface
40
     */
41
    protected $paginator;
42
43
    public function __construct(ManagerRegistry $managerRegistry, PaginatorInterface $paginator, string $class)
44
    {
45
        $this->manager = $managerRegistry->getManagerForClass($class);
46
        $this->repository = $this->manager->getRepository($class);
47
        $this->paginator = $paginator;
48
    }
49
50
    /**
51
     * @param string $name
52
     * @param array  $arguments
53
     *
54
     * @return mixed
55
     */
56
    public function __call($name, $arguments)
57
    {
58
        if (method_exists($this->repository, $name)) {
59
            return call_user_func_array([$this->repository, $name], $arguments);
60
        }
61
62
        if (method_exists($this->manager, $name)) {
63
            return call_user_func_array([$this->manager, $name], $arguments);
64
        }
65
    }
66
67
    /**
68
     * Saves the $object.
69
     *
70
     * @param $object
71
     */
72
    public function save($object)
73
    {
74
        $this->manager->persist($object);
75
        $this->manager->flush();
76
    }
77
78
    /**
79
     * @param int          $page
80
     * @param int          $itemsPerPage
81
     * @param array        $orderBy
82
     * @param QueryBuilder $qb
83
     *
84
     * @return PaginationInterface
85
     */
86
    public function findAllWithPaging($page = 1, $itemsPerPage = 100, array $orderBy = [], QueryBuilder $qb = null): PaginationInterface
87
    {
88
        if (!$qb) {
89
            $qb = $this->getQueryBuilder();
90
        }
91
92
        foreach ($orderBy as $field => $direction) {
93
            $qb->addOrderBy($field, $direction);
94
        }
95
96
        $objs = $this->paginator->paginate(
97
            $qb,
98
            $page,
99
            $itemsPerPage
100
        );
101
102
        return $objs;
103
    }
104
105
    /**
106
     * @return QueryBuilder
107
     */
108
    public function getQueryBuilder(): QueryBuilder
109
    {
110
        return $this->repository->createQueryBuilder('l');
111
    }
112
}
113