Test Failed
Push — develop ( 1f9f00...7ffe68 )
by Nuno
03:22
created

AlgoliaEngine::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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 Algolia\ScoutExtended\Searchable\ModelsResolver;
17
use Algolia\ScoutExtended\Searchable\ObjectIdEncrypter;
18
use Laravel\Scout\Builder;
19
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...
20
use Illuminate\Database\Eloquent\Collection;
21
use Algolia\ScoutExtended\Searchable\ObjectsResolver;
22
use Laravel\Scout\Engines\AlgoliaEngine as BaseAlgoliaEngine;
23
24
class AlgoliaEngine extends BaseAlgoliaEngine
25
{
26
    /**
27
     * @var \Algolia\ScoutExtended\Searchable\ObjectsResolver
28
     */
29
    private $objectsResolver;
30
31
    /**
32
     * AlgoliaEngine constructor.
33
     *
34
     * @param \Algolia\AlgoliaSearch\Client $algolia
35
     * @param \Algolia\ScoutExtended\Searchable\ObjectsResolver $objectsResolver
36
     *
37
     */
38
    public function __construct(
39
        Algolia $algolia,
40
        ObjectsResolver $objectsResolver
41
    ) {
42
        parent::__construct($algolia);
43
44
        $this->objectsResolver = $objectsResolver;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function update($searchables)
51
    {
52
        if ($searchables->isEmpty()) {
53
            return;
54
        }
55
56
        $index = $this->algolia->initIndex($searchables->first()->searchableAs());
57
58
        if ($this->usesSoftDelete($searchables->first()) && config('scout.soft_delete', false)) {
59
            $searchables->each->pushSoftDeleteMetadata();
60
        }
61
62
        $objects = $this->objectsResolver->toUpdate($searchables);
63
        $index->saveObjects(collect($objects)->filter()->values()->all());
64
    }
65
66
    /**
67
     * Remove the given model from the index.
68
     *
69
     * @param  \Illuminate\Database\Eloquent\Collection $models
70
     * @return void
71
     */
72
    public function delete($models)
73
    {
74
        $index = $this->algolia->initIndex($models->first()->searchableAs());
75
76
        $index->deleteObjects($models->map(function ($model) {
77
            return ObjectIdEncrypter::encrypt($model);
78
        })->values()->all());
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function map(Builder $builder, $results, $searchable)
85
    {
86
        if (count($results['hits']) === 0) {
87
            return Collection::make();
88
        }
89
90
        $ids = collect($results['hits'])->pluck('objectID')->values()->all();
91
92
        return resolve(ModelsResolver::class)->from($builder, $searchable, $ids);
93
    }
94
}
95