EagerLoadScope   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 2
dl 0
loc 24
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A apply() 0 13 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Attributes\Scopes;
6
7
use Illuminate\Support\Arr;
8
use Illuminate\Database\Eloquent\Scope;
9
use Illuminate\Database\Eloquent\Builder;
10
use Illuminate\Database\Eloquent\Model as Entity;
11
12
class EagerLoadScope implements Scope
13
{
14
    /**
15
     * Apply the scope to a given Eloquent query builder.
16
     *
17
     * @param \Illuminate\Database\Eloquent\Builder $builder
18
     * @param \Illuminate\Database\Eloquent\Model   $entity
19
     *
20
     * @return void
21
     */
22
    public function apply(Builder $builder, Entity $entity): void
23
    {
24
        $eagerLoads = $builder->getEagerLoads();
25
26
        // If there is any eagerload matching the eav key, we will replace it with
27
        // all the registered properties for the entity. We'll simulate as if the
28
        // user has manually added all of these withs in purpose when querying.
29
        if (array_key_exists('eav', $eagerLoads)) {
30
            $eagerLoads = array_merge($eagerLoads, $entity->getEntityAttributeRelations());
31
32
            $builder->setEagerLoads(Arr::except($eagerLoads, 'eav'));
33
        }
34
    }
35
}
36