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\Project\Issue\Comment; |
13
|
|
|
|
14
|
|
|
use Illuminate\Support\Collection; |
15
|
|
|
use Tinyissue\Model\Message\Queue; |
16
|
|
|
use Tinyissue\Model\Project\Issue; |
17
|
|
|
use Tinyissue\Model\Project; |
18
|
|
|
use Tinyissue\Model\User; |
19
|
|
|
use Tinyissue\Services\SendMessagesAbstract; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* SendMessages is a class to manage & process of sending messages about comment changes. |
23
|
|
|
* |
24
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class SendMessages extends SendMessagesAbstract |
27
|
|
|
{ |
28
|
|
|
protected $template = 'update_comment'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Returns an instance of Issue. |
32
|
|
|
* |
33
|
|
|
* @return bool |
34
|
1 |
|
*/ |
35
|
|
View Code Duplication |
protected function getIssue() |
|
|
|
|
36
|
1 |
|
{ |
37
|
1 |
|
if (!$this->issue instanceof Issue) { |
38
|
1 |
|
if ($this->getModel()) { |
39
|
|
|
$this->issue = $this->getModel()->issue; |
40
|
|
|
} else { |
41
|
1 |
|
// Possible deleted comment |
42
|
1 |
|
$issueId = $this->latestMessage->getDataFromPayload('origin.issue.id'); |
43
|
|
|
$this->issue = (new Issue())->find($issueId); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
} |
46
|
1 |
|
|
47
|
|
|
return $this->issue; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Returns an instance of Project. |
52
|
|
|
* |
53
|
|
|
* @return Project |
54
|
1 |
|
*/ |
55
|
|
|
protected function getProject() |
56
|
1 |
|
{ |
57
|
|
|
return $this->getIssue()->project; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Returns the project id. |
62
|
|
|
* |
63
|
|
|
* @return int |
64
|
1 |
|
*/ |
65
|
|
|
protected function getProjectId() |
66
|
1 |
|
{ |
67
|
|
|
return $this->getIssue()->project_id; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Returns message data belongs to adding a comment. |
72
|
|
|
* |
73
|
|
|
* @param Queue $queue |
74
|
|
|
* |
75
|
|
|
* @return array |
76
|
1 |
|
*/ |
77
|
|
|
protected function getMessageDataForAddComment(Queue $queue) |
78
|
1 |
|
{ |
79
|
1 |
|
$messageData = []; |
80
|
1 |
|
$messageData['changeByHeading'] = $queue->changeBy->fullname . |
|
|
|
|
81
|
1 |
|
' commented on ' . link_to($this->getIssue()->to(), '#' . $this->getIssue()->id); |
|
|
|
|
82
|
1 |
|
$messageData['changes']['comment'] = [ |
83
|
1 |
|
'noLabel' => true, |
84
|
1 |
|
'date' => $this->getModel()->created_at, |
85
|
|
|
'now' => \Html::format($this->getModel()->comment), |
86
|
|
|
]; |
87
|
1 |
|
|
88
|
|
|
return $messageData; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Returns message data belongs to updating a comment. |
93
|
|
|
* |
94
|
|
|
* @param Queue $queue |
95
|
|
|
* |
96
|
|
|
* @return array |
97
|
1 |
|
*/ |
98
|
|
View Code Duplication |
protected function getMessageDataForUpdateComment(Queue $queue) |
|
|
|
|
99
|
1 |
|
{ |
100
|
1 |
|
$messageData = []; |
101
|
1 |
|
$messageData['changeByHeading'] = $queue->changeBy->fullname . ' updated a comment in ' . link_to($this->getIssue()->to(), |
|
|
|
|
102
|
1 |
|
'#' . $this->getIssue()->id); |
103
|
1 |
|
$messageData['changes']['comment'] = [ |
104
|
1 |
|
'noLabel' => true, |
105
|
1 |
|
'date' => $queue->getDataFromPayload('origin.updated_at'), |
106
|
1 |
|
'was' => \Html::format($queue->getDataFromPayload('origin.comment')), |
107
|
|
|
'now' => \Html::format($queue->getDataFromPayload('dirty.comment')), |
108
|
|
|
]; |
109
|
1 |
|
|
110
|
|
|
return $messageData; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Returns message data belongs to deleting a note. |
115
|
|
|
* |
116
|
|
|
* @param Queue $queue |
117
|
|
|
* |
118
|
|
|
* @return array |
119
|
1 |
|
*/ |
120
|
|
View Code Duplication |
protected function getMessageDataForDeleteComment(Queue $queue) |
|
|
|
|
121
|
1 |
|
{ |
122
|
1 |
|
$messageData = []; |
123
|
1 |
|
$messageData['changeByHeading'] = $queue->changeBy->fullname . |
|
|
|
|
124
|
|
|
' deleted a comment from ' . link_to($this->getIssue()->to(), '#' . $this->getIssue()->id); |
|
|
|
|
125
|
1 |
|
|
126
|
1 |
|
$messageData['changes']['comment'] = [ |
127
|
1 |
|
'noLabel' => true, |
128
|
1 |
|
'date' => $queue->created_at, |
|
|
|
|
129
|
1 |
|
'was' => \Html::format($queue->getDataFromPayload('origin.comment')), |
130
|
|
|
'now' => '', |
131
|
|
|
]; |
132
|
1 |
|
|
133
|
|
|
return $messageData; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Return text to be used for the message heading. |
138
|
|
|
* |
139
|
|
|
* @param Queue $queue |
140
|
|
|
* @param Collection|null $changes |
141
|
|
|
* |
142
|
|
|
* @return string |
143
|
1 |
|
*/ |
144
|
|
|
protected function getMessageHeading(Queue $queue, Collection $changes = null) |
145
|
1 |
|
{ |
146
|
1 |
|
$heading = $queue->changeBy->fullname . |
|
|
|
|
147
|
|
|
' commented on ' . link_to($this->getIssue()->to(), '#' . $this->getIssue()->id); |
|
|
|
|
148
|
1 |
|
|
149
|
|
|
return $heading; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Check that the issue comment is loaded and the comment is belongs to the issue. |
154
|
|
|
* |
155
|
|
|
* @return bool |
156
|
1 |
|
*/ |
157
|
|
|
protected function validateData() |
158
|
1 |
|
{ |
159
|
|
|
return $this->getIssue() && $this->getModel() && $this->getModel()->issue_id === $this->getIssue()->id; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Populate assigned relation in the current issue. |
164
|
|
|
* |
165
|
|
|
* @return void |
166
|
1 |
|
*/ |
167
|
|
View Code Duplication |
protected function populateData() |
|
|
|
|
168
|
|
|
{ |
169
|
1 |
|
// Set the relation of assigned in issue object from the fetch data |
170
|
1 |
|
$this->issue->setRelation('assigned', $this->getUserById($this->issue->assigned_to)); |
171
|
1 |
|
$creator = $this->getUserById($this->issue->created_by); |
172
|
1 |
|
if ($creator) { |
173
|
|
|
$this->issue->setRelation('user', $creator); |
174
|
1 |
|
} |
175
|
1 |
|
$this->loadIssueCreatorToProjectUsers(); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Check if the latest message is about deleting a comment. |
180
|
|
|
* |
181
|
|
|
* @return bool |
182
|
1 |
|
*/ |
183
|
|
|
public function isStatusMessage() |
184
|
1 |
|
{ |
185
|
|
|
return $this->latestMessage->event === Queue::DELETE_COMMENT; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Whether or not the user wants to receive the message. |
190
|
|
|
* |
191
|
|
|
* @param Project\User $user |
192
|
|
|
* @param array $data |
193
|
|
|
* |
194
|
|
|
* @return bool |
195
|
|
|
*/ |
196
|
|
|
protected function wantToReceiveMessage(Project\User $user, array $data) |
197
|
|
|
{ |
198
|
|
|
$status = parent::wantToReceiveMessage($user, $data); |
199
|
|
|
|
200
|
|
|
if (!$status) { |
201
|
|
|
return false; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
// Check if user allowed to receive the message |
205
|
|
|
$tags = $this->getIssue()->tags; |
206
|
|
|
foreach ($tags as $tag) { |
207
|
|
|
if (!$tag->allowMessagesToUser($user->user)) { |
|
|
|
|
208
|
|
|
return false; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
return true; |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
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.