|
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\Project\Note; |
|
13
|
|
|
|
|
14
|
|
|
use Tinyissue\Contracts\Model\UserInterface; |
|
15
|
|
|
use Tinyissue\Model; |
|
16
|
|
|
use Tinyissue\Model\Message\Queue; |
|
17
|
|
|
use Tinyissue\Model\Project; |
|
18
|
|
|
use Tinyissue\Repository\RepositoryUpdater; |
|
19
|
|
|
|
|
20
|
|
|
class Updater extends RepositoryUpdater |
|
21
|
|
|
{ |
|
22
|
|
|
public function __construct(Project\Note $model) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->model = $model; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Create a new note. |
|
29
|
|
|
* |
|
30
|
|
|
* @param array $input |
|
31
|
|
|
* |
|
32
|
|
|
* @return Project\Note |
|
33
|
|
|
*/ |
|
34
|
|
|
public function create(array $input) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->model->body = $input['note_body']; |
|
37
|
|
|
$this->model->project_id = $this->model->project->id; |
|
38
|
|
|
$this->model->created_by = $this->model->createdBy->id; |
|
39
|
|
|
|
|
40
|
|
|
// Add event on successful save |
|
41
|
|
|
Project\Note::saved(function (Project\Note $note) { |
|
42
|
|
|
$this->queueAdd($note, $this->user); |
|
43
|
|
|
}); |
|
44
|
|
|
|
|
45
|
|
|
$this->model->save(); |
|
46
|
|
|
|
|
47
|
|
|
// Add to user's activity log |
|
48
|
|
|
$this->saveToActivity([ |
|
49
|
|
|
'type_id' => Model\Activity::TYPE_NOTE, |
|
50
|
|
|
'parent_id' => $this->model->project->id, |
|
51
|
|
|
'user_id' => $this->model->createdBy->id, |
|
52
|
|
|
]); |
|
53
|
|
|
|
|
54
|
|
|
return $this->model; |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Update the note body. |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $body |
|
61
|
|
|
* |
|
62
|
|
|
* @return Project\Note |
|
63
|
|
|
*/ |
|
64
|
|
|
public function updateBody($body) |
|
65
|
|
|
{ |
|
66
|
|
|
$this->model->body = $body; |
|
67
|
|
|
|
|
68
|
|
|
// Add event on successful save |
|
69
|
|
|
Project\Note::saved(function (Project\Note $note) { |
|
70
|
|
|
$this->queueUpdate($note, $this->user); |
|
71
|
|
|
}); |
|
72
|
|
|
|
|
73
|
|
|
return $this->model->save(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Delete a note. |
|
78
|
|
|
* |
|
79
|
|
|
* @return bool|null |
|
80
|
|
|
* |
|
81
|
|
|
* @throws \Exception |
|
82
|
|
|
*/ |
|
83
|
|
|
public function delete() |
|
84
|
|
|
{ |
|
85
|
|
|
$this->model->activity()->delete(); |
|
86
|
|
|
|
|
87
|
|
|
// Add event on successful delete |
|
88
|
|
|
Project\Note::deleted(function (Project\Note $note) { |
|
89
|
|
|
$this->queueDelete($note, $this->user); |
|
90
|
|
|
}); |
|
91
|
|
|
|
|
92
|
|
|
return $this->model->delete(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Insert add note to message queue. |
|
97
|
|
|
* |
|
98
|
|
|
* @param Project\Note $note |
|
99
|
|
|
* @param UserInterface $changeBy |
|
100
|
|
|
* |
|
101
|
|
|
* @return void |
|
102
|
|
|
*/ |
|
103
|
|
|
public function queueAdd(Project\Note $note, UserInterface $changeBy) |
|
104
|
|
|
{ |
|
105
|
|
|
return (new Queue())->queue(Queue::ADD_NOTE, $note, $changeBy); |
|
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Insert update note to message queue. |
|
110
|
|
|
* |
|
111
|
|
|
* @param Project\Note $note |
|
112
|
|
|
* @param UserInterface $changeBy |
|
113
|
|
|
* |
|
114
|
|
|
* @return void |
|
115
|
|
|
*/ |
|
116
|
|
|
public function queueUpdate(Project\Note $note, UserInterface $changeBy) |
|
117
|
|
|
{ |
|
118
|
|
|
// Skip message if nothing changed in note |
|
119
|
|
|
if (!$note->isDirty()) { |
|
120
|
|
|
return; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return (new Queue())->queue(Queue::UPDATE_NOTE, $note, $changeBy); |
|
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Insert delete note to message queue. |
|
128
|
|
|
* |
|
129
|
|
|
* @param Project\Note $note |
|
130
|
|
|
* @param UserInterface $changeBy |
|
131
|
|
|
* |
|
132
|
|
|
* @return void |
|
133
|
|
|
*/ |
|
134
|
|
|
public function queueDelete(Project\Note $note, UserInterface $changeBy) |
|
135
|
|
|
{ |
|
136
|
|
|
return (new Queue())->queueDelete(Queue::DELETE_NOTE, $note, $changeBy); |
|
|
|
|
|
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
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_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.