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

Updater::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
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\Note;
13
14
use Illuminate\Database\Eloquent;
15
use Tinyissue\Contracts\Model\UserInterface;
16
use Tinyissue\Model;
17
use Tinyissue\Model\Project;
18
use Tinyissue\Repository\RepositoryUpdater;
19
20
class Updater extends RepositoryUpdater
21
{
22
    public function __construct(Project\Note $model)
23
    {
24
        $this->model = $model;
25
    }
26
27
    /**
28
     * Create a new note.
29
     *
30
     * @param array $input
31
     *
32
     * @return $this
33
     */
34
    public function create(array $input)
35
    {
36
        $this->model->body       = $input['note_body'];
37
        $this->model->project_id = $this->model->project->id;
38
        $this->model->created_by = $this->model->createdBy->id;
39
40
        // Add event on successful save
41
//        Model\Project\Note::saved(function (Model\Project\Note $note) {
42
////            $this->queueAdd($note, $note->createdBy);// TODO
43
//        });
44
45
        $this->model->save();
46
47
        // Add to user's activity log
48
        $this->saveToActivity([
49
            'type_id'   => Model\Activity::TYPE_NOTE,
50
            'parent_id' => $this->model->project->id,
51
            'user_id'   => $this->model->createdBy->id,
52
        ]);
53
54
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Tinyissue\Repository\Project\Note\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...
55
    }
56
57
    /**
58
     * Update the note body.
59
     *
60
     * @param string     $body
61
     * @param UserInterface $user
62
     *
63
     * @return Eloquent\Model
64
     */
65
    public function updateBody($body, UserInterface $user)
0 ignored issues
show
Unused Code introduced by
The parameter $user is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
66
    {
67
        $this->model->body = $body;
68
69
        // Add event on successful save
70
//        Model\Project\Note::saved(function (Model\Project\Note $note) use ($user) {
71
////            $this->queueUpdate($note, $user);// TODO
72
//        });
73
74
        return $this->model->save();
75
    }
76
77
    /**
78
     * Delete a note.
79
     *
80
     * @return bool|null
81
     *
82
     * @throws \Exception
83
     */
84
    public function delete()
85
    {
86
        $this->model->activity()->delete();
87
88
        // Add event on successful delete
89
//        Model\Project\Note::deleted(function (Model\Project\Note $note) {
90
            // TODO
91
//            $this->queueDelete($note, $this->getLoggedUser());
92
//        });
93
94
        return $this->model->delete();
95
    }
96
}
97