|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dontdrinkandroot\RestBundle\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityRepository; |
|
6
|
|
|
use Doctrine\ORM\Tools\Pagination\Paginator; |
|
7
|
|
|
|
|
8
|
|
|
class DoctrineEntityRepositoryCrudService extends EntityRepository implements CrudServiceInterface |
|
9
|
|
|
{ |
|
10
|
22 |
|
public function __construct($entityManager, $entityClass) |
|
11
|
|
|
{ |
|
12
|
22 |
|
parent::__construct($entityManager, $entityManager->getClassMetadata($entityClass)); |
|
13
|
22 |
|
} |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* {@inheritdoc} |
|
17
|
|
|
*/ |
|
18
|
18 |
|
public function findById($id) |
|
19
|
|
|
{ |
|
20
|
18 |
|
if ($this->isUuid($id)) { |
|
21
|
|
|
return $this->findOneBy(['uuid' => $id]); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
18 |
|
return $this->find($id); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* {@inheritdoc} |
|
29
|
|
|
*/ |
|
30
|
4 |
|
public function listPaginated(int $page, int $perPage = 50): Paginator |
|
31
|
|
|
{ |
|
32
|
4 |
|
$queryBuilder = $this->createQueryBuilder('entity'); |
|
33
|
4 |
|
$queryBuilder->setFirstResult(($page - 1) * $perPage); |
|
34
|
4 |
|
$queryBuilder->setMaxResults($perPage); |
|
35
|
|
|
|
|
36
|
4 |
|
return new Paginator($queryBuilder); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
18 |
|
protected function isUuid($id) |
|
40
|
|
|
{ |
|
41
|
18 |
|
return 1 === preg_match('/^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$/', $id); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* {@inheritdoc} |
|
46
|
|
|
*/ |
|
47
|
|
|
public function create($entity) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->getEntityManager()->persist($entity); |
|
50
|
|
|
$this->getEntityManager()->flush($entity); |
|
51
|
|
|
|
|
52
|
|
|
return $entity; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
*/ |
|
58
|
2 |
|
public function update($entity) |
|
59
|
|
|
{ |
|
60
|
2 |
|
$this->getEntityManager()->flush($entity); |
|
61
|
|
|
|
|
62
|
2 |
|
return $entity; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* {@inheritdoc} |
|
67
|
|
|
*/ |
|
68
|
|
|
public function remove($entity) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->getEntityManager()->remove($entity); |
|
71
|
|
|
$this->getEntityManager()->flush($entity); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* {@inheritdoc} |
|
76
|
|
|
*/ |
|
77
|
2 |
|
public function listAssociationPaginated($entity, string $fieldName, int $page = 1, $perPage = 50) |
|
78
|
|
|
{ |
|
79
|
2 |
|
$classMetadata = $this->getEntityManager()->getClassMetadata(get_class($entity)); |
|
80
|
2 |
|
$association = $classMetadata->getAssociationMapping($fieldName); |
|
81
|
2 |
|
$targetClass = $classMetadata->getAssociationTargetClass($fieldName); |
|
82
|
|
|
|
|
83
|
2 |
|
$inverseFieldName = null; |
|
|
|
|
|
|
84
|
2 |
|
if ($classMetadata->isAssociationInverseSide($fieldName)) { |
|
85
|
|
|
$inverseFieldName = $association['mappedBy']; |
|
86
|
|
|
} else { |
|
87
|
2 |
|
$inverseFieldName = $association['inversedBy']; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
2 |
|
$queryBuilder = $this->getEntityManager()->createQueryBuilder(); |
|
91
|
2 |
|
$queryBuilder->select('association'); |
|
92
|
2 |
|
$queryBuilder->from($targetClass, 'association'); |
|
93
|
2 |
|
$queryBuilder->join('association.' . $inverseFieldName, 'entity'); |
|
94
|
2 |
|
$queryBuilder->where('entity = :entity'); |
|
95
|
2 |
|
$queryBuilder->setParameter('entity', $entity); |
|
96
|
|
|
|
|
97
|
2 |
|
$queryBuilder->setFirstResult(($page - 1) * $perPage); |
|
98
|
2 |
|
$queryBuilder->setMaxResults($perPage); |
|
99
|
|
|
|
|
100
|
2 |
|
$queryBuilder->getQuery(); |
|
101
|
|
|
|
|
102
|
2 |
|
return new Paginator($queryBuilder); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.