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 Illuminate\Database\Eloquent; |
15
|
|
|
use Tinyissue\Contracts\Model\UserInterface; |
16
|
|
|
use Tinyissue\Model; |
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 $this |
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
|
|
|
// Model\Project\Note::saved(function (Model\Project\Note $note) { |
42
|
|
|
//// $this->queueAdd($note, $note->createdBy);// TODO |
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; |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Update the note body. |
59
|
|
|
* |
60
|
|
|
* @param string $body |
61
|
|
|
* @param UserInterface $user |
62
|
|
|
* |
63
|
|
|
* @return Eloquent\Model |
64
|
|
|
*/ |
65
|
|
|
public function updateBody($body, UserInterface $user) |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$this->model->body = $body; |
68
|
|
|
|
69
|
|
|
// Add event on successful save |
70
|
|
|
// Model\Project\Note::saved(function (Model\Project\Note $note) use ($user) { |
71
|
|
|
//// $this->queueUpdate($note, $user);// TODO |
72
|
|
|
// }); |
73
|
|
|
|
74
|
|
|
return $this->model->save(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Delete a note. |
79
|
|
|
* |
80
|
|
|
* @return bool|null |
81
|
|
|
* |
82
|
|
|
* @throws \Exception |
83
|
|
|
*/ |
84
|
|
|
public function delete() |
85
|
|
|
{ |
86
|
|
|
$this->model->activity()->delete(); |
87
|
|
|
|
88
|
|
|
// Add event on successful delete |
89
|
|
|
// Model\Project\Note::deleted(function (Model\Project\Note $note) { |
90
|
|
|
// TODO |
91
|
|
|
// $this->queueDelete($note, $this->getLoggedUser()); |
92
|
|
|
// }); |
93
|
|
|
|
94
|
|
|
return $this->model->delete(); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
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.