Passed
Pull Request — master (#147)
by Mozammil
06:17 queued 02:37
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
    const METADATA = ['_highlightResult', '_rankingInfo'];
34
35
    /**
36
     * Get a set of models from the provided results.
37
     *
38
     * @param \Laravel\Scout\Builder $builder
39
     * @param  string|object $searchable
40
     * @param  array $results
41
     *
42
     * @return \Illuminate\Database\Eloquent\Collection
43
     */
44 4
    public function from(Builder $builder, $searchable, array $results): Collection
45
    {
46 4
        $instances = collect();
47 4
        $hits = collect($results['hits']);
48
49 4
        $models = [];
50 4
        $ids = $hits->pluck('objectID')->values()->all();
51 4
        foreach ($ids as $id) {
52 4
            $modelClass = ObjectIdEncrypter::decryptSearchable($id);
53 3
            $modelKey = ObjectIdEncrypter::decryptSearchableKey($id);
54 3
            if (! array_key_exists($modelClass, $models)) {
55 3
                $models[$modelClass] = [];
56
            }
57
58 3
            $models[$modelClass][] = $modelKey;
59
        }
60
61 3
        foreach ($models as $modelClass => $modelKeys) {
62 3
            $model = new $modelClass;
63
64 3
            if (in_array(Searchable::class, class_uses_recursive($model), true)) {
65 3
                if (! empty($models = $model->getScoutModelsByIds($builder, $modelKeys))) {
66 3
                    $instances = $instances->merge($models);
67
                }
68
            } else {
69 1
                $query = in_array(SoftDeletes::class, class_uses_recursive($model),
70 1
                    true) ? $model->withTrashed() : $model->newQuery();
71
72 1
                if ($builder->queryCallback) {
73
                    call_user_func($builder->queryCallback, $query);
74
                }
75
76 1
                $scoutKey = method_exists($model,
77 1
                    'getScoutKeyName') ? $model->getScoutKeyName() : $model->getQualifiedKeyName();
78 1
                if ($models = $query->whereIn($scoutKey, $modelKeys)->get()) {
79 3
                    $instances = $instances->merge($models);
80
                }
81
            }
82
        }
83
84 3
        $result = $searchable->newCollection();
85 3
        $hits = $hits->keyBy('objectID');
86
87 3
        foreach ($ids as $id) {
88 3
            foreach ($instances as $instance) {
89 3
                if (($instanceKey = ObjectIdEncrypter::encrypt($instance)) === ObjectIdEncrypter::withoutPart($id)) {
90 3
                    if ($hit = $hits->get($instanceKey)) {
91 2
                        foreach (Arr::only($hit, self::METADATA) as $metadataKey => $metadataValue) {
92
                            $instance->withScoutMetadata($metadataKey, $metadataValue);
93
                        }
94
                    }
95
96 3
                    $result->push($instance);
97 3
                    break;
98
                }
99
            }
100
        }
101
102 3
        return $result;
103
    }
104
}
105