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 get_class; |
17
|
|
|
use Laravel\Scout\ModelObserver as BaseModelObserver; |
18
|
|
|
|
19
|
|
|
final class AggregatorObserver extends BaseModelObserver |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var array [ |
23
|
|
|
* '\App\Post' => [ |
24
|
|
|
* '\App\Search\NewsAggregator', |
25
|
|
|
* '\App\Search\BlogAggregator', |
26
|
|
|
* ] |
27
|
|
|
* ] |
28
|
|
|
*/ |
29
|
|
|
private $aggregators = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Set the aggregator. |
33
|
|
|
* |
34
|
|
|
* @param string $aggregator |
35
|
|
|
* @param string[] $models |
36
|
|
|
* |
37
|
|
|
* @return void |
38
|
|
|
*/ |
39
|
7 |
|
public function setAggregator(string $aggregator, array $models): void |
40
|
|
|
{ |
41
|
7 |
|
foreach ($models as $model) { |
42
|
7 |
|
if (! array_key_exists($model, $this->aggregators)) { |
43
|
7 |
|
$this->aggregators[$model] = []; |
44
|
|
|
} |
45
|
|
|
|
46
|
7 |
|
$this->aggregators[$model][] = $aggregator; |
47
|
|
|
} |
48
|
7 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
7 |
|
public function saved($model): void |
54
|
|
|
{ |
55
|
7 |
|
$class = get_class($model); |
56
|
|
|
|
57
|
7 |
|
if (! array_key_exists($class, $this->aggregators)) { |
58
|
|
|
return; |
59
|
|
|
} |
60
|
|
|
|
61
|
7 |
|
foreach ($this->aggregators[$class] as $aggregator) { |
62
|
7 |
|
parent::saved($aggregator::create($model)); |
63
|
|
|
} |
64
|
7 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
4 |
|
public function deleted($model): void |
70
|
|
|
{ |
71
|
4 |
|
if (static::syncingDisabledFor($model)) { |
72
|
|
|
return; |
73
|
|
|
} |
74
|
|
|
|
75
|
4 |
|
if ($this->usesSoftDelete($model) && config('scout.soft_delete', false)) { |
76
|
1 |
|
$this->saved($model); |
77
|
|
|
} else { |
78
|
3 |
|
$class = get_class($model); |
79
|
|
|
|
80
|
3 |
|
if (! array_key_exists($class, $this->aggregators)) { |
81
|
|
|
return; |
82
|
|
|
} |
83
|
|
|
|
84
|
3 |
|
foreach ($this->aggregators[$class] as $aggregator) { |
85
|
3 |
|
$aggregator::create($model)->unsearchable(); |
86
|
|
|
} |
87
|
|
|
} |
88
|
4 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Handle the force deleted event for the model. |
92
|
|
|
* |
93
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
2 |
|
public function forceDeleted($model): void |
97
|
|
|
{ |
98
|
2 |
|
if (static::syncingDisabledFor($model)) { |
99
|
|
|
return; |
100
|
|
|
} |
101
|
|
|
|
102
|
2 |
|
$class = get_class($model); |
103
|
|
|
|
104
|
2 |
|
if (! array_key_exists($class, $this->aggregators)) { |
105
|
|
|
return; |
106
|
|
|
} |
107
|
|
|
|
108
|
2 |
|
foreach ($this->aggregators[$class] as $aggregator) { |
109
|
2 |
|
$aggregator::create($model)->unsearchable(); |
110
|
|
|
} |
111
|
2 |
|
} |
112
|
|
|
} |
113
|
|
|
|