|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Tinyissue package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Mohamed Alsharaf <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Tinyissue\Model\Traits\Message\Queue; |
|
13
|
|
|
|
|
14
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
15
|
|
|
use Tinyissue\Model\Message; |
|
16
|
|
|
use Tinyissue\Model\Project\Issue; |
|
17
|
|
|
use Tinyissue\Model\User; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* CrudTrait is trait class containing the methods for adding/editing/deleting the message queue model. |
|
21
|
|
|
* |
|
22
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
|
23
|
|
|
* |
|
24
|
|
|
* @property static $this |
|
25
|
|
|
*/ |
|
26
|
|
|
trait CrudTrait |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* Insert a record into the message queue. |
|
30
|
|
|
* |
|
31
|
|
|
* @param string $name |
|
32
|
|
|
* @param Model $model |
|
33
|
|
|
* @param int|User $changeBy |
|
34
|
|
|
* |
|
35
|
|
|
* @return void |
|
36
|
|
|
*/ |
|
37
|
|
|
public function queue($name, Model $model, $changeBy) |
|
38
|
|
|
{ |
|
39
|
|
|
// Get modified attributes |
|
40
|
|
|
$dirty = $model->getDirty(); |
|
41
|
|
|
$isNew = strpos($name, 'add_') === 0; |
|
42
|
|
|
|
|
43
|
|
|
// Stop if nothing changed |
|
44
|
|
|
if (!$model->isDirty() && !$isNew) { |
|
45
|
|
|
return; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
// Get the original value of the modified attributes |
|
49
|
|
|
$origin = []; |
|
50
|
|
|
foreach ($dirty as $field => $value) { |
|
51
|
|
|
$origin[$field] = $model->getOriginal($field, $value); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
// Fill and save to message queue |
|
55
|
|
|
$fill = $this->getFillAttributes($name, $model, $changeBy, [ |
|
56
|
|
|
'dirty' => $dirty, |
|
57
|
|
|
'origin' => $origin, |
|
58
|
|
|
]); |
|
59
|
|
|
|
|
60
|
|
|
return $this->fill($fill)->save(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Insert a record into the message queue about a delete event. |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $name |
|
67
|
|
|
* @param Model $model |
|
68
|
|
|
* @param int|User $changeBy |
|
69
|
|
|
* |
|
70
|
|
|
* @return void |
|
71
|
|
|
*/ |
|
72
|
|
|
public function queueDelete($name, Model $model, $changeBy) |
|
73
|
|
|
{ |
|
74
|
|
|
// Fill and save to message queue |
|
75
|
|
|
$fill = $this->getFillAttributes($name, $model, $changeBy, [ |
|
76
|
|
|
'dirty' => [], |
|
77
|
|
|
'origin' => $model->toArray(), |
|
78
|
|
|
]); |
|
79
|
|
|
|
|
80
|
|
|
return $this->fill($fill)->save(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Insert records about tag changes into the message queue. |
|
85
|
|
|
* |
|
86
|
|
|
* @param Issue $issue |
|
87
|
|
|
* @param array $added |
|
88
|
|
|
* @param array $removed |
|
89
|
|
|
* @param User $changeBy |
|
90
|
|
|
* |
|
91
|
|
|
* @return mixed |
|
92
|
|
|
*/ |
|
93
|
|
|
public function queueIssueTagChanges(Issue $issue, array $added, array $removed, User $changeBy) |
|
94
|
|
|
{ |
|
95
|
|
|
// Fill and save to message queue |
|
96
|
|
|
$fill = $this->getFillAttributes(Message\Queue::CHANGE_TAG_ISSUE, $issue, $changeBy, [ |
|
97
|
|
|
'added' => $added, |
|
98
|
|
|
'removed' => $removed, |
|
99
|
|
|
]); |
|
100
|
|
|
|
|
101
|
|
|
return $this->fill($fill)->save(); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Returns an array containing the data needed for the message queue save. |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $name |
|
108
|
|
|
* @param Model $model |
|
109
|
|
|
* @param int|User $changeBy |
|
110
|
|
|
* @param array $payload |
|
111
|
|
|
* |
|
112
|
|
|
* @return array |
|
113
|
|
|
*/ |
|
114
|
|
|
protected function getFillAttributes($name, Model $model, $changeBy, array $payload) |
|
115
|
|
|
{ |
|
116
|
|
|
$changeById = (int) ($changeBy instanceof User ? $changeBy->id : $changeBy); |
|
117
|
|
|
|
|
118
|
|
|
$fill = []; |
|
119
|
|
|
$fill['event'] = $name; |
|
120
|
|
|
$fill['payload'] = $payload; |
|
121
|
|
|
$fill['model_type'] = get_class($model); |
|
122
|
|
|
$fill['model_id'] = $model->id; |
|
123
|
|
|
$fill['change_by_id'] = $changeById; |
|
124
|
|
|
|
|
125
|
|
|
return $fill; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
abstract public function fill(array $attributes); |
|
129
|
|
|
} |
|
130
|
|
|
|