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

CrudTrait   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 90.77%

Importance

Changes 9
Bugs 3 Features 2
Metric Value
wmc 13
c 9
b 3
f 2
lcom 1
cbo 5
dl 0
loc 156
ccs 59
cts 65
cp 0.9077
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A changeUpdatedBy() 0 7 1
A reassign() 0 18 3
B updateIssue() 0 29 2
B createIssue() 0 41 4
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 6
    public function changeUpdatedBy($userId)
59
    {
60 6
        $this->updated_by = $userId;
61 6
        $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 6
        return $this->save();
64
    }
65
66
    /**
67
     * Reassign the issue to a new user.
68
     *
69
     * @param int|User $assignTo
70
     * @param int|User $user
71
     *
72
     * @return Eloquent\Model
73
     */
74 2
    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 2
        static::saved([$this, 'queueAssign']);
82
83 2
        $this->save();
84
85 2
        return $this->activities()->save(new User\Activity([
86 2
            'type_id'   => Activity::TYPE_REASSIGN_ISSUE,
87 2
            'parent_id' => $this->project->id,
88 2
            'user_id'   => $userId,
89 2
            'action_id' => $this->assigned_to,
90
        ]));
91
    }
92
93
    /**
94
     * Update the given issue.
95
     *
96
     * @param array $input
97
     *
98
     * @return Eloquent\Model
99
     */
100 1
    public function updateIssue(array $input)
101
    {
102
        $fill = [
103 1
            'title'       => $input['title'],
104 1
            'body'        => $input['body'],
105 1
            'assigned_to' => $input['assigned_to'],
106 1
            'time_quote'  => $input['time_quote'],
107 1
            'updated_by'  => $this->updatedBy->id,
108
        ];
109
110
        /* Add to activity log for assignment if changed */
111 1
        if ($input['assigned_to'] != $this->assigned_to) {
112
            $this->activities()->save(new User\Activity([
113
                'type_id'   => Activity::TYPE_REASSIGN_ISSUE,
114
                'parent_id' => $this->project->id,
115
                'user_id'   => $this->updatedBy->id,
116
                'action_id' => $this->assigned_to,
117
            ]));
118
        }
119
120 1
        $this->fill($fill);
121
122 1
        $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...
123
124
        // Add event on successful save
125 1
        static::saved([$this, 'queueUpdate']);
126
127 1
        return $this->save();
128
    }
129
130
    /**
131
     * Create a new issue.
132
     *
133
     * @param array $input
134
     *
135
     * @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...
136
     */
137 28
    public function createIssue(array $input)
138
    {
139
        $fill = [
140 28
            'created_by' => $this->user->id,
141 28
            'project_id' => $this->project->id,
142 28
            'title'      => $input['title'],
143 28
            'body'       => $input['body'],
144
        ];
145
146 28
        if ($this->user->permission('issue-modify')) {
147 27
            $fill['assigned_to'] = $input['assigned_to'];
148 27
            $fill['time_quote']  = $input['time_quote'];
149
        }
150
151 28
        $this->fill($fill)->save();
152
153
        // Add issue to messages queue
154 28
        $this->queueAdd($this);
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...
155
156
        /* Add to user's activity log */
157 17
        $this->activities()->save(new User\Activity([
158 17
            'type_id'   => Activity::TYPE_CREATE_ISSUE,
159 17
            'parent_id' => $this->project->id,
160 17
            'user_id'   => $this->user->id,
161
        ]));
162
163
        /* Add attachments to issue */
164 17
        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...
165 17
            ->where('uploaded_by', '=', $this->user->id)
166 17
            ->update(['issue_id' => $this->id]);
167
168
        // Add default tag to newly created issue
169 17
        $defaultTag = app('tinyissue.settings')->getFirstStatusTagId();
170 17
        if ($defaultTag > 0 && empty($input['tag_status'])) {
171
            $input['tag_status'] = $defaultTag;
172
        }
173
174 17
        $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...
175
176 17
        return $this;
177
    }
178
179
    /**
180
     * Move the issue (comments & activities) to another project.
181
     *
182
     * @param int $projectId
183
     *
184
     * @return $this
185
     */
186 1
    public function changeProject($projectId)
187
    {
188 1
        $this->project_id = $projectId;
189 1
        $this->save();
190 1
        $comments = $this->comments()->get();
191 1
        foreach ($comments as $comment) {
192 1
            $comment->project_id = $projectId;
193 1
            $comment->save();
194
        }
195
196 1
        $activities = $this->activities()->get();
197 1
        foreach ($activities as $activity) {
198 1
            $activity->parent_id = $projectId;
199 1
            $activity->save();
200
        }
201
202 1
        return $this;
203
    }
204
}
205