|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Loevgaard\DandomainFoundation\Repository; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; |
|
6
|
|
|
use Loevgaard\DandomainDateTime\DateTimeImmutable; |
|
7
|
|
|
use Loevgaard\DandomainFoundation\Repository\Generated\AbstractRepositoryTrait; |
|
|
|
|
|
|
8
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
9
|
|
|
|
|
10
|
|
|
abstract class AbstractRepository extends ServiceEntityRepository implements RepositoryInterface |
|
11
|
|
|
{ |
|
12
|
|
|
use AbstractRepositoryTrait; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @param $entity |
|
16
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException |
|
17
|
|
|
*/ |
|
18
|
|
|
public function save($entity) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->getEntityManager()->persist($entity); |
|
21
|
|
|
$this->getEntityManager()->flush(); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param array $options |
|
26
|
|
|
* @return \Generator |
|
27
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException |
|
28
|
|
|
*/ |
|
29
|
|
|
public function iterate(array $options = []): \Generator |
|
30
|
|
|
{ |
|
31
|
|
|
$resolver = new OptionsResolver(); |
|
32
|
|
|
$this->configureIterateOptions($resolver); |
|
33
|
|
|
$options = $resolver->resolve($options); |
|
34
|
|
|
|
|
35
|
|
|
$em = $this->getEntityManager(); |
|
36
|
|
|
|
|
37
|
|
|
$qb = $this->createQueryBuilder('c'); |
|
38
|
|
|
|
|
39
|
|
|
$result = $qb->getQuery()->iterate(); |
|
40
|
|
|
$i = 1; |
|
41
|
|
|
foreach ($result as $item) { |
|
42
|
|
|
$obj = $item[0]; |
|
43
|
|
|
yield $obj; |
|
44
|
|
|
|
|
45
|
|
|
if ($options['update']) { |
|
46
|
|
|
if (0 == $i % $options['bulkSize']) { |
|
47
|
|
|
$em->flush(); |
|
48
|
|
|
$em->clear(); |
|
49
|
|
|
} |
|
50
|
|
|
} else { |
|
51
|
|
|
$em->detach($obj); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
++$i; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if ($options['update']) { |
|
58
|
|
|
$em->flush(); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Will remove entities based on the ids you input |
|
64
|
|
|
* |
|
65
|
|
|
* @param int[] $in |
|
66
|
|
|
* @param int[] $notIn |
|
67
|
|
|
*/ |
|
68
|
|
|
public function removeByIds(array $in = [], array $notIn = []) |
|
69
|
|
|
{ |
|
70
|
|
|
if (!count($in) && !count($notIn)) { |
|
71
|
|
|
return; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$qb = $this->createQueryBuilder('e'); |
|
75
|
|
|
|
|
76
|
|
|
if ($this->getClassMetadata()->hasField('deletedAt')) { |
|
77
|
|
|
$qb->update() |
|
78
|
|
|
->set('e.deletedAt', ':date') |
|
79
|
|
|
->setParameter('date', new DateTimeImmutable()) |
|
80
|
|
|
; |
|
81
|
|
|
} else { |
|
82
|
|
|
$qb->delete(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
if (count($in)) { |
|
86
|
|
|
$qb->andWhere($qb->expr()->in('e.id', ':inIds')); |
|
87
|
|
|
$qb->setParameter('inIds', $in); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
if (count($notIn)) { |
|
91
|
|
|
$qb->andWhere($qb->expr()->notIn('e.id', ':notInIds')); |
|
92
|
|
|
$qb->setParameter('notInIds', $notIn); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$qb->getQuery()->execute(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function clearAll() |
|
99
|
|
|
{ |
|
100
|
|
|
$this->getEntityManager()->clear(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* On 90% of the entities there is an external id so we put this helper method here |
|
105
|
|
|
* so that all these repository doesn't have to implement the same method, instead they |
|
106
|
|
|
* can call this method and just create the type hint and validation of input |
|
107
|
|
|
* |
|
108
|
|
|
* @param $externalId |
|
109
|
|
|
* @return null|object |
|
110
|
|
|
*/ |
|
111
|
|
|
protected function _findOneByExternalId($externalId) |
|
112
|
|
|
{ |
|
113
|
|
|
$obj = $this->findOneBy([ |
|
114
|
|
|
'externalId' => $externalId, |
|
115
|
|
|
]); |
|
116
|
|
|
|
|
117
|
|
|
return $obj; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
protected function configureIterateOptions(OptionsResolver $resolver) |
|
121
|
|
|
{ |
|
122
|
|
|
$resolver->setDefaults([ |
|
123
|
|
|
'update' => true, |
|
124
|
|
|
'bulkSize' => 50, |
|
125
|
|
|
]); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths