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