Completed
Branch develop-3.0 (ae28cb)
by Mohamed
08:28
created

Updater::queueUpdate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
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 Tinyissue\Contracts\Model\UserInterface;
15
use Tinyissue\Model;
16
use Tinyissue\Model\Message\Queue;
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 Project\Note
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
        Project\Note::saved(function (Project\Note $note) {
42
            $this->queueAdd($note, $this->user);
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->model;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->model; (Illuminate\Database\Eloquent\Model) 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
     *
62
     * @return Project\Note
63
     */
64
    public function updateBody($body)
65
    {
66
        $this->model->body = $body;
67
68
        // Add event on successful save
69
        Project\Note::saved(function (Project\Note $note) {
70
            $this->queueUpdate($note, $this->user);
71
        });
72
73
        return $this->model->save();
74
    }
75
76
    /**
77
     * Delete a note.
78
     *
79
     * @return bool|null
80
     *
81
     * @throws \Exception
82
     */
83
    public function delete()
84
    {
85
        $this->model->activity()->delete();
86
87
        // Add event on successful delete
88
        Project\Note::deleted(function (Project\Note $note) {
89
            $this->queueDelete($note, $this->user);
90
        });
91
92
        return $this->model->delete();
93
    }
94
95
    /**
96
     * Insert add note to message queue.
97
     *
98
     * @param Project\Note  $note
99
     * @param UserInterface $changeBy
100
     *
101
     * @return void
102
     */
103
    public function queueAdd(Project\Note $note, UserInterface $changeBy)
104
    {
105
        return (new Queue())->queue(Queue::ADD_NOTE, $note, $changeBy);
0 ignored issues
show
Documentation Bug introduced by
The method queue does not exist on object<Tinyissue\Model\Message\Queue>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
106
    }
107
108
    /**
109
     * Insert update note to message queue.
110
     *
111
     * @param Project\Note  $note
112
     * @param UserInterface $changeBy
113
     *
114
     * @return void
115
     */
116
    public function queueUpdate(Project\Note $note, UserInterface $changeBy)
117
    {
118
        // Skip message if nothing changed in note
119
        if (!$note->isDirty()) {
120
            return;
121
        }
122
123
        return (new Queue())->queue(Queue::UPDATE_NOTE, $note, $changeBy);
0 ignored issues
show
Documentation Bug introduced by
The method queue does not exist on object<Tinyissue\Model\Message\Queue>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
124
    }
125
126
    /**
127
     * Insert delete note to message queue.
128
     *
129
     * @param Project\Note  $note
130
     * @param UserInterface $changeBy
131
     *
132
     * @return void
133
     */
134
    public function queueDelete(Project\Note $note, UserInterface $changeBy)
135
    {
136
        return (new Queue())->queueDelete(Queue::DELETE_NOTE, $note, $changeBy);
0 ignored issues
show
Bug introduced by
The method queueDelete() does not exist on Tinyissue\Model\Message\Queue. Did you maybe mean delete()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
137
    }
138
}
139