Test Failed
Branch develop (dd818d)
by Nuno
03:56
created

ModelsResolver::from()   B

Complexity

Conditions 8
Paths 19

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 8.0109

Importance

Changes 0
Metric Value
cc 8
eloc 18
nc 19
nop 3
dl 0
loc 30
ccs 17
cts 18
cp 0.9444
crap 8.0109
rs 8.4444
c 0
b 0
f 0
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\Support\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\Support\Collection
36
     */
37 2
    public function from(Builder $builder, $searchable, array $ids): Collection
0 ignored issues
show
Unused Code introduced by
The parameter $searchable is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

37
    public function from(Builder $builder, /** @scrutinizer ignore-unused */ $searchable, array $ids): Collection

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
38
    {
39 2
        $instances = collect();
40
41 2
        foreach ($ids as $id) {
42 2
            $modelClass = ObjectIdEncrypter::decryptSearchable($id);
43 2
            $model = new $modelClass;
44 2
            $modelKey = ObjectIdEncrypter::decryptSearchableKey($id);
45
46 2
            if (in_array(Searchable::class, class_uses_recursive($model), true)) {
47 2
                if (! empty($models = $model->getScoutModelsByIds($builder, [$modelKey]))) {
48 2
                    $instances = $instances->merge($models);
49
                }
50
            } else {
51 1
                $query = in_array(SoftDeletes::class, class_uses_recursive($model),
52 1
                    true) ? $model->withTrashed() : $model->newQuery();
53
54 1
                if ($builder->queryCallback) {
55
                    call_user_func($builder->queryCallback, $query);
56
                }
57
58 1
                $scoutKey = method_exists($model,
59 1
                    'getScoutKeyName') ? $model->getScoutKeyName() : $model->getQualifiedKeyName();
60 1
                if ($instance = $query->where($scoutKey, $modelKey)->get()->first()) {
61 2
                    $instances->push($instance);
62
                }
63
            }
64
        }
65
66 2
        return $instances;
67
    }
68
}
69