Conditions | 16 |
Paths | 1024 |
Total Lines | 80 |
Code Lines | 52 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
31 | public function paginateData(EntityRepository $repository, array $findBy = []) |
||
32 | { |
||
33 | $queryBuilder = $repository->createQueryBuilder('o'); |
||
34 | $request = $this->request->query; |
||
35 | |||
36 | // On s'assure de bien recevoir des arrays |
||
37 | foreach ($findBy as $key => $value) { |
||
38 | $findBy[$key] = array($value); |
||
39 | } |
||
40 | |||
41 | // On récupère les paramètres de la requête |
||
42 | $page = $request->has('page') ? $request->get('page') : 1; |
||
43 | $limit = $request->has('limit') ? $request->get('limit') : 100; |
||
44 | $sort = $request->has('sort') ? $request->get('sort') : null; |
||
45 | |||
46 | if ($sort === null) { |
||
47 | $sortBy = ['id' => 'DESC']; |
||
48 | } else { |
||
49 | $sortBy = []; |
||
50 | |||
51 | foreach (explode(',', $sort) as $value) { |
||
52 | $order = preg_match('/^\-.*/isU', $value) ? 'DESC' : 'ASC'; |
||
53 | $field = preg_replace('/^\-/isU', '', $value); |
||
54 | $sortBy[$field] = $order; |
||
55 | } |
||
56 | } |
||
57 | |||
58 | foreach ($request->all() as $key => $values) { |
||
59 | if ($key != 'page' && $key != 'limit' && $key != 'sort') { |
||
60 | $findBy[$key] = explode(',', $values); |
||
61 | } |
||
62 | } |
||
63 | |||
64 | // On compte le nombre total d'entrées dans la BDD |
||
65 | $queryBuilder->select('count(o.id)'); |
||
66 | foreach ($findBy as $key => $values){ |
||
67 | $andCount = 0; |
||
68 | $and = ''; |
||
69 | foreach($values as $value){ |
||
70 | if($andCount > 0){ |
||
71 | $and .= ' OR '; |
||
72 | } |
||
73 | $and .= 'o.' . $key . ' = :' . $key . $andCount; |
||
74 | $queryBuilder->setParameter($key . $andCount, $value); |
||
75 | |||
76 | $andCount++; |
||
77 | } |
||
78 | $queryBuilder->andWhere($and); |
||
79 | } |
||
80 | |||
81 | foreach($sortBy as $field => $order){ |
||
82 | $queryBuilder->addOrderBy('o.' . $field, $order); |
||
83 | } |
||
84 | |||
85 | $count = $queryBuilder->getQuery()->getSingleScalarResult(); |
||
86 | |||
87 | // On vérifie que l'utilisateur ne fasse pas de connerie avec les variables |
||
88 | $totalPages = ceil($count/$limit); |
||
89 | $limit = min($limit, 10000); |
||
90 | $limit = max($limit, 1); |
||
91 | $page = min($page, $totalPages); |
||
92 | $page = max($page, 1); |
||
93 | |||
94 | $results = $queryBuilder->select('o') |
||
95 | ->setMaxResults($limit) |
||
96 | ->setFirstResult(($page - 1)*$limit) |
||
97 | ->getQuery() |
||
98 | ->getResult(); |
||
99 | |||
100 | return [ |
||
101 | 'findBy' => $findBy, |
||
102 | 'sortBy' => $sortBy, |
||
103 | 'limit' => $limit, |
||
104 | 'offset' => ($page - 1)*$limit, |
||
105 | 'page' => $page, |
||
106 | 'totalPages' => $totalPages, |
||
107 | 'count' => $count, |
||
108 | 'results' => $results, |
||
109 | ]; |
||
110 | } |
||
111 | |||
160 |
The
EntityManager
might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManager
is closed. Any other code which depends on the same instance of theEntityManager
during this request will fail.On the other hand, if you instead inject the
ManagerRegistry
, thegetManager()
method guarantees that you will always get a usable manager instance.