DeleteJob::handle()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.0123

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 18
ccs 8
cts 9
cp 0.8889
crap 3.0123
rs 9.9332
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 Algolia\AlgoliaSearch\SearchClient;
17
use Algolia\ScoutExtended\Searchable\ObjectIdEncrypter;
18
use Illuminate\Support\Collection;
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 21
    public function __construct(Collection $searchables)
38
    {
39 21
        $this->searchables = $searchables;
40 21
    }
41
42
    /**
43
     * @param \Algolia\AlgoliaSearch\SearchClient $client
44
     *
45
     * @return void
46
     */
47 21
    public function handle(SearchClient $client): void
48
    {
49 21
        if ($this->searchables->isEmpty()) {
50 15
            return;
51
        }
52
53 11
        $index = $client->initIndex($this->searchables->first()->searchableAs());
54
55 11
        $result = $index->deleteBy([
56
            'tagFilters' => [
57
                $this->searchables->map(function ($searchable) {
58 11
                    return ObjectIdEncrypter::encrypt($searchable);
59 11
                })->toArray(),
60
            ],
61
        ]);
62
63 11
        if (config('scout.synchronous', false)) {
64
            $result->wait();
65
        }
66 11
    }
67
}
68