Test Failed
Push — develop ( 3663ad...1f9f00 )
by Nuno
04:47
created

Aggregator::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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\Searchable;
15
16
use function in_array;
17
use Laravel\Scout\Builder;
18
use Laravel\Scout\Searchable;
19
use Illuminate\Support\Collection;
20
use Illuminate\Database\Eloquent\Model;
21
use Illuminate\Database\Eloquent\SoftDeletes;
22
use Algolia\ScoutExtended\Contracts\SearchableCountableContract;
23
use Algolia\ScoutExtended\Exceptions\ModelNotDefinedInAggregatorException;
24
25
abstract class Aggregator implements SearchableCountableContract
26
{
27
    use Searchable;
0 ignored issues
show
Bug introduced by
The trait Laravel\Scout\Searchable requires the property $queryCallback which is not provided by Algolia\ScoutExtended\Searchable\Aggregator.
Loading history...
28
29
    /**
30
     * The names of the models that should be aggregated.
31
     *
32
     * @var string[]
33
     */
34
    protected $models = [];
35
36
    /**
37
     * The model being queried, if any.
38
     *
39
     * @var \Illuminate\Database\Eloquent\Model|null
40
     */
41
    protected $model;
42
43
    /**
44
     * Returns the index name.
45
     *
46
     * @var string
47
     */
48
    protected $indexName;
49
50
    /**
51
     * Boot the aggregator.
52
     *
53
     * @return void
54
     */
55 6
    public static function bootSearchable(): void
56
    {
57 6
        ($self = new static)->registerSearchableMacros();
58
59 6
        $observer = tap(resolve(AggregatorObserver::class))->setAggregator(static::class, $models = $self->getModels());
60
61 6
        foreach ($models as $model) {
62 6
            $model::observe($observer);
63
        }
64 6
    }
65
66
    /**
67
     * Creates an instance of the aggregator.
68
     *
69
     * @param \Illuminate\Database\Eloquent\Model $model
70
     *
71
     * @return \Algolia\ScoutExtended\Searchable\Aggregator
72
     */
73 1
    public static function create(Model $model): Aggregator
74
    {
75 1
        return (new static)->setModel($model);
76
    }
77
78
    /**
79
     * Get the names of the models that should be aggregated.
80
     *
81
     * @return string[]
82
     */
83 6
    public function getModels(): array
84
    {
85 6
        return $this->models;
86
    }
87
88
    /**
89
     * Get the model instance being queried.
90
     *
91
     * @return \Illuminate\Database\Eloquent\Model
92
     *
93
     * @throws \Algolia\ScoutExtended\Exceptions\ModelNotDefinedInAggregatorException
94
     */
95
    public function getModel(): Model
96
    {
97
        if ($this->model === null) {
98
            throw new ModelNotDefinedInAggregatorException();
99
        }
100
101
        return $this->model;
102
    }
103
104
    /**
105
     * Set a model instance for the model being queried.
106
     *
107
     * @param \Illuminate\Database\Eloquent\Model $model
108
     *
109
     * @return \Algolia\ScoutExtended\Searchable\Aggregator
110
     */
111 1
    public function setModel(Model $model): Aggregator
112
    {
113 1
        $this->model = $model;
114
115 1
        return $this;
116
    }
117
118
    /**
119
     * Get the requested models from an array of object IDs.
120
     *
121
     * @param  \Laravel\Scout\Builder $builder
122
     * @param  array $ids
123
     *
124
     * @return \Illuminate\Support\Collection
125
     */
126
    public function getScoutModelsByIds(Builder $builder, array $ids): Collection
127
    {
128
        return resolve(ModelsResolver::class)->from($builder, static::class, $this->models, $ids);
129
    }
130
131
    /**
132
     * Get the value used to index the model.
133
     *
134
     * @return string
135
     */
136
    public function getScoutKey(): string
137
    {
138
        if ($this->model === null) {
139
            throw new ModelNotDefinedInAggregatorException();
140
        }
141
142
        return ObjectIdEncrypter::encrypt($this->model);
143
    }
144
145
    /**
146
     * Get the index name for the searchable.
147
     *
148
     * @return string
149
     */
150
    public function searchableAs(): string
151
    {
152
        return config('scout.prefix').UuidGenerator::getUuid(static::class);
153
    }
154
155
    /**
156
     * Get the searchable array of the searchable.
157
     *
158
     * @return array
159
     */
160
    public function toSearchableArray(): array
161
    {
162
        if ($this->model === null) {
163
            throw new ModelNotDefinedInAggregatorException();
164
        }
165
166
        return method_exists($this->model,
0 ignored issues
show
Bug Best Practice introduced by
The expression return method_exists($th...$this->model->toArray() could return the type Illuminate\Database\Eloquent\Builder which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
167
            'toSearchableArray') ? $this->model->toSearchableArray() : $this->model->toArray();
168
    }
169
170
    /**
171
     * Make all instances of the model searchable.
172
     *
173
     * @return void
174
     */
175
    public static function makeAllSearchable()
176
    {
177
        foreach ((new static)->getModels() as $model) {
178
            $instance = new $model;
179
180
            $softDeletes = in_array(SoftDeletes::class, class_uses_recursive($model)) && config('scout.soft_delete',
181
                    false);
182
183
            $instance->newQuery()->when($softDeletes, function ($query) {
184
                $query->withTrashed();
185
            })->orderBy($instance->getKeyName())->get()->map(function ($model) {
186
                return static::create($model);
187
            })->searchable();
188
        }
189
    }
190
191
    /**
192
     * Remove all instances of the model from the search index.
193
     *
194
     * @return void
195
     */
196
    public static function removeAllFromSearch(): void
197
    {
198
        foreach ((new static)->getModels() as $model) {
199
            $instance = new $model;
200
201
            $instance->newQuery()->orderBy($instance->getKeyName())->get()->map(function ($model) {
202
                return static::create($model);
203
            })->unsearchable();
204
        }
205
    }
206
207
    /**
208
     * {@inheritdoc}
209
     */
210
    public function getSearchableCount(): int
211
    {
212
        $count = 0;
213
214
        foreach ($this->getModels() as $model) {
215
            $softDeletes = in_array(SoftDeletes::class, class_uses_recursive($model),
216
                    true) && config('scout.soft_delete', false);
217
218
            $count += $model::query()->when($softDeletes, function ($query) {
219
                $query->withTrashed();
220
            })->count();
221
        }
222
223
        return (int) $count;
224
    }
225
}
226