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\Note; |
13
|
|
|
|
14
|
|
|
use Tinyissue\Model\Project; |
15
|
|
|
use Tinyissue\Model\Project\Issue; |
16
|
|
|
use Tinyissue\Model\Message\Queue; |
17
|
|
|
use Illuminate\Support\Collection; |
18
|
|
|
use Tinyissue\Model\User; |
19
|
|
|
use Tinyissue\Services\SendMessagesAbstract; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* SendMessages is a class to manage & process of sending messages about note changes. |
23
|
|
|
* |
24
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class SendMessages extends SendMessagesAbstract |
27
|
|
|
{ |
28
|
|
|
protected $template = 'update_note'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Returns the subject text. |
32
|
|
|
* |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
1 |
|
protected function getSubject() |
36
|
|
|
{ |
37
|
1 |
|
return $this->getProject()->name; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Note: changes is belongs to the project (no issue here). |
42
|
|
|
* |
43
|
|
|
* @return bool |
44
|
|
|
*/ |
45
|
1 |
|
protected function getIssue() |
46
|
|
|
{ |
47
|
1 |
|
return false; |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Returns an instance of Project. |
52
|
|
|
* |
53
|
|
|
* @return Project |
54
|
|
|
*/ |
55
|
1 |
View Code Duplication |
protected function getProject() |
|
|
|
|
56
|
|
|
{ |
57
|
1 |
|
if ($this->getModel()) { |
58
|
1 |
|
return $this->getModel()->project; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// Possible deleted note |
62
|
|
|
if (null === $this->project) { |
63
|
|
|
$projectId = $this->latestMessage->getDataFromPayload('origin.project_id'); |
64
|
|
|
$this->project = (new Project())->find($projectId); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $this->project; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Returns the project id. |
72
|
|
|
* |
73
|
|
|
* @return int |
74
|
|
|
*/ |
75
|
1 |
|
protected function getProjectId() |
76
|
|
|
{ |
77
|
1 |
|
return $this->getProject()->id; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Returns message data belongs to adding a note. |
82
|
|
|
* |
83
|
|
|
* @param Queue $queue |
84
|
|
|
* |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
1 |
|
protected function getMessageDataForAddNote(Queue $queue) |
88
|
|
|
{ |
89
|
1 |
|
$messageData = []; |
90
|
1 |
|
$messageData['changeByHeading'] = $queue->changeBy->fullname . ' added a note'; |
|
|
|
|
91
|
1 |
|
$messageData['changes']['note'] = [ |
92
|
1 |
|
'noLabel' => true, |
93
|
1 |
|
'date' => $this->getModel()->created_at, |
94
|
1 |
|
'now' => \Html::format($this->getModel()->body), |
95
|
|
|
]; |
96
|
|
|
|
97
|
1 |
|
return $messageData; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Returns message data belongs to updating a note. |
102
|
|
|
* |
103
|
|
|
* @param Queue $queue |
104
|
|
|
* |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
1 |
|
protected function getMessageDataForUpdateNote(Queue $queue) |
108
|
|
|
{ |
109
|
1 |
|
$messageData = []; |
110
|
1 |
|
$messageData['changeByHeading'] = $queue->changeBy->fullname . ' updated a note in ' . link_to($this->getProject()->to(), $this->getProject()->name); |
|
|
|
|
111
|
1 |
|
$messageData['changes']['note'] = [ |
112
|
1 |
|
'noLabel' => true, |
113
|
1 |
|
'date' => $queue->getDataFromPayload('origin.updated_at'), |
114
|
1 |
|
'was' => \Html::format($queue->getDataFromPayload('origin.body')), |
115
|
1 |
|
'now' => \Html::format($queue->getDataFromPayload('dirty.body')), |
116
|
|
|
]; |
117
|
|
|
|
118
|
1 |
|
return $messageData; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Returns message data belongs to deleting a note. |
123
|
|
|
* |
124
|
|
|
* @param Queue $queue |
125
|
|
|
* |
126
|
|
|
* @return array |
127
|
|
|
*/ |
128
|
|
View Code Duplication |
protected function getMessageDataForDeleteNote(Queue $queue) |
|
|
|
|
129
|
|
|
{ |
130
|
|
|
$messageData = []; |
131
|
|
|
$messageData['changeByHeading'] = $queue->changeBy->fullname . ' deleted a note in ' . link_to($this->getProject()->to(), $this->getProject()->name); |
|
|
|
|
132
|
|
|
$messageData['changes']['note'] = [ |
133
|
|
|
'noLabel' => true, |
134
|
|
|
'date' => $queue->created_at, |
|
|
|
|
135
|
|
|
'was' => \Html::format($queue->getDataFromPayload('origin.body')), |
136
|
|
|
'now' => '', |
137
|
|
|
]; |
138
|
|
|
|
139
|
|
|
return $messageData; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Return text to be used for the message heading. |
144
|
|
|
* |
145
|
|
|
* @param Queue $queue |
146
|
|
|
* @param Collection|null $changes |
147
|
|
|
* |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
1 |
|
protected function getMessageHeading(Queue $queue, Collection $changes = null) |
151
|
|
|
{ |
152
|
1 |
|
$heading = parent::getMessageHeading($queue, $changes); |
153
|
1 |
|
$heading .= ' updated a note in ' . link_to($this->getProject()->to(), '#' . $this->getProjectId()); |
154
|
|
|
|
155
|
1 |
|
return $heading; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Check that the project is loaded and the note is belongs to the project. |
160
|
|
|
* |
161
|
|
|
* @return bool |
162
|
|
|
*/ |
163
|
1 |
|
protected function validateData() |
164
|
|
|
{ |
165
|
1 |
|
return $this->getProject() && $this->getModel() && $this->getModel()->project_id === $this->getProject()->id; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Check if the latest message is about deleting a note. |
170
|
|
|
* |
171
|
|
|
* @return bool |
172
|
|
|
*/ |
173
|
1 |
|
public function isStatusMessage() |
174
|
|
|
{ |
175
|
1 |
|
return $this->latestMessage->event === Queue::DELETE_NOTE; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.