Completed
Push — develop-3.0 ( 4fe777...24fc5d )
by Mohamed
09:15
created

Updater::deleteProject()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 28
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 12
nc 4
nop 0
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\Repository\Project;
13
14
use Illuminate\Support\Collection;
15
use Tinyissue\Model\Project;
16
use Tinyissue\Model\Tag;
17
use Tinyissue\Model\User;
18
use Tinyissue\Repository\RepositoryUpdater;
19
20
class Updater extends RepositoryUpdater
21
{
22
    public function __construct(Project $model)
23
    {
24
        $this->model = $model;
25
    }
26
27
    /**
28
     * removes a user from a project.
29
     *
30
     * @param int $userId
31
     *
32
     * @return mixed
33
     */
34
    public function unassignUser($userId)
35
    {
36
        return $this->model->projectUsers()->where('user_id', '=', $userId)->delete();
37
    }
38
39
    /**
40
     * Assign a user to a project.
41
     *
42
     * @param int $userId
43
     * @param int $roleId
44
     *
45
     * @return Project\User
46
     */
47 View Code Duplication
    public function assignUser($userId, $roleId = 0)
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...
48
    {
49
        if ($userId <= 0) {
50
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by Tinyissue\Repository\Project\Updater::assignUser of type Tinyissue\Model\Project\User.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
51
        }
52
53
        return $this->model->projectUsers()->save(new Project\User([
54
            'user_id' => $userId,
55
            'role_id' => $roleId,
56
        ]));
57
    }
58
59
    public function assignUsers(array $userIds)
60
    {
61
        foreach ($userIds as $userId) {
62
            $this->assignUser($userId);
63
        }
64
    }
65
66
    /**
67
     * Create a new project.
68
     *
69
     * @param array $input
70
     *
71
     * @return $this
72
     */
73
    public function create(array $input = [])
74
    {
75
        $this->model->fill($input)->save();
76
77
        $this->saveKanbanTags(array_get($input, 'columns', []));
78
        $this->assignUsers(array_get($input, 'user', []));
79
80
        /* Assign selected users to the project */
81
//        if (isset($input['user']) && count($input['user']) > 0) {
82
//            foreach ($input['user'] as $id) {
83
//                $this->assignUser($id);
84
//            }
85
//        }
86
87
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Tinyissue\Repository\Project\Updater) is incompatible with the return type of the parent method Tinyissue\Repository\RepositoryUpdater::create of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
88
    }
89
90
    /**
91
     * Update project details.
92
     *
93
     * @param array $attributes
94
     *
95
     * @return bool
96
     */
97
    public function update(array $attributes = [])
98
    {
99
        $this->saveKanbanTags(array_get($attributes, 'columns', []));
100
101
        return $this->model->update($attributes);
102
    }
103
104
    /**
105
     *  Delete a project.
106
     *
107
     * @return void
108
     *
109
     * @throws \Exception
110
     */
111
    public function delete()
112
    {
113
        return $this->transaction('deleteProject');
114
    }
115
116
    protected function deleteProject()
117
    {
118
        // Remove issues
119
        $issues = $this->model->issues()->get();
120
        foreach ($issues as $issue) {
121
            $issue->delete();
122
        }
123
124
        // Remove project notes
125
        $notes = $this->model->notes()->get();
126
        foreach ($notes as $note) {
127
            $note->delete();
128
        }
129
130
        // Remove project users
131
        Project\User::where('project_id', '=', $this->model->id)->delete();
132
133
        // Remove user activities
134
        User\Activity::where('parent_id', '=', $this->model->id)->delete();
135
136
        // Remove kanban tags
137
        \DB::table('projects_kanban_tags')->where('project_id', '=', $this->model->id)->delete();
138
139
        // Remove the project
140
        $this->removeProjectStorage($this->model);
0 ignored issues
show
Compatibility introduced by
$this->model of type object<Illuminate\Database\Eloquent\Model> is not a sub-type of object<Tinyissue\Model\Project>. It seems like you assume a child class of the class Illuminate\Database\Eloquent\Model to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
141
142
        return $this->model->delete();
143
    }
144
145
    /**
146
     * Save the project tags.
147
     *
148
     * @param array $tagIds
149
     *
150
     * @return bool
151
     */
152
    protected function saveKanbanTags(array $tagIds)
153
    {
154
        if (empty($tagIds)) {
155
            return true;
156
        }
157
158
        // Transform the user input tags into tag objects
159
        // Filter out invalid tags entered by the user
160
        $tags = new Collection($tagIds);
161
        $tags = $tags->transform(function ($tagNameOrId) {
162
            return Tag::find($tagNameOrId);
163
        })->filter(function ($tag) {
164
            return $tag instanceof Tag;
165
        });
166
167
        // Delete all existing
168
        $this->model->kanbanTags()->detach();
169
170
        // Save tags
171
        $kanbanTags = $this->model->kanbanTags();
172
        foreach ($tags as $position => $tag) {
173
            $kanbanTags->attach([$tag->id => ['position' => $position]]);
174
        }
175
176
        return true;
177
    }
178
//
179
//    protected function createNote()
180
//    {
181
//        $this->model->notes()->create()
182
//    }
183
}
184