Completed
Branch develop-3.0 (4fe777)
by Mohamed
11:06
created

UpdaterRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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\Contracts\Repository\Project\UpdaterRepository as ProjectUpdater;
16
use Tinyissue\Model\Project;
17
use Tinyissue\Model\Tag;
18
use Tinyissue\Model\User;
19
use Tinyissue\Repository\RepositoryUpdater;
20
21
class UpdaterRepository extends RepositoryUpdater implements ProjectUpdater
22
{
23
    public function __construct(Project $model)
24
    {
25
        $this->model = $model;
26
    }
27
28
    /**
29
     * removes a user from a project.
30
     *
31
     * @param int $userId
32
     *
33
     * @return mixed
34
     */
35
    public function unassignUser($userId)
36
    {
37
        return $this->model->projectUsers()->where('user_id', '=', $userId)->delete();
38
    }
39
40
    /**
41
     * Assign a user to a project.
42
     *
43
     * @param int $userId
44
     * @param int $roleId
45
     *
46
     * @return Project\User
47
     */
48 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...
49
    {
50
        if ($userId <= 0) {
51
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the interface Tinyissue\Contracts\Repo...rRepository::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...
52
        }
53
54
        return $this->model->projectUsers()->save(new Project\User([
55
            'user_id' => $userId,
56
            'role_id' => $roleId,
57
        ]));
58
    }
59
60
    public function assignUsers(array $userIds)
61
    {
62
        foreach ($userIds as $userId) {
63
            $this->assignUser($userId);
64
        }
65
    }
66
67
    /**
68
     * Create a new project.
69
     *
70
     * @param array $input
71
     *
72
     * @return $this
73
     */
74
    public function create(array $input = [])
75
    {
76
        $this->model->fill($input)->save();
77
78
        $this->saveKanbanTags(array_get($input, 'columns', []));
79
        $this->assignUsers(array_get($input, 'user', []));
80
81
        /* Assign selected users to the project */
82
//        if (isset($input['user']) && count($input['user']) > 0) {
83
//            foreach ($input['user'] as $id) {
84
//                $this->assignUser($id);
85
//            }
86
//        }
87
88
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Tinyissue\Repository\Project\UpdaterRepository) 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...
89
    }
90
91
    /**
92
     * Update project details.
93
     *
94
     * @param array $attributes
95
     *
96
     * @return bool
97
     */
98
    public function update(array $attributes = [])
99
    {
100
        $this->saveKanbanTags(array_get($attributes, 'columns', []));
101
102
        return $this->model->update($attributes);
103
    }
104
105
    /**
106
     *  Delete a project.
107
     *
108
     * @return void
109
     *
110
     * @throws \Exception
111
     */
112
    public function delete()
113
    {
114
        return $this->transaction('deleteProject');
115
    }
116
117
    protected function deleteProject()
118
    {
119
        // Remove issues
120
        $issues = $this->model->issues()->get();
121
        foreach ($issues as $issue) {
122
            $issue->delete();
123
        }
124
125
        // Remove project notes
126
        $notes = $this->model->notes()->get();
127
        foreach ($notes as $note) {
128
            $note->delete();
129
        }
130
131
        // Remove project users
132
        Project\User::where('project_id', '=', $this->model->id)->delete();
133
134
        // Remove user activities
135
        User\Activity::where('parent_id', '=', $this->model->id)->delete();
136
137
        // Remove kanban tags
138
        \DB::table('projects_kanban_tags')->where('project_id', '=', $this->model->id)->delete();
139
140
        // Remove the project
141
        // @todo abstarct the file sytem functionality to service class
142
        $dir = config('filesystems.disks.local.root') . '/' . config('tinyissue.uploads_dir') . '/' . $this->model->id;
143
        if (is_dir($dir)) {
144
            rmdir($dir);
145
        }
146
147
        return $this->model->delete();
148
    }
149
150
    /**
151
     * Save the project tags.
152
     *
153
     * @param array $tagIds
154
     *
155
     * @return bool
156
     */
157
    protected function saveKanbanTags(array $tagIds)
158
    {
159
        if (empty($tagIds)) {
160
            return true;
161
        }
162
163
        // Transform the user input tags into tag objects
164
        // Filter out invalid tags entered by the user
165
        $tags = new Collection($tagIds);
166
        $tags = $tags->transform(function ($tagNameOrId) {
167
            return Tag::find($tagNameOrId);
168
        })->filter(function ($tag) {
169
            return $tag instanceof Tag;
170
        });
171
172
        // Delete all existing
173
        $this->model->kanbanTags()->detach();
174
175
        // Save tags
176
        $kanbanTags = $this->model->kanbanTags();
177
        foreach ($tags as $position => $tag) {
178
            $kanbanTags->attach([$tag->id => ['position' => $position]]);
179
        }
180
181
        return true;
182
    }
183
//
184
//    protected function createNote()
185
//    {
186
//        $this->model->notes()->create()
187
//    }
188
}
189