Completed
Push — master ( 1a118c...f720af )
by Kévin
03:46
created

ShouldEagerLoad::hasFetchEagerAssociation()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.6737
c 0
b 0
f 0
cc 5
eloc 11
nc 5
nop 3
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\Bridge\Doctrine\Orm\Util;
15
16
use Doctrine\ORM\EntityManager;
17
use Doctrine\ORM\Mapping\ClassMetadataInfo;
18
19
/**
20
 * @author Antoine Bluchet <[email protected]>
21
 */
22
trait ShouldEagerLoad
23
{
24
    /**
25
     * Checks if an operation has a `force_eager` attribute.
26
     *
27
     * @param string $resourceClass
28
     * @param array  $options
29
     *
30
     * @return bool
31
     */
32
    private function shouldOperationForceEager(string $resourceClass, array $options): bool
33
    {
34
        $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
0 ignored issues
show
Bug introduced by
The property resourceMetadataFactory does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
35
36
        if (isset($options['collection_operation_name'])) {
37
            $forceEager = $resourceMetadata->getCollectionOperationAttribute($options['collection_operation_name'], 'force_eager', null, true);
38
        } elseif (isset($options['item_operation_name'])) {
39
            $forceEager = $resourceMetadata->getItemOperationAttribute($options['item_operation_name'], 'force_eager', null, true);
40
        } else {
41
            $forceEager = $resourceMetadata->getAttribute('force_eager');
42
        }
43
44
        return is_bool($forceEager) ? $forceEager : $this->forceEager;
0 ignored issues
show
Bug introduced by
The property forceEager does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
45
    }
46
47
    /**
48
     * Checkes if the class has an associationMapping with FETCH=EAGER.
49
     *
50
     * @param EntityManager     $em
51
     * @param ClassMetadataInfo $classMetadata
52
     * @param array             $checked       array cache of tested metadata classes
53
     */
54
    private function hasFetchEagerAssociation(EntityManager $em, ClassMetadataInfo $classMetadata, &$checked = [])
55
    {
56
        $checked[] = $classMetadata->name;
57
58
        foreach ($classMetadata->associationMappings as $mapping) {
59
            if (ClassMetadataInfo::FETCH_EAGER === $mapping['fetch']) {
60
                return true;
61
            }
62
63
            $related = $em->getClassMetadata($mapping['targetEntity']);
64
65
            if (in_array($related->name, $checked, true)) {
66
                continue;
67
            }
68
69
            if (true === $this->hasFetchEagerAssociation($em, $related, $checked)) {
70
                return true;
71
            }
72
        }
73
74
        return false;
75
    }
76
}
77