Completed
Push — develop-3.0 ( 5ab583...f20237 )
by Mohamed
06:33
created

Updater::create()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 39
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 22
nc 1
nop 1
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\Model\Activity;
15
use Tinyissue\Model\Message\Queue;
16
use Tinyissue\Model\Project\Issue;
17
use Tinyissue\Model\Project\Issue\Attachment;
18
use Tinyissue\Model\Project\Issue\Comment;
19
use Tinyissue\Model\User;
20
use Tinyissue\Repository\RepositoryUpdater;
21
22
class Updater extends RepositoryUpdater
23
{
24
    /**
25
     * @var Comment
26
     */
27
    protected $model;
28
29
    public function __construct(Comment $model)
30
    {
31
        $this->model = $model;
32
    }
33
34
    /**
35
     * Create new comment.
36
     *
37
     * @param array $input
38
     *
39
     * @return Comment
40
     */
41
    public function create(array $input)
42
    {
43
        $fill = [
44
            'created_by' => $this->model->user->id,
45
            'project_id' => $this->model->project->id,
46
            'issue_id'   => $this->model->issue->id,
47
            'comment'    => $input['comment'],
48
        ];
49
50
        $this->model->fill($fill);
51
52
        // Add event on successful save
53
        Comment::saved(function (Comment $comment) {
54
            $this->queueAdd($comment, $this->user);
55
        });
56
57
        $this->save();
58
59
        /* Add to user's activity log */
60
        $this->saveToActivity([
61
            'type_id'   => Activity::TYPE_COMMENT,
62
            'parent_id' => $this->model->project->id,
63
            'item_id'   => $this->model->issue->id,
64
            'user_id'   => $this->model->user->id,
65
        ]);
66
67
        /* Add attachments to issue */
68
        Attachment::instance()->updater()->updateCommentToken(
69
            array_get($input, 'upload_token'),
70
            $this->model->user->id,
71
            $this->model->issue->id,
72
            $this->model->id
73
        );
74
75
        /* Update the project */
76
        $this->model->issue->updater($this->model->user)->changeUpdatedBy();
77
78
        return $this->model;
79
    }
80
81
    /**
82
     * Update comment body.
83
     *
84
     * @param string $body
85
     *
86
     * @return Comment
87
     */
88
    public function updateBody($body)
89
    {
90
        $this->model->fill([
91
            'comment' => $body,
92
        ]);
93
94
        // Add event on successful save
95
        Comment::saved(function (Comment $comment) {
96
            $this->queueUpdate($comment, $this->user);
97
        });
98
99
        return $this->save();
100
    }
101
102
    public function delete()
103
    {
104
        return $this->transaction('deleteComment');
105
    }
106
107
    /**
108
     * Delete a comment and its attachments.
109
     *
110
     * @return Comment
111
     *
112
     * @throws \Exception
113
     */
114
    protected function deleteComment()
115
    {
116
        $this->model->activity()->delete();
117
118
        foreach ($this->model->attachments as $attachment) {
119
            $attachment->updater()->delete();
120
        }
121
122
        // Add event on successful delete
123
        Comment::deleted(function (Comment $comment) {
124
            $this->queueDelete($comment, $this->user);
125
        });
126
127
        return $this->model->delete();
128
    }
129
130
    /**
131
     * Insert add comment to message queue.
132
     *
133
     * @param Comment  $comment
134
     * @param User $changeBy
135
     *
136
     * @return void
137
     */
138
    public function queueAdd(Comment $comment, User $changeBy)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
139
    {
140
        // Skip message if issue closed
141
        if (!$comment->issue->isOpen()) {
142
            return;
143
        }
144
145
        return (new Queue())->updater($changeBy)->queue(Queue::ADD_COMMENT, $comment, $changeBy);
146
    }
147
148
    /**
149
     * Insert update comment to message queue.
150
     *
151
     * @param Comment  $comment
152
     * @param User $changeBy
153
     *
154
     * @return void
155
     */
156
    public function queueUpdate(Comment $comment, User $changeBy)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
157
    {
158
        // Skip message if issue closed or nothing changed in comment
159
        if (!$comment->issue->isOpen() || !$comment->isDirty()) {
160
            return;
161
        }
162
163
        return (new Queue())->updater($changeBy)->queue(Queue::UPDATE_COMMENT, $comment, $changeBy);
164
    }
165
166
    /**
167
     * Insert delete comment to message queue.
168
     *
169
     * @param Comment  $comment
170
     * @param User $changeBy
171
     *
172
     * @return void
173
     */
174
    public function queueDelete(Comment $comment, User $changeBy)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
175
    {
176
        // Skip message if issue closed
177
        if ($comment->issue instanceof Issue && !$comment->issue->isOpen()) {
178
            return;
179
        }
180
181
        return (new Queue())->updater($changeBy)->queueDelete(Queue::DELETE_COMMENT, $comment, $changeBy);
182
    }
183
}
184