Passed
Push — master ( fd883f...efe295 )
by Nuno
04:21
created

AlgoliaEngine   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 117
ccs 35
cts 35
cp 1
rs 10
c 0
b 0
f 0
wmc 14

9 Methods

Rating   Name   Duplication   Size   Complexity  
A update() 0 3 1
A delete() 0 3 1
A getClient() 0 3 1
A setClient() 0 3 1
A __construct() 0 3 1
A map() 0 9 2
A flush() 0 5 1
A mutateWheres() 0 12 3
A filters() 0 13 3
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\SearchClient;
18
use Algolia\ScoutExtended\Jobs\DeleteJob;
19
use Algolia\ScoutExtended\Jobs\UpdateJob;
20
use Illuminate\Database\Eloquent\Collection;
21
use Algolia\ScoutExtended\Searchable\ModelsResolver;
22
use Laravel\Scout\Engines\AlgoliaEngine as BaseAlgoliaEngine;
23
24
class AlgoliaEngine extends BaseAlgoliaEngine
25
{
26
    /**
27
     * The Algolia client.
28
     *
29
     * @var \Algolia\AlgoliaSearch\SearchClient
30
     */
31
    protected $algolia;
32
33
    /**
34
     * Create a new engine instance.
35
     *
36
     * @param  \Algolia\AlgoliaSearch\SearchClient $algolia
37
     * @return void
38
     */
39 36
    public function __construct(SearchClient $algolia)
40
    {
41 36
        $this->algolia = $algolia;
42 36
    }
43
44
    /**
45
     * @param \Algolia\AlgoliaSearch\SearchClient $algolia
46
     *
47
     * @return void
48
     */
49 31
    public function setClient($algolia): void
50
    {
51 31
        $this->algolia = $algolia;
52 31
    }
53
54
    /**
55
     * Get the client.
56
     *
57
     * @return \Algolia\AlgoliaSearch\SearchClient $algolia
58
     */
59 35
    public function getClient(): SearchClient
60
    {
61 35
        return $this->algolia;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 13
    public function update($searchables)
68
    {
69 13
        dispatch_now(new UpdateJob($searchables));
70 13
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 5
    public function delete($searchables)
76
    {
77 5
        dispatch_now(new DeleteJob($searchables));
78 5
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 7
    public function map(Builder $builder, $results, $searchable)
84
    {
85 7
        if (count($results['hits']) === 0) {
86 5
            return Collection::make();
87
        }
88
89 2
        $ids = collect($results['hits'])->pluck('objectID')->values()->all();
90
91 2
        return resolve(ModelsResolver::class)->from($builder, $searchable, $ids);
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97 2
    public function flush($model)
98
    {
99 2
        $index = $this->algolia->initIndex($model->searchableAs());
100
101 2
        $index->clearObjects();
102 2
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107 8
    protected function filters(Builder $builder): array
108
    {
109 8
        $operators = ['<', '<=', '=', '!=', '>=', '>'];
110
111 8
        $wheres = $this->mutateWheres($builder->wheres);
112
113
        return collect($wheres)->map(function ($value, $key) use ($operators) {
114 2
            if (ends_with($key, $operators) || starts_with($value, $operators)) {
115 1
                return $key.' '.$value;
116
            }
117
118 1
            return $key.'='.$value;
119 8
        })->values()->all();
120
    }
121
122
    /**
123
     * Mutate the given wheres.
124
     *
125
     * @param  array  $array
126
     *
127
     * @return array
128
     */
129 8
    private function mutateWheres(array $wheres): array
130
    {
131 8
        foreach ($wheres as $key => $where) {
132
            /*
133
             * Casts carbon instances to timestamp.
134
             */
135 2
            if ($where instanceof \Illuminate\Support\Carbon) {
136 2
                $wheres[$key] = $where->getTimestamp();
137
            }
138
        }
139
140 8
        return $wheres;
141
    }
142
}
143