Completed
Push — master ( 50f309...1f3d4e )
by Mohamed
06:23
created

CrudTrait::createProject()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5.2742

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 19
ccs 7
cts 9
cp 0.7778
rs 8.8571
cc 5
eloc 9
nc 4
nop 1
crap 5.2742
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;
13
14
use Illuminate\Database\Query;
15
use Tinyissue\Model\Project;
16
use Tinyissue\Model\User;
17
use Illuminate\Support\Collection;
18
use Tinyissue\Model\Tag;
19
20
/**
21
 * CrudTrait is trait class containing the methods for adding/editing/deleting the Project model.
22
 *
23
 * @author Mohamed Alsharaf <[email protected]>
24
 *
25
 * @property int $id
26
 *
27
 * @method   Query\Builder where($column, $operator = null, $value = null, $boolean = 'and')
28
 * @method   Query\Builder join($table, $one, $operator = null, $two = null, $type = 'inner', $where = false)
29
 * @method   Project       fill(array $attributes)
30
 * @method   RelationTrait projectUsers()
31
 */
32
trait CrudTrait
33
{
34
    /**
35
     * removes a user from a project.
36
     *
37
     * @param int $userId
38
     *
39
     * @return mixed
40
     */
41 1
    public function unassignUser($userId)
42
    {
43 1
        return $this->projectUsers()->where('user_id', '=', $userId)->delete();
0 ignored issues
show
Bug introduced by
It seems like where() 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...
44
    }
45
46
    /**
47
     * Create a new project.
48
     *
49
     * @param array $input
50
     *
51
     * @return $this
52
     */
53 42
    public function createProject(array $input = [])
54
    {
55 42
        if (!empty($input['columns'])) {
56
            $this->saveTags($input['columns']);
57
58
            unset($input['columns']);
59
        }
60
61 42
        $this->fill($input)->save();
62
63
        /* Assign selected users to the project */
64 42
        if (isset($input['user']) && count($input['user']) > 0) {
65 24
            foreach ($input['user'] as $id) {
66 24
                $this->assignUser($id);
67
            }
68
        }
69
70 42
        return $this;
71
    }
72
73
    /**
74
     * Update project details.
75
     *
76
     * @param array $attributes
77
     *
78
     * @return bool
79
     */
80 1
    public function update(array $attributes = [])
81
    {
82 1
        if (!empty($attributes['columns'])) {
83
            $this->saveTags($attributes['columns']);
84
85
            unset($attributes['columns']);
86
        }
87
88 1
        return parent::update($attributes);
89
    }
90
91
    /**
92
     * Save the project tags.
93
     *
94
     * @param string $tagString
95
     *
96
     * @return bool
97
     */
98
    public function saveTags($tagString)
99
    {
100
        // Transform the user input tags into tag objects
101
        // Filter out invalid tags entered by the user
102
        $tags = new Collection(array_map('trim', explode(',', $tagString)));
103
        $tags = $tags->transform(function ($tagNameOrId) {
104
            return Tag::find($tagNameOrId);
105
        })->filter(function ($tag) {
106
            return $tag instanceof Tag;
107
        })->merge((new Tag())->getOpenAndCloseTags());
108
109
        // Delete all existing
110
        $this->kanbanTags()->detach();
0 ignored issues
show
Bug introduced by
It seems like kanbanTags() 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...
111
112
        // Save tags
113
        $kanbanTags = $this->kanbanTags();
0 ignored issues
show
Bug introduced by
It seems like kanbanTags() 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...
114
        $count      = $tags->count();
115 View Code Duplication
        foreach ($tags as $position => $tag) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
116
            $position = $tag->name === Tag::STATUS_OPEN ? -1 : $position;
117
            $position = $tag->name === Tag::STATUS_CLOSED ? $count + 1 : $position;
118
            $kanbanTags->attach([$tag->id => ['position' => $position]]);
119
        }
120
121
        return true;
122
    }
123
124
    /**
125
     * Assign a user to a project.
126
     *
127
     * @param int $userId
128
     * @param int $roleId
129
     *
130
     * @return Project\User
131
     */
132 25
    public function assignUser($userId, $roleId = 0)
133
    {
134 25
        return $this->projectUsers()->save(new Project\User([
0 ignored issues
show
Bug introduced by
It seems like save() 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...
135 25
            'user_id' => $userId,
136 25
            'role_id' => $roleId,
137
        ]));
138
    }
139
140
    /**
141
     *  Delete a project.
142
     *
143
     * @return void
144
     *
145
     * @throws \Exception
146
     */
147 1
    public function delete()
148
    {
149 1
        $id = $this->id;
150 1
        parent::delete();
151
152
        /* Delete all children from the project */
153 1
        Project\Issue::where('project_id', '=', $id)->delete();
154 1
        Project\Issue\Comment::where('project_id', '=', $id)->delete();
155 1
        Project\User::where('project_id', '=', $id)->delete();
156 1
        User\Activity::where('parent_id', '=', $id)->delete();
157 1
    }
158
}
159