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\Repository\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
|
|
|
use Tinyissue\Repository\RepositoryUpdater; |
19
|
|
|
|
20
|
|
|
class Updater extends RepositoryUpdater |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var Message\Queue |
24
|
|
|
*/ |
25
|
|
|
protected $model; |
26
|
|
|
|
27
|
|
|
public function __construct(Message\Queue $model) |
28
|
|
|
{ |
29
|
|
|
$this->model = $model; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Insert a record into the message queue. |
34
|
|
|
* |
35
|
|
|
* @param string $name |
36
|
|
|
* @param Model $model |
37
|
|
|
* @param int|User $changeBy |
38
|
|
|
* |
39
|
|
|
* @return void |
40
|
|
|
*/ |
41
|
|
|
public function queue($name, Model $model, $changeBy) |
42
|
|
|
{ |
43
|
|
|
// Get modified attributes |
44
|
|
|
$dirty = $model->getDirty(); |
45
|
|
|
$isNew = strpos($name, 'add_') === 0; |
46
|
|
|
|
47
|
|
|
// Stop if nothing changed |
48
|
|
|
if (!$model->isDirty() && !$isNew) { |
49
|
|
|
return; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// Get the original value of the modified attributes |
53
|
|
|
$origin = []; |
54
|
|
|
foreach ($dirty as $field => $value) { |
55
|
|
|
$origin[$field] = $model->getOriginal($field, $value); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// Fill and save to message queue |
59
|
|
|
$fill = $this->getFillAttributes($name, $model, $changeBy, [ |
60
|
|
|
'dirty' => $dirty, |
61
|
|
|
'origin' => $origin, |
62
|
|
|
]); |
63
|
|
|
|
64
|
|
|
return $this->model->fill($fill)->save(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Insert a record into the message queue about a delete event. |
69
|
|
|
* |
70
|
|
|
* @param string $name |
71
|
|
|
* @param Model $model |
72
|
|
|
* @param int|User $changeBy |
73
|
|
|
* |
74
|
|
|
* @return void |
75
|
|
|
*/ |
76
|
|
|
public function queueDelete($name, Model $model, $changeBy) |
77
|
|
|
{ |
78
|
|
|
// Fill and save to message queue |
79
|
|
|
$fill = $this->getFillAttributes($name, $model, $changeBy, [ |
80
|
|
|
'dirty' => [], |
81
|
|
|
'origin' => $model->toArray(), |
82
|
|
|
]); |
83
|
|
|
|
84
|
|
|
return $this->model->fill($fill)->save(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Insert records about tag changes into the message queue. |
89
|
|
|
* |
90
|
|
|
* @param Issue $issue |
91
|
|
|
* @param array $added |
92
|
|
|
* @param array $removed |
93
|
|
|
* @param User $changeBy |
94
|
|
|
* |
95
|
|
|
* @return mixed |
96
|
|
|
*/ |
97
|
|
|
public function queueIssueTagChanges(Issue $issue, array $added, array $removed, User $changeBy) |
98
|
|
|
{ |
99
|
|
|
// Fill and save to message queue |
100
|
|
|
$fill = $this->getFillAttributes(Message\Queue::CHANGE_TAG_ISSUE, $issue, $changeBy, [ |
101
|
|
|
'added' => $added, |
102
|
|
|
'removed' => $removed, |
103
|
|
|
]); |
104
|
|
|
|
105
|
|
|
return $this->model->fill($fill)->save(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Returns an array containing the data needed for the message queue save. |
110
|
|
|
* |
111
|
|
|
* @param string $name |
112
|
|
|
* @param Model $model |
113
|
|
|
* @param int|User $changeBy |
114
|
|
|
* @param array $payload |
115
|
|
|
* |
116
|
|
|
* @return array |
117
|
|
|
*/ |
118
|
|
|
protected function getFillAttributes($name, Model $model, $changeBy, array $payload) |
119
|
|
|
{ |
120
|
|
|
$changeById = (int) ($changeBy instanceof User ? $changeBy->id : $changeBy); |
121
|
|
|
|
122
|
|
|
$fill = []; |
123
|
|
|
$fill['event'] = $name; |
124
|
|
|
$fill['payload'] = $payload; |
125
|
|
|
$fill['model_type'] = get_class($model); |
126
|
|
|
$fill['model_id'] = $model->id; |
127
|
|
|
$fill['change_by_id'] = $changeById; |
128
|
|
|
|
129
|
|
|
return $fill; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|