Completed
Push — master ( d461d1...b123ad )
by Joachim
05:50
created

EntityRepository::getRepository()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace Loevgaard\DandomainAltapayBundle\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 string
35
     */
36
    protected $class;
37
38
    /**
39
     * @var PaginatorInterface
40
     */
41
    protected $paginator;
42
43
    /**
44
     * @var DoctrineEntityRepository
45
     */
46
    private $repository;
47
48
    public function __construct(ManagerRegistry $managerRegistry, PaginatorInterface $paginator, string $class)
49
    {
50
        $this->manager = $managerRegistry->getManagerForClass($class);
51
        $this->class = $class;
52
        $this->paginator = $paginator;
53
    }
54
55
    /**
56
     * @param string $name
57
     * @param array  $arguments
58
     *
59
     * @return mixed
60
     */
61
    public function __call($name, $arguments)
62
    {
63 View Code Duplication
        if (method_exists($this->getRepository(), $name)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
64
            return call_user_func_array([$this->getRepository(), $name], $arguments);
65
        }
66
67 View Code Duplication
        if (method_exists($this->manager, $name)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
68
            return call_user_func_array([$this->manager, $name], $arguments);
69
        }
70
    }
71
72
    /**
73
     * Saves the $object.
74
     *
75
     * @param $object
76
     */
77
    public function save($object)
78
    {
79
        $this->manager->persist($object);
80
        $this->manager->flush();
81
    }
82
83
    /**
84
     * @param int          $page
85
     * @param int          $itemsPerPage
86
     * @param array        $orderBy
87
     * @param QueryBuilder $qb
88
     *
89
     * @return PaginationInterface
90
     */
91
    public function findAllWithPaging($page = 1, $itemsPerPage = 100, array $orderBy = [], QueryBuilder $qb = null): PaginationInterface
92
    {
93
        if (!$qb) {
94
            $qb = $this->getQueryBuilder('e');
95
        }
96
97
        foreach ($orderBy as $field => $direction) {
98
            $qb->addOrderBy($field, $direction);
99
        }
100
101
        $objs = $this->paginator->paginate(
102
            $qb,
103
            $page,
104
            $itemsPerPage
105
        );
106
107
        return $objs;
108
    }
109
110
    /**
111
     * @param string $alias
112
     *
113
     * @return QueryBuilder
114
     */
115
    protected function getQueryBuilder(string $alias): QueryBuilder
116
    {
117
        return $this->getRepository()->createQueryBuilder($alias);
118
    }
119
120
    /**
121
     * @return DoctrineEntityRepository
122
     */
123
    protected function getRepository(): DoctrineEntityRepository
124
    {
125
        if (!$this->repository) {
126
            $this->repository = $this->manager->getRepository($this->class);
127
        }
128
129
        return $this->repository;
130
    }
131
}
132