Passed
Push — master ( 0b8656...f0f74b )
by Nuno
02:11 queued 11s
created

ModelsResolver::from()   D

Complexity

Conditions 14
Paths 285

Size

Total Lines 55
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 14.0059

Importance

Changes 0
Metric Value
cc 14
eloc 32
nc 285
nop 3
dl 0
loc 55
ccs 31
cts 32
cp 0.9688
crap 14.0059
rs 4.3708
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
     * @var string[]
31
     */
32
    private static $metadata = [
33
        '_highlightResult',
34
        '_rankingInfo',
35
    ];
36
37
    /**
38
     * Get a set of models from the provided results.
39
     *
40
     * @param \Laravel\Scout\Builder $builder
41
     * @param  string|object $searchable
42
     * @param  array $results
43
     *
44
     * @return \Illuminate\Database\Eloquent\Collection
45
     */
46 5
    public function from(Builder $builder, $searchable, array $results): Collection
47
    {
48 5
        $instances = collect();
49 5
        $hits = collect($results['hits'])->keyBy('objectID');
50
51 5
        $models = [];
52 5
        foreach ($hits->keys() as $id) {
53 5
            $modelClass = ObjectIdEncrypter::decryptSearchable((string) $id);
54 4
            $modelKey = ObjectIdEncrypter::decryptSearchableKey((string) $id);
55 4
            if (! array_key_exists($modelClass, $models)) {
56 4
                $models[$modelClass] = [];
57
            }
58
59 4
            $models[$modelClass][] = $modelKey;
60
        }
61
62 4
        foreach ($models as $modelClass => $modelKeys) {
63 4
            $model = new $modelClass;
64
65 4
            if (in_array(Searchable::class, class_uses_recursive($model), true)) {
66 4
                if (! empty($models = $model->getScoutModelsByIds($builder, $modelKeys))) {
67 4
                    $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 4
                    $instances = $instances->merge($models);
81
                }
82
            }
83
        }
84
85 4
        $result = $searchable->newCollection();
86
87 4
        foreach ($hits as $id => $hit) {
88 4
            foreach ($instances as $instance) {
89 4
                if (ObjectIdEncrypter::encrypt($instance) === ObjectIdEncrypter::withoutPart((string) $id)) {
90 4
                    foreach (Arr::only($hit, self::$metadata) as $metadataKey => $metadataValue) {
91 1
                        $instance->withScoutMetadata($metadataKey, $metadataValue);
92
                    }
93
94 4
                    $result->push($instance);
95 4
                    break;
96
                }
97
            }
98
        }
99
100 4
        return $result;
101
    }
102
}
103