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\Issue\Comment; |
13
|
|
|
|
14
|
|
|
use Tinyissue\Contracts\Model\UserInterface; |
15
|
|
|
use Tinyissue\Model\Activity; |
16
|
|
|
use Tinyissue\Model\Project\Issue\Attachment; |
17
|
|
|
use Tinyissue\Model\Project\Issue\Comment; |
18
|
|
|
use Tinyissue\Model\User; |
19
|
|
|
use Tinyissue\Repository\RepositoryUpdater; |
20
|
|
|
|
21
|
|
|
class Updater extends RepositoryUpdater |
22
|
|
|
{ |
23
|
|
|
public function __construct(Comment $model) |
24
|
|
|
{ |
25
|
|
|
$this->model = $model; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Create new comment. |
30
|
|
|
* |
31
|
|
|
* @param array $input |
32
|
|
|
* |
33
|
|
|
* @return $this |
34
|
|
|
*/ |
35
|
|
|
public function create(array $input) |
36
|
|
|
{ |
37
|
|
|
$fill = [ |
38
|
|
|
'created_by' => $this->model->user->id, |
39
|
|
|
'project_id' => $this->model->project->id, |
40
|
|
|
'issue_id' => $this->model->issue->id, |
41
|
|
|
'comment' => $input['comment'], |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
$this->model->fill($fill); |
45
|
|
|
|
46
|
|
|
// Add event on successful save |
47
|
|
|
// static::saved(function (Comment $comment) { |
48
|
|
|
//// $this->queueAdd($comment, $comment->user);// TODO |
49
|
|
|
// }); |
50
|
|
|
|
51
|
|
|
$this->save(); |
52
|
|
|
|
53
|
|
|
/* Add to user's activity log */ |
54
|
|
|
$this->saveToActivity([ |
55
|
|
|
'type_id' => Activity::TYPE_COMMENT, |
56
|
|
|
'parent_id' => $this->model->project->id, |
57
|
|
|
'item_id' => $this->model->issue->id, |
58
|
|
|
'user_id' => $this->model->user->id, |
59
|
|
|
]); |
60
|
|
|
|
61
|
|
|
/* Add attachments to issue */ |
62
|
|
|
Attachment::instance()->updateCommentToken( |
|
|
|
|
63
|
|
|
$input['upload_token'], |
64
|
|
|
$this->model->user->id, |
65
|
|
|
$this->model->issue->id, |
66
|
|
|
$this->model->id |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
/* Update the project */ |
70
|
|
|
$this->model->issue->changeUpdatedBy($this->model->user->id); |
71
|
|
|
|
72
|
|
|
return $this; |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Update comment body. |
77
|
|
|
* |
78
|
|
|
* @param string $body |
79
|
|
|
* @param UserInterface $user |
80
|
|
|
* |
81
|
|
|
* @return Eloquent\Model |
82
|
|
|
*/ |
83
|
|
|
public function updateBody($body, UserInterface $user) |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
$this->model->fill([ |
86
|
|
|
'comment' => $body, |
87
|
|
|
]); |
88
|
|
|
|
89
|
|
|
// Add event on successful save |
90
|
|
|
// static::saved(function (Issue\Comment $comment) use ($user) { |
91
|
|
|
// // @TODO |
92
|
|
|
//// $this->queueUpdate($comment, $user); |
93
|
|
|
// }); |
94
|
|
|
|
95
|
|
|
return $this->save(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function delete() |
99
|
|
|
{ |
100
|
|
|
return $this->transaction('deleteComment'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Delete a comment and its attachments. |
105
|
|
|
* |
106
|
|
|
* @param UserInterface $user |
|
|
|
|
107
|
|
|
* |
108
|
|
|
* @return Eloquent\Model |
109
|
|
|
* |
110
|
|
|
* @throws \Exception |
111
|
|
|
*/ |
112
|
|
|
protected function deleteComment() |
113
|
|
|
{ |
114
|
|
|
$this->model->activity()->delete(); |
115
|
|
|
|
116
|
|
|
foreach ($this->model->attachments as $attachment) { |
117
|
|
|
// $path = $this->getUploadStorage($this->model->project_id, $attachment->upload_token); |
118
|
|
|
// $attachment->deleteFile($path, $attachment->filename); |
119
|
|
|
$attachment->updater()->delete(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
// Add event on successful delete |
123
|
|
|
// static::deleted(function (Issue\Comment $comment) { |
124
|
|
|
// @TODO |
125
|
|
|
// $this->queueDelete($comment, $this->getLoggedUser()); |
126
|
|
|
// }); |
127
|
|
|
|
128
|
|
|
return $this->model->delete(); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: