Test Failed
Push — develop ( 013d90...7b3751 )
by Nuno
03:38
created

DeleteJob::handle()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 16
ccs 0
cts 9
cp 0
rs 9.9666
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12
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\Jobs;
15
16
use Illuminate\Support\Collection;
17
use Algolia\AlgoliaSearch\Interfaces\ClientInterface;
0 ignored issues
show
Bug introduced by
The type Algolia\AlgoliaSearch\Interfaces\ClientInterface 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 Algolia\ScoutExtended\Searchable\ObjectIdEncrypter;
19
20
/**
21
 * @internal
22
 */
23
final class DeleteJob
24
{
25
    /**
26
     * @var \Illuminate\Support\Collection
27
     */
28
    private $searchables;
29
30
    /**
31
     * DeleteJob constructor.
32
     *
33
     * @param \Illuminate\Support\Collection $searchables
34
     *
35
     * @return void
36
     */
37
    public function __construct(Collection $searchables)
38
    {
39
        $this->searchables = $searchables;
40
    }
41
42
    /**
43
     * @param \Algolia\AlgoliaSearch\Interfaces\ClientInterface $client
44
     *
45
     * @return void
46
     */
47
    public function handle(ClientInterface $client): void
48
    {
49
        if ($this->searchables->isEmpty()) {
50
            return;
51
        }
52
53
        $index = $client->initIndex($this->searchables->first()->searchableAs());
54
55
        $result = $index->deleteBy([
56
            'tagFilters' => $this->searchables->map(function ($searchable) {
57
                return ObjectIdEncrypter::encrypt($searchable);
58
            })->toArray(),
59
        ]);
60
61
        if (config('scout.synchronous', false)) {
62
            $result->wait();
63
        }
64
    }
65
}
66