updateParentChildRelationships()   B
last analyzed

Complexity

Conditions 8
Paths 50

Size

Total Lines 62
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
cc 8
eloc 38
c 0
b 0
f 0
nc 50
nop 1
dl 0
loc 62
ccs 0
cts 59
cp 0
crap 72
rs 8.0675

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
namespace Loevgaard\DandomainFoundation\Repository;
4
5
use Doctrine\ORM\ORMException;
6
use Doctrine\ORM\UnexpectedResultException;
7
use Loevgaard\DandomainFoundation\Entity\Generated\ProductInterface;
0 ignored issues
show
Bug introduced by
The type Loevgaard\DandomainFound...erated\ProductInterface 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 Loevgaard\DandomainFoundation\Entity\Product;
9
use Loevgaard\DandomainFoundation\Repository\Generated\ProductRepositoryTrait;
0 ignored issues
show
Bug introduced by
The type Loevgaard\DandomainFound...\ProductRepositoryTrait 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...
10
use Symfony\Bridge\Doctrine\RegistryInterface;
11
12
/**
13
 * @method null|ProductInterface find($id)
14
 * @method ProductInterface[]    findBy(array $criteria, array $orderBy = null, int $limit = null, int $offset = null)
15
 * @method null|ProductInterface findOneBy(array $criteria)
16
 * @method ProductInterface[]    findAll()
17
 */
18
class ProductRepository extends AbstractRepository
19
{
20
    use ProductRepositoryTrait;
21
22
    public function __construct(RegistryInterface $registry)
23
    {
24
        parent::__construct($registry, Product::class);
0 ignored issues
show
Bug introduced by
$registry of type Symfony\Bridge\Doctrine\RegistryInterface is incompatible with the type Doctrine\Common\Persistence\ManagerRegistry expected by parameter $registry of Loevgaard\DandomainFound...pository::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

24
        parent::__construct(/** @scrutinizer ignore-type */ $registry, Product::class);
Loading history...
25
    }
26
27
    public function findOneByExternalId(int $externalId): ?ProductInterface
28
    {
29
        /** @var ProductInterface $obj */
30
        $obj = $this->_findOneByExternalId($externalId);
31
32
        return $obj;
33
    }
34
35
    /**
36
     * @param string $number
37
     *
38
     * @return ProductInterface|null
39
     */
40
    public function findOneByNumber(string $number): ?ProductInterface
41
    {
42
        /** @var ProductInterface $obj */
43
        $obj = $this->findOneBy([
44
            'number' => $number,
45
        ]);
46
47
        return $obj;
48
    }
49
50
    /**
51
     * @param array $productIds
52
     *
53
     * @throws \Doctrine\ORM\OptimisticLockException
54
     */
55
    public function updateParentChildRelationships(array $productIds = [])
56
    {
57
        $variantMasterIdCache = [];
58
59
        $innerQb = $this->createQueryBuilder('p');
60
        $innerQb->where('p.isVariantMaster = 1')
61
            ->andWhere('p.number = :number');
62
63
        $qb = $this->createQueryBuilder('p');
64
        $qb
65
            ->where('p.isVariantMaster = 0')
66
            ->andWhere('p.variantMasterId is not null')
67
        ;
68
69
        if (count($productIds)) {
70
            $qb->andWhere($qb->expr()->in('p.id', ':productIds'));
71
            $qb->setParameter('productIds', $productIds);
72
        }
73
74
        $batchSize = 50;
75
        $i = 1;
76
77
        $iterableResult = $qb->getQuery()->iterate();
78
        foreach ($iterableResult as $row) {
79
            /** @var ProductInterface $product */
80
            $product = $row[0];
81
82
            if (!isset($variantMasterIdCache[$product->getVariantMasterId()])) {
83
                try {
84
                    /** @var ProductInterface $parent */
85
                    $parent = $innerQb->setParameter(
86
                        'number',
87
                        $product->getVariantMasterId()
88
                    )->getQuery()->getSingleResult();
89
                    $parent = $parent->getId();
90
                } catch (UnexpectedResultException $e) {
91
                    $parent = null;
92
                }
93
94
                $variantMasterIdCache[$product->getVariantMasterId()] = $parent;
95
            }
96
97
            $ref = $variantMasterIdCache[$product->getVariantMasterId()];
98
            if ($ref) {
99
                try {
100
                    $ref = $this->getEntityManager()->getReference(Product::class, $ref);
101
                } catch (ORMException $e) {
102
                    $ref = null;
103
                }
104
            }
105
106
            $product->setParent($ref);
107
108
            if (0 === ($i % $batchSize)) {
109
                $this->getEntityManager()->flush();
110
                $this->getEntityManager()->clear();
111
            }
112
113
            ++$i;
114
        }
115
116
        $this->getEntityManager()->flush();
117
    }
118
}
119