CrudTrait   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 84.21%

Importance

Changes 4
Bugs 3 Features 0
Metric Value
wmc 4
c 4
b 3
f 0
lcom 1
cbo 2
dl 0
loc 105
ccs 32
cts 38
cp 0.8421
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
fill() 0 1 ?
queueAdd() 0 1 ?
queueUpdate() 0 1 ?
queueDelete() 0 1 ?
save() 0 1 ?
activity() 0 1 ?
B createComment() 0 36 1
A updateBody() 0 13 1
A deleteComment() 0 20 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 Tinyissue\Model;
16
use Tinyissue\Model\Activity;
17
use Tinyissue\Model\User;
18
use Tinyissue\Model\Project\Issue;
19
use Tinyissue\Model\Project\Issue\Attachment;
20
21
/**
22
 * CrudTrait is trait class containing the methods for adding/editing/deleting the Project\Issue\Comment model.
23
 *
24
 * @author Mohamed Alsharaf <[email protected]>
25
 *
26
 * @property static $this
27
 */
28
trait CrudTrait
29
{
30
    /**
31
     * Create new comment.
32
     *
33
     * @param array $input
34
     *
35
     * @return $this
36
     */
37 10
    public function createComment(array $input)
38
    {
39
        $fill = [
40 10
            'created_by' => $this->user->id,
0 ignored issues
show
Bug introduced by
The property user does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
41 10
            'project_id' => $this->project->id,
0 ignored issues
show
Bug introduced by
The property project does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
42 10
            'issue_id'   => $this->issue->id,
0 ignored issues
show
Bug introduced by
The property issue does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
43 10
            'comment'    => $input['comment'],
44
        ];
45
46 10
        $this->fill($fill);
47
48
        // Add event on successful save
49
        static::saved(function (Issue\Comment $comment) {
50 10
            $this->queueAdd($comment, $comment->user);
51 10
        });
52
53 10
        $this->save();
54
55
        /* Add to user's activity log */
56 10
        $this->activity()->save(new User\Activity([
57 10
            'type_id'   => Activity::TYPE_COMMENT,
58 10
            'parent_id' => $this->project->id,
59 10
            'item_id'   => $this->issue->id,
60 10
            'user_id'   => $this->user->id,
61
        ]));
62
63
        /* Add attachments to issue */
64 10
        Attachment::where('upload_token', '=', $input['upload_token'])
65 10
            ->where('uploaded_by', '=', $this->user->id)
66 10
            ->update(['issue_id' => $this->issue->id, 'comment_id' => $this->id]);
0 ignored issues
show
Bug introduced by
The property id does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
67
68
        /* Update the project */
69 10
        $this->issue->changeUpdatedBy($this->user->id);
70
71 10
        return $this;
72
    }
73
74
    /**
75
     * Update comment body.
76
     *
77
     * @param string $body
78
     * @param User   $user
79
     *
80
     * @return Eloquent\Model
81
     */
82 2
    public function updateBody($body, User $user)
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...
83
    {
84 2
        $this->fill([
85 2
            'comment' => $body,
86
        ]);
87
88
        // Add event on successful save
89
        static::saved(function (Issue\Comment $comment) use ($user) {
90 2
            $this->queueUpdate($comment, $user);
91 2
        });
92
93 2
        return $this->save();
94
    }
95
96
    /**
97
     * Delete a comment and its attachments.
98
     *
99
     * @param User $user
100
     *
101
     * @return Eloquent\Model
102
     *
103
     * @throws \Exception
104
     */
105 2
    public function deleteComment(User $user)
106
    {
107 2
        $this->activity()->delete();
108
109 2
        foreach ($this->attachments as $attachment) {
0 ignored issues
show
Bug introduced by
The property attachments does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
110
            $path = config('filesystems.disks.local.root')
111
                . '/' . config('tinyissue.uploads_dir')
112
                . '/' . $this->project_id
0 ignored issues
show
Bug introduced by
The property project_id does not seem to exist. Did you mean project?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
113
                . '/' . $attachment->upload_token;
114
            $attachment->deleteFile($path, $attachment->filename);
115
            $attachment->delete();
116
        }
117
118
        // Add event on successful delete
119 2
        static::deleted(function (Issue\Comment $comment) use ($user) {
120 2
            $this->queueDelete($comment, $user);
121 2
        });
122
123 2
        return $this->delete();
0 ignored issues
show
Bug introduced by
The method delete() does not exist on Tinyissue\Model\Traits\P...Issue\Comment\CrudTrait. Did you maybe mean deleteComment()?

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...
124
    }
125
126
    abstract public function fill(array $attributes);
127
    abstract public function queueAdd(Issue\Comment $issue, $changeBy);
128
    abstract public function queueUpdate(Issue\Comment $issue, $changeBy);
129
    abstract public function queueDelete(Issue\Comment $issue, $changeBy);
130
    abstract public function save(array $options = []);
131
    abstract public function activity();
132
}
133