EntityRepository::__call()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 6
Ratio 50 %

Importance

Changes 0
Metric Value
dl 6
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 2
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->paginator = $paginator;
52
        $this->class = $class;
53
54
    }
55
56
    /**
57
     * @param string $name
58
     * @param array  $arguments
59
     *
60
     * @return mixed
61
     */
62
    public function __call($name, $arguments)
63
    {
64 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...
65
            return call_user_func_array([$this->getRepository(), $name], $arguments);
66
        }
67
68 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...
69
            return call_user_func_array([$this->manager, $name], $arguments);
70
        }
71
72
        throw new \RuntimeException('Method '.$name.' not defined in '.__CLASS__);
73
    }
74
75
    /**
76
     * Saves the $object.
77
     *
78
     * @param $object
79
     */
80
    public function save($object)
81
    {
82
        $this->manager->persist($object);
83
        $this->manager->flush();
84
    }
85
86
    /**
87
     * @param int          $page
88
     * @param int          $itemsPerPage
89
     * @param array        $orderBy
90
     * @param QueryBuilder $qb
91
     *
92
     * @return PaginationInterface
93
     */
94
    public function findAllWithPaging($page = 1, $itemsPerPage = 100, array $orderBy = [], QueryBuilder $qb = null): PaginationInterface
95
    {
96
        if (!$qb) {
97
            $qb = $this->getQueryBuilder('e');
98
        }
99
100
        foreach ($orderBy as $field => $direction) {
101
            $qb->addOrderBy($field, $direction);
102
        }
103
104
        $objs = $this->paginator->paginate(
105
            $qb,
106
            $page,
107
            $itemsPerPage
108
        );
109
110
        return $objs;
111
    }
112
113
    /**
114
     * @param string $alias
115
     *
116
     * @return QueryBuilder
117
     */
118
    public function getQueryBuilder(string $alias): QueryBuilder
119
    {
120
        return $this->getRepository()->createQueryBuilder($alias);
121
    }
122
123
    /**
124
     * @return DoctrineEntityRepository
125
     */
126
    protected function getRepository(): DoctrineEntityRepository
127
    {
128
        if (!$this->repository) {
129
            $this->repository = $this->manager->getRepository($this->class);
130
        }
131
132
        return $this->repository;
133
    }
134
}
135