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; |
15
|
|
|
use Illuminate\Database\Eloquent\Model; |
16
|
|
|
use Tinyissue\Model\Message; |
17
|
|
|
use Tinyissue\Model\Project\Issue; |
18
|
|
|
use Tinyissue\Model\User; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* CrudTrait is trait class containing the methods for adding/editing/deleting the message queue model. |
22
|
|
|
* |
23
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
trait CrudTrait |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Insert a record into the message queue. |
29
|
|
|
* |
30
|
|
|
* @param string $name |
31
|
|
|
* @param Model $model |
32
|
|
|
* @param int|User $changeBy |
33
|
|
|
* |
34
|
|
|
* @return void |
35
|
|
|
*/ |
36
|
32 |
|
public function queue($name, Model $model, $changeBy) |
37
|
|
|
{ |
38
|
|
|
// Get modified attributes |
39
|
32 |
|
$dirty = $model->getDirty(); |
40
|
32 |
|
$isNew = strpos($name, 'add_') === 0; |
41
|
|
|
|
42
|
|
|
// Stop if nothing changed |
43
|
32 |
|
if (!$model->isDirty() && !$isNew) { |
44
|
|
|
return; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// Get the original value of the modified attributes |
48
|
32 |
|
$origin = []; |
49
|
32 |
|
foreach ($dirty as $field => $value) { |
50
|
17 |
|
$origin[$field] = $model->getOriginal($field, $value); |
51
|
32 |
|
} |
52
|
|
|
|
53
|
|
|
// Fill and save to message queue |
54
|
32 |
|
$fill = $this->getFillAttributes($name, $model, $changeBy, [ |
55
|
32 |
|
'dirty' => $dirty, |
56
|
32 |
|
'origin' => $origin, |
57
|
32 |
|
]); |
58
|
|
|
|
59
|
32 |
|
return $this->fill($fill)->save(); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Insert a record into the message queue about a delete event. |
64
|
|
|
* |
65
|
|
|
* @param string $name |
66
|
|
|
* @param Model $model |
67
|
|
|
* @param int|User $changeBy |
68
|
|
|
* |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
2 |
|
public function queueDelete($name, Model $model, $changeBy) |
72
|
|
|
{ |
73
|
|
|
// Fill and save to message queue |
74
|
2 |
|
$fill = $this->getFillAttributes($name, $model, $changeBy, [ |
75
|
2 |
|
'dirty' => [], |
76
|
2 |
|
'origin' => $model->toArray(), |
77
|
2 |
|
]); |
78
|
|
|
|
79
|
2 |
|
return $this->fill($fill)->save(); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Insert records about tag changes into the message queue. |
84
|
|
|
* |
85
|
|
|
* @param Issue $issue |
86
|
|
|
* @param array $added |
87
|
|
|
* @param array $removed |
88
|
|
|
* @param User $changeBy |
89
|
|
|
* |
90
|
|
|
* @return mixed |
91
|
|
|
*/ |
92
|
2 |
|
public function queueIssueTagChanges(Issue $issue, array $added, array $removed, User $changeBy) |
93
|
|
|
{ |
94
|
|
|
// Fill and save to message queue |
95
|
2 |
|
$fill = $this->getFillAttributes(Message\Queue::CHANGE_TAG_ISSUE, $issue, $changeBy, [ |
96
|
2 |
|
'added' => $added, |
97
|
2 |
|
'removed' => $removed, |
98
|
2 |
|
]); |
99
|
|
|
|
100
|
2 |
|
return $this->fill($fill)->save(); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Returns an array containing the data needed for the message queue save. |
105
|
|
|
* |
106
|
|
|
* @param string $name |
107
|
|
|
* @param Model $model |
108
|
|
|
* @param int|User $changeBy |
109
|
|
|
* @param array $payload |
110
|
|
|
* |
111
|
|
|
* @return array |
112
|
|
|
*/ |
113
|
33 |
|
protected function getFillAttributes($name, Model $model, $changeBy, array $payload) |
114
|
|
|
{ |
115
|
33 |
|
$changeById = (int)($changeBy instanceof User ? $changeBy->id : $changeBy); |
116
|
|
|
|
117
|
33 |
|
$fill = []; |
118
|
33 |
|
$fill['event'] = $name; |
119
|
33 |
|
$fill['payload'] = $payload; |
120
|
33 |
|
$fill['model_type'] = get_class($model); |
121
|
33 |
|
$fill['model_id'] = $model->id; |
122
|
33 |
|
$fill['change_by_id'] = $changeById; |
123
|
|
|
|
124
|
33 |
|
return $fill; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.