Passed
Pull Request — master (#147)
by Mozammil
03:35
created

ModelsResolver::from()   D

Complexity

Conditions 15
Paths 285

Size

Total Lines 59
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 15.0418

Importance

Changes 0
Metric Value
cc 15
eloc 35
nc 285
nop 3
dl 0
loc 59
ccs 33
cts 35
cp 0.9429
crap 15.0418
rs 4.0208
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
     * If the following metadata keys are present in the algolia result,
31
     * it will be made available with the resolved model.
32
     *
33
     */
34
    const METADATA = ['_highlightResult', '_rankingInfo'];
35
36
    /**
37
     * Get a set of models from the provided results.
38
     *
39
     * @param \Laravel\Scout\Builder $builder
40
     * @param  string|object $searchable
41
     * @param  array $results
42
     *
43
     * @return \Illuminate\Database\Eloquent\Collection
44
     */
45 4
    public function from(Builder $builder, $searchable, array $results): Collection
46
    {
47 4
        $instances = collect();
48 4
        $hits = collect($results['hits']);
49
50 4
        $models = [];
51 4
        $ids = $hits->pluck('objectID')->values()->all();
52 4
        foreach ($ids as $id) {
53 4
            $modelClass = ObjectIdEncrypter::decryptSearchable($id);
54 3
            $modelKey = ObjectIdEncrypter::decryptSearchableKey($id);
55 3
            if (! array_key_exists($modelClass, $models)) {
56 3
                $models[$modelClass] = [];
57
            }
58
59 3
            $models[$modelClass][] = $modelKey;
60
        }
61
62 3
        foreach ($models as $modelClass => $modelKeys) {
63 3
            $model = new $modelClass;
64
65 3
            if (in_array(Searchable::class, class_uses_recursive($model), true)) {
66 3
                if (! empty($models = $model->getScoutModelsByIds($builder, $modelKeys))) {
67 3
                    $instances = $instances->merge($models);
68
                }
69
            } else {
70 1
                $query = in_array(SoftDeletes::class, class_uses_recursive($model),
71 1
                    true) ? $model->withTrashed() : $model->newQuery();
72
73 1
                if ($builder->queryCallback) {
74
                    call_user_func($builder->queryCallback, $query);
75
                }
76
77 1
                $scoutKey = method_exists($model,
78 1
                    'getScoutKeyName') ? $model->getScoutKeyName() : $model->getQualifiedKeyName();
79 1
                if ($models = $query->whereIn($scoutKey, $modelKeys)->get()) {
80 3
                    $instances = $instances->merge($models);
81
                }
82
            }
83
        }
84
85 3
        $result = $searchable->newCollection();
86 3
        $hits = $hits->keyBy('objectID');
87
88 3
        foreach ($ids as $id) {
89 3
            foreach ($instances as $instance) {
90 3
                if (($instanceKey = ObjectIdEncrypter::encrypt($instance)) === ObjectIdEncrypter::withoutPart($id)) {
91 3
                    if ($hit = $hits->get($instanceKey)) {
92 2
                        foreach (Arr::only($hit, self::METADATA) as $metadataKey => $metadataValue) {
93
                            $instance->withScoutMetadata($metadataKey, $metadataValue);
94
                        }
95
                    }
96
97 3
                    $result->push($instance);
98 3
                    break;
99
                }
100
            }
101
        }
102
103 3
        return $result;
104
    }
105
}
106