Passed
Pull Request — master (#147)
by Mozammil
05:04 queued 01:50
created

ModelsResolver::from()   D

Complexity

Conditions 15
Paths 228

Size

Total Lines 61
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 15.0457

Importance

Changes 0
Metric Value
cc 15
eloc 35
nc 228
nop 3
dl 0
loc 61
ccs 32
cts 34
cp 0.9412
crap 15.0457
rs 4.7333
c 0
b 0
f 0

How to fix   Long Method    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 Illuminate\Support\Arr;
19
use function call_user_func;
20
use Laravel\Scout\Searchable;
21
use Illuminate\Database\Eloquent\Collection;
22
use Illuminate\Database\Eloquent\SoftDeletes;
23
24
/**
25
 * @internal
26
 */
27
final class ModelsResolver
28
{
29
    /**
30
     * Get a set of models from the provided hits.
31
     *
32
     * @param \Laravel\Scout\Builder $builder
33
     * @param  string|object $searchable
34
     * @param  array $hits
35
     *
36
     * @return \Illuminate\Database\Eloquent\Collection
37
     */
38 4
    public function from(Builder $builder, $searchable, array $hits): Collection
39
    {
40 4
        $instances = collect();
41
42 4
        $ids = collect($hits)->pluck('objectID')->values()->all();
43
44 4
        $models = [];
45 4
        foreach ($ids as $id) {
46 4
            $modelClass = ObjectIdEncrypter::decryptSearchable($id);
47 3
            $modelKey = ObjectIdEncrypter::decryptSearchableKey($id);
48 3
            if (! array_key_exists($modelClass, $models)) {
49 3
                $models[$modelClass] = [];
50
            }
51
52 3
            $models[$modelClass][] = $modelKey;
53
        }
54
55 3
        foreach ($models as $modelClass => $modelKeys) {
56 3
            $model = new $modelClass;
57
58 3
            if (in_array(Searchable::class, class_uses_recursive($model), true)) {
59 3
                if (! empty($models = $model->getScoutModelsByIds($builder, $modelKeys))) {
60 3
                    $instances = $instances->merge($models);
61
                }
62
            } else {
63 1
                $query = in_array(SoftDeletes::class, class_uses_recursive($model),
64 1
                    true) ? $model->withTrashed() : $model->newQuery();
65
66 1
                if ($builder->queryCallback) {
67
                    call_user_func($builder->queryCallback, $query);
68
                }
69
70 1
                $scoutKey = method_exists($model,
71 1
                    'getScoutKeyName') ? $model->getScoutKeyName() : $model->getQualifiedKeyName();
72 1
                if ($models = $query->whereIn($scoutKey, $modelKeys)->get()) {
73 3
                    $instances = $instances->merge($models);
74
                }
75
            }
76
        }
77
78 3
        $result = $searchable->newCollection();
79
80 3
        foreach ($ids as $id) {
81 3
            foreach ($instances as $instance) {
82 3
                if (ObjectIdEncrypter::encrypt($instance) === ObjectIdEncrypter::withoutPart($id)) {
83 3
                    $result->push($instance);
84 3
                    break;
85
                }
86
            }
87
        }
88
89 3
        $hits = collect($hits)->keyBy('objectID');
90
91
        return $result->map(function ($model) use ($hits) {
92 3
            if ($hit = $hits->get(ObjectIdEncrypter::encrypt($model))) {
93 2
                foreach (Arr::only($hit, ['_highlightResult', '_rankingInfo']) as $key => $value) {
94
                    $model->withScoutMetadata($key, $value);
95
                }
96
            }
97
98 3
            return $model;
99 3
        });
100
    }
101
}
102