Completed
Push — develop ( fea319...7a0090 )
by Mohamed
07:15
created

CrudTrait   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 73.52%

Importance

Changes 11
Bugs 5 Features 2
Metric Value
wmc 13
c 11
b 5
f 2
lcom 1
cbo 6
dl 0
loc 160
ccs 50
cts 68
cp 0.7352
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A changeUpdatedBy() 0 7 1
B createIssue() 0 41 4
B updateIssue() 0 31 2
A reassign() 0 20 3
A changeProject() 0 18 3
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;
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\Project;
19
use Tinyissue\Model\Project\Issue\Attachment;
20
use Tinyissue\Model\User;
21
22
/**
23
 * CrudTrait is trait class containing the methods for adding/editing/deleting the Project\Issue model.
24
 *
25
 * @author Mohamed Alsharaf <[email protected]>
26
 *
27
 * @property int                        $id
28
 * @property int                        $created_by
29
 * @property int                        $project_id
30
 * @property string                     $title
31
 * @property string                     $body
32
 * @property int                        $assigned_to
33
 * @property int                        $time_quote
34
 * @property int                        $closed_by
35
 * @property int                        $closed_at
36
 * @property int                        status
37
 * @property int                        $updated_at
38
 * @property int                        $updated_by
39
 * @property Project                    $project
40
 * @property User                       $user
41
 * @property User                       $updatedBy
42
 *
43
 * @method   Eloquent\Model             save()
44
 * @method   Eloquent\Model             fill(array $attributes)
45
 * @method   Relations\BelongsToMany    tags()
46
 * @method   Relations\HasMany          activities()
47
 * @method   Relations\HasMany          comments()
48
 */
49
trait CrudTrait
50
{
51
    /**
52
     * Set the issue is updated by a user.
53
     *
54
     * @param int $userId
55
     *
56
     * @return Eloquent\Model
57
     */
58 9
    public function changeUpdatedBy($userId)
59
    {
60 9
        $this->updated_by = $userId;
61 9
        $this->touch();
0 ignored issues
show
Bug introduced by
It seems like touch() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
62
63 9
        return $this->save();
64
    }
65
66
    /**
67
     * Reassign the issue to a new user.
68
     *
69
     * @param int                                             $assignTo
70
     * @param \Illuminate\Contracts\Auth\Authenticatable|null $user
71
     *
72
     * @return Eloquent\Model
73
     */
74 4
    public function reassign($assignTo, $user)
75
    {
76 2
        $assignToId        = !$assignTo instanceof User ? $assignTo : $assignTo->id;
77 2
        $userId            = !$user instanceof User ? $user : $user->id;
78 2
        $this->assigned_to = $assignToId;
79
80
        // Add event on successful save
81
        static::saved(function (Project\Issue $issue) use ($userId) {
82 4
            $this->queueAssign($issue, $userId);
0 ignored issues
show
Bug introduced by
It seems like queueAssign() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
83 2
        });
84
85 2
        $this->save();
86
87
        return $this->activities()->save(new User\Activity([
88
            'type_id'   => Activity::TYPE_REASSIGN_ISSUE,
89
            'parent_id' => $this->project->id,
90
            'user_id'   => $userId,
91
            'action_id' => $this->assigned_to,
92
        ]));
93
    }
94
95
    /**
96
     * Update the given issue.
97
     *
98
     * @param array $input
99
     *
100
     * @return Eloquent\Model
101
     */
102 3
    public function updateIssue(array $input)
103
    {
104
        $fill = [
105 3
            'title'       => $input['title'],
106 3
            'body'        => $input['body'],
107 3
            'assigned_to' => $input['assigned_to'],
108 3
            'time_quote'  => $input['time_quote'],
109 3
            'updated_by'  => $this->updatedBy->id,
110
        ];
111
112
        /* Add to activity log for assignment if changed */
113 3
        if ($input['assigned_to'] != $this->assigned_to) {
114 1
            $this->activities()->save(new User\Activity([
115 1
                'type_id'   => Activity::TYPE_REASSIGN_ISSUE,
116 1
                'parent_id' => $this->project->id,
117 1
                'user_id'   => $this->updatedBy->id,
118 1
                'action_id' => $this->assigned_to,
119
            ]));
120
        }
121
122 3
        $this->fill($fill);
123
124 3
        $this->syncTags($input, $this->tags()->with('parent')->get());
0 ignored issues
show
Bug introduced by
It seems like syncTags() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
125
126
        // Add event on successful save
127 3
        static::saved(function (Project\Issue $issue) {
128 3
            $this->queueUpdate($issue, $issue->updatedBy);
0 ignored issues
show
Bug introduced by
It seems like queueUpdate() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
129 3
        });
130
131 3
        return $this->save();
132
    }
133
134
    /**
135
     * Create a new issue.
136
     *
137
     * @param array $input
138
     *
139
     * @return CrudTrait
0 ignored issues
show
Comprehensibility Bug introduced by
The return type CrudTrait is a trait, and thus cannot be used for type-hinting in PHP. Maybe consider adding an interface and use that for type-hinting?

In PHP traits cannot be used for type-hinting as they do not define a well-defined structure. This is because any class that uses a trait can rename that trait’s methods.

If you would like to return an object that has a guaranteed set of methods, you could create a companion interface that lists these methods explicitly.

Loading history...
140
     */
141 31
    public function createIssue(array $input)
142
    {
143
        $fill = [
144 31
            'created_by' => $this->user->id,
145 31
            'project_id' => $this->project->id,
146 31
            'title'      => $input['title'],
147 31
            'body'       => $input['body'],
148
        ];
149
150 31
        if ($this->user->permission('issue-modify')) {
151 30
            $fill['assigned_to'] = $input['assigned_to'];
152 30
            $fill['time_quote']  = $input['time_quote'];
153
        }
154
155 31
        $this->fill($fill)->save();
156
157
        // Add issue to messages queue
158 29
        $this->queueAdd($this, $this->user);
0 ignored issues
show
Bug introduced by
It seems like queueAdd() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
159
160
        /* Add to user's activity log */
161 29
        $this->activities()->save(new User\Activity([
162 29
            'type_id'   => Activity::TYPE_CREATE_ISSUE,
163 29
            'parent_id' => $this->project->id,
164 29
            'user_id'   => $this->user->id,
165
        ]));
166
167
        /* Add attachments to issue */
168 29
        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...
169 29
            ->where('uploaded_by', '=', $this->user->id)
170 29
            ->update(['issue_id' => $this->id]);
171
172
        // Add default tag to newly created issue
173 29
        $defaultTag = app('tinyissue.settings')->getFirstStatusTagId();
174 29
        if ($defaultTag > 0 && empty($input['tag_status'])) {
175
            $input['tag_status'] = $defaultTag;
176
        }
177
178 29
        $this->syncTags($input);
0 ignored issues
show
Bug introduced by
It seems like syncTags() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
179
180 29
        return $this;
181
    }
182
183
    /**
184
     * Move the issue (comments & activities) to another project.
185
     *
186
     * @param int $projectId
187
     *
188
     * @return $this
189
     */
190
    public function changeProject($projectId)
191
    {
192
        $this->project_id = $projectId;
193
        $this->save();
194
        $comments = $this->comments()->get();
195
        foreach ($comments as $comment) {
196
            $comment->project_id = $projectId;
197
            $comment->save();
198
        }
199
200
        $activities = $this->activities()->get();
201
        foreach ($activities as $activity) {
202
            $activity->parent_id = $projectId;
203
            $activity->save();
204
        }
205
206
        return $this;
207
    }
208
}
209