TelefonicaScope   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 0
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 Telefonica\Scopes;
6
7
use Illuminate\Database\Eloquent\Scope;
8
use Illuminate\Database\Eloquent\Builder;
9
use Illuminate\Database\Eloquent\Model as Entity;
10
11
class TelefonicaScope implements Scope
12
{
13
    /**
14
     * Apply the scope to a given Eloquent query builder.
15
     *
16
     * @param \Illuminate\Database\Eloquent\Builder $builder
17
     * @param \Illuminate\Database\Eloquent\Model   $entity
18
     *
19
     * @return void
20
     */
21
    public function apply(Builder $builder, Entity $entity): void
22
    {
23
        $eagerLoads = $builder->getTelefonicas();
24
25
        // If there is any eagerload matching the eav key, we will replace it with
26
        // all the registered properties for the entity. We'll simulate as if the
27
        // user has manually added all of these withs in purpose when querying.
28
        if (array_key_exists('eav', $eagerLoads)) {
29
            $eagerLoads = array_merge($eagerLoads, $entity->getEntityAttributeRelations());
30
31
            $builder->setTelefonicas(array_except($eagerLoads, 'eav'));
32
        }
33
    }
34
}
35