Completed
Push — develop ( e210c1...b7d5d2 )
by Mohamed
07:16 queued 59s
created

CrudTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 82.35%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 4
c 4
b 1
f 0
lcom 1
cbo 3
dl 0
loc 89
ccs 28
cts 34
cp 0.8235
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B createComment() 0 34 1
A updateBody() 0 11 1
A deleteComment() 0 18 2
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\Traits\Project\Issue\Comment;
13
14
use Illuminate\Database\Eloquent;
15
use Illuminate\Database\Eloquent\Relations;
16
use Tinyissue\Model;
17
use Tinyissue\Model\Activity;
18
use Tinyissue\Model\User;
19
use Tinyissue\Model\Project;
20
use Tinyissue\Model\Project\Issue;
21
use Tinyissue\Model\Project\Issue\Attachment;
22
use Illuminate\Database\Eloquent\Collection;
23
24
/**
25
 * CrudTrait is trait class containing the methods for adding/editing/deleting the Project\Issue\Comment model.
26
 *
27
 * @author Mohamed Alsharaf <[email protected]>
28
 *
29
 * @property int                $id
30
 * @property int                $issue_id
31
 * @property int                $project_id
32
 * @property string             $comment
33
 * @property int                $created_by
34
 * @property User               $user
35
 * @property Project            $project
36
 * @property Issue              $issue
37
 * @property Collection         $attachments
38
 *
39
 * @method   Eloquent\Model     save()
40
 * @method   Eloquent\Model     fill(array $attributes)
41
 * @method   Relations\HasOne   activity()
42
 * @method   Eloquent\Model     delete()
43
 */
44
trait CrudTrait
45
{
46
    /**
47
     * Create new comment.
48
     *
49
     * @param array $input
50
     *
51
     * @return $this
52
     */
53 6
    public function createComment(array $input)
54
    {
55
        $fill = [
56 6
            'created_by' => $this->user->id,
57 6
            'project_id' => $this->project->id,
58 6
            'issue_id'   => $this->issue->id,
59 6
            'comment'    => $input['comment'],
60
        ];
61
62 6
        $this->fill($fill);
63
64
        // Add event on successful save
65 6
        static::saved([$this, 'queueAdd']);
66
67 6
        $this->save();
68
69
        /* Add to user's activity log */
70 6
        $this->activity()->save(new User\Activity([
71 6
            'type_id'   => Activity::TYPE_COMMENT,
72 6
            'parent_id' => $this->project->id,
73 6
            'item_id'   => $this->issue->id,
74 6
            'user_id'   => $this->user->id,
75
        ]));
76
77
        /* Add attachments to issue */
78 6
        Attachment::where('upload_token', '=', $input['upload_token'])
0 ignored issues
show
Unused Code introduced by
The call to Attachment::where() has too many arguments starting with 'upload_token'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
79 6
            ->where('uploaded_by', '=', $this->user->id)
80 6
            ->update(['issue_id' => $this->issue->id, 'comment_id' => $this->id]);
81
82
        /* Update the project */
83 6
        $this->issue->changeUpdatedBy($this->user->id);
84
85 6
        return $this;
86
    }
87
88
    /**
89
     * Update comment body.
90
     *
91
     * @param string $body
92
     *
93
     * @return Eloquent\Model
94
     */
95 1
    public function updateBody($body)
96
    {
97 1
        $this->fill([
98 1
            'comment' => $body
99
        ]);
100
101
        // Add event on successful save
102 1
        static::saved([$this, 'queueUpdate']);
103
104 1
        return $this->save();
105
    }
106
107
    /**
108
     * Delete a comment and its attachments.
109
     *
110
     * @return Eloquent\Model
111
     *
112
     * @throws \Exception
113
     */
114 1
    public function deleteComment()
115
    {
116 1
        $this->activity()->delete();
117
118 1
        foreach ($this->attachments as $attachment) {
119
            $path = config('filesystems.disks.local.root')
120
                . '/' . config('tinyissue.uploads_dir')
121
                . '/' . $this->project_id
122
                . '/' . $attachment->upload_token;
123
            $attachment->deleteFile($path, $attachment->filename);
124
            $attachment->delete();
125
        }
126
127
        // Add event on successful delete
128 1
        static::deleted([$this, 'queueDelete']);
0 ignored issues
show
Bug introduced by
The method deleted() does not exist on Tinyissue\Model\Traits\P...Issue\Comment\CrudTrait. Did you maybe mean delete()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
129
130 1
        return $this->delete();
131
    }
132
}
133