Test Failed
Push — develop ( 3663ad...1f9f00 )
by Nuno
04:47
created

AlgoliaEngine::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 7
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
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\Engines;
15
16
use Laravel\Scout\Builder;
17
use Algolia\AlgoliaSearch\Client as Algolia;
0 ignored issues
show
Bug introduced by
The type Algolia\AlgoliaSearch\Client was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use Illuminate\Database\Eloquent\Collection;
19
use Algolia\ScoutExtended\Searchable\ObjectsResolver;
20
use Laravel\Scout\Engines\AlgoliaEngine as BaseAlgoliaEngine;
21
22
class AlgoliaEngine extends BaseAlgoliaEngine
23
{
24
    /**
25
     * @var \Algolia\ScoutExtended\Searchable\ObjectsResolver
26
     */
27
    private $objectsResolver;
28
29
    /**
30
     * AlgoliaEngine constructor.
31
     *
32
     * @param \Algolia\AlgoliaSearch\Client $algolia
33
     * @param \Algolia\ScoutExtended\Searchable\ObjectsResolver $objectsResolver
34
     *
35
     */
36
    public function __construct(
37
        Algolia $algolia,
38
        ObjectsResolver $objectsResolver
39
    ) {
40
        parent::__construct($algolia);
41
42
        $this->objectsResolver = $objectsResolver;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function update($searchables)
49
    {
50
        if ($searchables->isEmpty()) {
51
            return;
52
        }
53
54
        $index = $this->algolia->initIndex($searchables->first()->searchableAs());
55
56
        if ($this->usesSoftDelete($searchables->first()) && config('scout.soft_delete', false)) {
57
            $searchables->each->pushSoftDeleteMetadata();
58
        }
59
60
        $objects = $this->objectsResolver->toUpdate($searchables);
61
        $index->saveObjects(collect($objects)->filter()->values()->all());
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function map(Builder $builder, $results, $searchable)
68
    {
69
        if (count($results['hits']) === 0) {
70
            return Collection::make();
71
        }
72
73
        $searchables = $searchable->getScoutModelsByIds($builder,
74
            collect($results['hits'])->pluck('objectID')->values()->all())->keyBy(function ($searchable) {
75
            return $searchable->getScoutKey();
76
        })->map->getModel();
77
78
        return Collection::make($results['hits'])->map(function ($hit) use ($searchables) {
79
            if (isset($searchables[$hit['objectID']])) {
80
                return $searchables[$hit['objectID']];
81
            }
82
        })->filter()->values();
83
    }
84
}
85