Completed
Push — master ( ef62ef...3339ef )
by Mike
02:44
created

Builder::eagerLoadRelation()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 24
rs 8.9713
cc 2
eloc 16
nc 2
nop 3
1
<?php namespace GeneaLabs\LaravelModelCaching;
2
3
use Closure;
4
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
5
6
class Builder extends EloquentBuilder
7
{
8
    protected function eagerLoadRelation(array $models, $name, Closure $constraints)
9
    {
10
        $relation = $this->getRelation($name);
11
        $relation->addEagerConstraints($models);
12
        $constraints($relation);
13
14
        $parentIds = implode('_', collect($models)->pluck('id')->toArray());
15
        $parentName = str_slug(get_class($relation->getParent()));
16
        $childName = str_slug(get_class($relation->getRelated()));
17
        $cache = cache();
18
19
        if (is_subclass_of(cache()->getStore(), TaggableStore::class)) {
0 ignored issues
show
Bug introduced by
The type GeneaLabs\LaravelModelCaching\TaggableStore 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...
20
            $cache->tags([$parentName, $childName]);
21
        }
22
23
        $results = $cache
24
            ->rememberForever("{$parentName}_{$parentIds}-{$childName}s", function () use ($relation) {
25
                return $relation->getEager();
26
            });
27
28
        return $relation->match(
29
            $relation->initRelation($models, $name),
30
            $results,
31
            $name
32
        );
33
    }
34
}
35