Passed
Push — analysis-z3NYGr ( 02a42e )
by Nuno
11:13 queued 02:06
created

ModelsResolver::from()   C

Complexity

Conditions 13
Paths 228

Size

Total Lines 50
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 13.0069

Importance

Changes 0
Metric Value
cc 13
eloc 29
nc 228
nop 3
dl 0
loc 50
ccs 28
cts 29
cp 0.9655
crap 13.0069
rs 5.4333
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Scout Extended.
7
 *
8
 * (c) Algolia Team <[email protected]>
9
 *
10
 *  For the full copyright and license information, please view the LICENSE
11
 *  file that was distributed with this source code.
12
 */
13
14
namespace Algolia\ScoutExtended\Searchable;
15
16
use function in_array;
17
use Laravel\Scout\Builder;
18
use function call_user_func;
19
use Laravel\Scout\Searchable;
20
use Illuminate\Database\Eloquent\Collection;
21
use Illuminate\Database\Eloquent\SoftDeletes;
22
23
/**
24
 * @internal
25
 */
26
final class ModelsResolver
27
{
28
    /**
29
     * Get a set of models from the provided ids.
30
     *
31
     * @param \Laravel\Scout\Builder $builder
32
     * @param  string|object $searchable
33
     * @param  array $ids
34
     *
35
     * @return \Illuminate\Database\Eloquent\Collection
36
     */
37 3
    public function from(Builder $builder, $searchable, array $ids): Collection
38
    {
39 3
        $instances = collect();
40
41 3
        $models = [];
42 3
        foreach ($ids as $id) {
43 3
            $modelClass = ObjectIdEncrypter::decryptSearchable($id);
44 3
            $modelKey = ObjectIdEncrypter::decryptSearchableKey($id);
45 3
            if (! array_key_exists($modelClass, $models)) {
46 3
                $models[$modelClass] = [];
47
            }
48
49 3
            $models[$modelClass][] = $modelKey;
50
        }
51
52 3
        foreach ($models as $modelClass => $modelKeys) {
53 3
            $model = new $modelClass;
54
55 3
            if (in_array(Searchable::class, class_uses_recursive($model), true)) {
56 3
                if (! empty($models = $model->getScoutModelsByIds($builder, $modelKeys))) {
57 3
                    $instances = $instances->merge($models);
58
                }
59
            } else {
60 1
                $query = in_array(SoftDeletes::class, class_uses_recursive($model),
61 1
                    true) ? $model->withTrashed() : $model->newQuery();
62
63 1
                if ($builder->queryCallback) {
64
                    call_user_func($builder->queryCallback, $query);
65
                }
66
67 1
                $scoutKey = method_exists($model,
68 1
                    'getScoutKeyName') ? $model->getScoutKeyName() : $model->getQualifiedKeyName();
69 1
                if ($models = $query->whereIn($scoutKey, $modelKeys)->get()) {
70 3
                    $instances = $instances->merge($models);
71
                }
72
            }
73
        }
74
75 3
        $result = $searchable->newCollection();
76
77 3
        foreach ($ids as $id) {
78 3
            foreach ($instances as $instance) {
79 3
                if (ObjectIdEncrypter::encrypt($instance) === ObjectIdEncrypter::withoutPart($id)) {
80 3
                    $result->push($instance);
81 3
                    break;
82
                }
83
            }
84
        }
85
86 3
        return $result;
87
    }
88
}
89