Completed
Push — master ( 29c52c...d78909 )
by Kévin
03:18
created

EagerLoadingTrait::getBooleanOperationAttribute()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 14
Code Lines 9

Duplication

Lines 7
Ratio 50 %

Importance

Changes 0
Metric Value
dl 7
loc 14
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 6
nop 4
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
 * @internal
23
 */
24
trait EagerLoadingTrait
25
{
26
    private $forceEager;
27
    private $fetchPartial;
28
    private $resourceMetadataFactory;
29
30
    /**
31
     * Checks if an operation has a `force_eager` attribute.
32
     *
33
     * @param string $resourceClass
34
     * @param array  $options
35
     *
36
     * @return bool
37
     */
38
    private function shouldOperationForceEager(string $resourceClass, array $options): bool
39
    {
40
        return $this->getBooleanOperationAttribute($resourceClass, $options, 'force_eager', $this->forceEager);
41
    }
42
43
    /**
44
     * Checks if an operation has a `fetch_partial` attribute.
45
     *
46
     * @param string $resourceClass
47
     * @param array  $options
48
     *
49
     * @return bool
50
     */
51
    private function shouldOperationFetchPartial(string $resourceClass, array $options): bool
52
    {
53
        return $this->getBooleanOperationAttribute($resourceClass, $options, 'fetch_partial', $this->fetchPartial);
54
    }
55
56
    /**
57
     * Get the boolean attribute of an operation or the resource metadata.
58
     *
59
     * @param string $resourceClass
60
     * @param array  $options
61
     * @param string $attributeName
62
     * @param bool   $default
63
     *
64
     * @return bool
65
     */
66
    private function getBooleanOperationAttribute(string $resourceClass, array $options, string $attributeName, bool $default): bool
67
    {
68
        $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
69
70 View Code Duplication
        if (isset($options['collection_operation_name'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
            $attribute = $resourceMetadata->getCollectionOperationAttribute($options['collection_operation_name'], $attributeName, null, true);
72
        } elseif (isset($options['item_operation_name'])) {
73
            $attribute = $resourceMetadata->getItemOperationAttribute($options['item_operation_name'], $attributeName, null, true);
74
        } else {
75
            $attribute = $resourceMetadata->getAttribute($attributeName);
76
        }
77
78
        return is_bool($attribute) ? $attribute : $default;
79
    }
80
81
    /**
82
     * Checkes if the class has an associationMapping with FETCH=EAGER.
83
     *
84
     * @param EntityManager     $em
85
     * @param ClassMetadataInfo $classMetadata
86
     * @param array             $checked       array cache of tested metadata classes
87
     *
88
     * @return bool
89
     */
90
    private function hasFetchEagerAssociation(EntityManager $em, ClassMetadataInfo $classMetadata, array &$checked = []): bool
91
    {
92
        $checked[] = $classMetadata->name;
93
94
        foreach ($classMetadata->associationMappings as $mapping) {
95
            if (ClassMetadataInfo::FETCH_EAGER === $mapping['fetch']) {
96
                return true;
97
            }
98
99
            $related = $em->getClassMetadata($mapping['targetEntity']);
100
101
            if (in_array($related->name, $checked, true)) {
102
                continue;
103
            }
104
105
            if (true === $this->hasFetchEagerAssociation($em, $related, $checked)) {
106
                return true;
107
            }
108
        }
109
110
        return false;
111
    }
112
}
113