1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: mina |
5
|
|
|
* Date: 3/20/17 |
6
|
|
|
* Time: 4:04 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace MAbadir\ElasticLaravel\Observers; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use MAbadir\ElasticLaravel\Jobs\IndexModel; |
13
|
|
|
|
14
|
|
|
class ModelObserver |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Runs when a model is created |
18
|
|
|
* @param $model |
19
|
|
|
*/ |
20
|
|
View Code Duplication |
public function created($model) |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
$params = [ |
23
|
|
|
'type' => $model->getIndexType(), |
24
|
|
|
'id' => $model->getIndexId(), |
25
|
|
|
'body' => $model->getSearchableIndex(), |
26
|
|
|
]; |
27
|
|
|
dispatch(new IndexModel($model, $params,'index')); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Runs when a model is updated |
32
|
|
|
* @param $model |
33
|
|
|
*/ |
34
|
|
View Code Duplication |
public function updated($model) |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
$params = [ |
37
|
|
|
'type' => $model->getIndexType(), |
38
|
|
|
'id' => $model->getIndexId(), |
39
|
|
|
'body' => [ |
40
|
|
|
'doc' => $model->getSearchableIndex() |
41
|
|
|
], |
42
|
|
|
]; |
43
|
|
|
dispatch(new IndexModel($model, $params,'update')); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Runs when a model is updated |
48
|
|
|
* @param $model |
49
|
|
|
*/ |
50
|
|
View Code Duplication |
public function restored($model) |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
$params = [ |
53
|
|
|
'type' => $model->getIndexType(), |
54
|
|
|
'id' => $model->getIndexId(), |
55
|
|
|
'body' => $model->getSearchableIndex(), |
56
|
|
|
]; |
57
|
|
|
dispatch(new IndexModel($model, $params,'create')); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Runs when a model is deleted |
62
|
|
|
* @param $model |
63
|
|
|
*/ |
64
|
|
|
public function deleting($model) |
65
|
|
|
{ |
66
|
|
|
$params = [ |
67
|
|
|
'type' => $model->getIndexType(), |
68
|
|
|
'id' => $model->getIndexId(), |
69
|
|
|
]; |
70
|
|
|
dispatch(new IndexModel($model, $params,'delete')); |
71
|
|
|
} |
72
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.