Completed
Push — master ( 09b0d1...113822 )
by Nenad
03:04
created

TNTSearchEngine   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 11
Bugs 3 Features 0
Metric Value
wmc 14
c 11
b 3
f 0
lcom 1
cbo 5
dl 0
loc 151
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A update() 0 16 2
A delete() 0 10 1
A search() 0 4 1
A paginate() 0 4 1
A performSearch() 0 8 3
A filters() 0 6 1
A map() 0 14 2
A initIndex() 0 14 2
1
<?php
2
3
namespace TeamTNT\Scout\Engines;
4
5
use Illuminate\Database\Eloquent\Collection;
6
use Laravel\Scout\Builder;
7
use Laravel\Scout\Engines\Engine;
8
use TeamTNT\TNTSearch\TNTSearch;
9
10
class TNTSearchEngine extends Engine
11
{
12
    /**
13
     * Create a new engine instance.
14
     *
15
     * @param TeamTNT\TNTSearch\TNTSearch $tnt
16
     *
17
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
18
     */
19
    public function __construct(TNTSearch $tnt)
20
    {
21
        $this->tnt = $tnt;
0 ignored issues
show
Bug introduced by
The property tnt does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
22
    }
23
24
    /**
25
     * Update the given model in the index.
26
     *
27
     * @param Collection $models
28
     *
29
     * @return void
30
     */
31
    public function update($models)
32
    {
33
        $this->initIndex($models->first());
34
        $models->each(function ($model) {
35
            $searchableFields = $model->toSearchableArray();
36
37
            $this->tnt->selectIndex("{$model->searchableAs()}.index");
38
            $index = $this->tnt->getIndex();
39
            $index->setPrimaryKey($model->getKeyName());
40
            if ($model->getKey()) {
41
                $index->update($model->getKey(), $searchableFields);
42
            } else {
43
                $index->insert($searchableFields);
44
            }
45
        });
46
    }
47
48
    /**
49
     * Remove the given model from the index.
50
     *
51
     * @param Collection $models
52
     *
53
     * @return void
54
     */
55
    public function delete($models)
56
    {
57
        $this->initIndex($models->first());
58
        $models->each(function ($model) {
59
            $this->tnt->selectIndex("{$model->searchableAs()}.index");
60
            $index = $this->tnt->getIndex();
61
            $index->setPrimaryKey($model->getKeyName());
62
            $index->delete($model->id);
63
        });
64
    }
65
66
    /**
67
     * Perform the given search on the engine.
68
     *
69
     * @param Builder $builder
70
     *
71
     * @return mixed
72
     */
73
    public function search(Builder $builder)
74
    {
75
        return $this->performSearch($builder);
76
    }
77
78
    /**
79
     * Perform the given search on the engine.
80
     *
81
     * @param Builder $builder
82
     * @param int     $perPage
83
     * @param int     $page
84
     *
85
     * @return mixed
86
     */
87
    public function paginate(Builder $builder, $perPage, $page)
88
    {
89
        return $this->performSearch($builder);
90
    }
91
92
    /**
93
     * Perform the given search on the engine.
94
     *
95
     * @param Builder $builder
96
     * @param array   $options
97
     *
98
     * @return mixed
99
     */
100
    protected function performSearch(Builder $builder, array $options = [])
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
101
    {
102
        $index = $builder->index ?: $builder->model->searchableAs();
103
        $limit = $builder->limit ?: 10;
104
        $this->tnt->selectIndex("{$index}.index");
105
106
        return $this->tnt->search($builder->query, $limit);
107
    }
108
109
    /**
110
     * Get the filter array for the query.
111
     *
112
     * @param Builder $builder
113
     *
114
     * @return array
115
     */
116
    protected function filters(Builder $builder)
117
    {
118
        return collect($builder->wheres)->map(function ($value, $key) {
119
            return $key . '=' . $value;
120
        })->values()->all();
121
    }
122
123
    /**
124
     * Map the given results to instances of the given model.
125
     *
126
     * @param mixed                               $results
127
     * @param \Illuminate\Database\Eloquent\Model $model
128
     *
129
     * @return Collection
130
     */
131
    public function map($results, $model)
132
    {
133
        if (count($results['ids']) === 0) {
134
            return Collection::make();
135
        }
136
        $keys   = collect($results['ids']);
137
        $models = $model->whereIn(
138
            $model->getKeyName(), $keys
139
        )->get()->keyBy($model->getKeyName());
140
141
        return collect($results['ids'])->map(function ($hit) use ($models) {
142
            return $models[$hit];
143
        });
144
    }
145
146
    public function initIndex($model)
147
    {
148
        $indexName = $model->searchableAs();
149
150
        if (!file_exists($this->tnt->config['storage'] . "/{$indexName}.index")) {
151
            $indexer = $this->tnt->createIndex("$indexName.index");
152
            $indexer->setDatabaseHandle($model->getConnection()->getPdo());
153
            $indexer->disableOutput = true;
154
            $indexer->setPrimaryKey($model->getKeyName());
155
            $fields = implode(', ', $model->searchable);
156
            $indexer->query("SELECT {$model->getKeyName()}, $fields FROM $indexName WHERE {$model->getKeyName()} = {$model->getKey()}");
157
            $indexer->run();
158
        }
159
    }
160
}
161