Passed
Push — master ( 196154...99d312 )
by Joachim
16:53
created

AbstractRepository   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 100
ccs 0
cts 62
cp 0
rs 10
c 0
b 0
f 0
wmc 13

4 Methods

Rating   Name   Duplication   Size   Complexity  
B removeByIds() 0 28 6
B iterate() 0 30 5
A _findOneByExternalId() 0 7 1
A configureIterateOptions() 0 5 1
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;
0 ignored issues
show
Bug introduced by
The type Loevgaard\DandomainFound...AbstractRepositoryTrait was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Symfony\Component\OptionsResolver\OptionsResolver;
9
10
abstract class AbstractRepository extends ServiceEntityRepository
11
{
12
    use AbstractRepositoryTrait;
13
14
    /**
15
     * On 90% of the entities there is an external id so we put this helper method here
16
     * so that all these repository doesn't have to implement the same method, instead they
17
     * can call this method and just create the type hint and validation of input
18
     *
19
     * @param $externalId
20
     * @return null|object
21
     */
22
    protected function _findOneByExternalId($externalId)
23
    {
24
        $obj = $this->findOneBy([
25
            'externalId' => $externalId,
26
        ]);
27
28
        return $obj;
29
    }
30
31
    /**
32
     * @param array $options
33
     * @return \Generator
34
     * @throws \Doctrine\ORM\OptimisticLockException
35
     */
36
    public function iterate(array $options = []): \Generator
37
    {
38
        $resolver = new OptionsResolver();
39
        $this->configureIterateOptions($resolver);
40
        $options = $resolver->resolve($options);
41
42
        $em = $this->getEntityManager();
43
44
        $qb = $this->createQueryBuilder('c');
45
46
        $result = $qb->getQuery()->iterate();
47
        $i = 1;
48
        foreach ($result as $item) {
49
            $obj = $item[0];
50
            yield $obj;
51
52
            if ($options['update']) {
53
                if (0 == $i % $options['bulkSize']) {
54
                    $em->flush();
55
                    $em->clear();
56
                }
57
            } else {
58
                $em->detach($obj);
59
            }
60
61
            ++$i;
62
        }
63
64
        if ($options['update']) {
65
            $em->flush();
66
        }
67
    }
68
69
    /**
70
     * Will remove entities based on the ids you input
71
     *
72
     * @param int[] $in
73
     * @param int[] $notIn
74
     */
75
    public function removeByIds(array $in = [], array $notIn = [])
76
    {
77
        if (!count($in) && !count($notIn)) {
78
            return;
79
        }
80
81
        $qb = $this->createQueryBuilder('e');
82
83
        if($this->getClassMetadata()->hasField('deletedAt')) {
84
            $qb->update()
85
                ->set('e.deletedAt', ':date')
86
                ->setParameter('date', new DateTimeImmutable())
87
            ;
88
        } else {
89
            $qb->delete();
90
        }
91
92
        if (count($in)) {
93
            $qb->andWhere($qb->expr()->in('e.id', ':inIds'));
94
            $qb->setParameter('inIds', $in);
95
        }
96
97
        if (count($notIn)) {
98
            $qb->andWhere($qb->expr()->notIn('e.id', ':notInIds'));
99
            $qb->setParameter('notInIds', $notIn);
100
        }
101
102
        $qb->getQuery()->execute();
103
    }
104
105
    protected function configureIterateOptions(OptionsResolver $resolver)
106
    {
107
        $resolver->setDefaults([
108
            'update' => true,
109
            'bulkSize' => 50,
110
        ]);
111
    }
112
}
113