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

Updater::updateBody()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
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\Issue\Comment;
13
14
use Tinyissue\Model\Activity;
15
use Tinyissue\Model\Message\Queue;
16
use Tinyissue\Model\Project\Issue;
17
use Tinyissue\Model\Project\Issue\Attachment;
18
use Tinyissue\Model\Project\Issue\Comment;
19
use Tinyissue\Model\User;
20
use Tinyissue\Repository\RepositoryUpdater;
21
22
class Updater extends RepositoryUpdater
23
{
24
    public function __construct(Comment $model)
25
    {
26
        $this->model = $model;
27
    }
28
29
    /**
30
     * Create new comment.
31
     *
32
     * @param array $input
33
     *
34
     * @return Comment
35
     */
36
    public function create(array $input)
37
    {
38
        $fill = [
39
            'created_by' => $this->model->user->id,
40
            'project_id' => $this->model->project->id,
41
            'issue_id'   => $this->model->issue->id,
42
            'comment'    => $input['comment'],
43
        ];
44
45
        $this->model->fill($fill);
46
47
        // Add event on successful save
48
        Comment::saved(function (Comment $comment) {
49
            $this->queueAdd($comment, $this->user);
0 ignored issues
show
Documentation introduced by
$this->user is of type object<Tinyissue\Contracts\Model\UserInterface>, but the function expects a integer|object<Tinyissue\Model\User>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
50
        });
51
52
        $this->save();
53
54
        /* Add to user's activity log */
55
        $this->saveToActivity([
56
            'type_id'   => Activity::TYPE_COMMENT,
57
            'parent_id' => $this->model->project->id,
58
            'item_id'   => $this->model->issue->id,
59
            'user_id'   => $this->model->user->id,
60
        ]);
61
62
        /* Add attachments to issue */
63
        Attachment::instance()->updateCommentToken(
0 ignored issues
show
Documentation Bug introduced by
The method updateCommentToken does not exist on object<Tinyissue\Model\Project\Issue\Attachment>? 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...
64
            array_get($input, 'upload_token'),
65
            $this->model->user->id,
66
            $this->model->issue->id,
67
            $this->model->id
68
        );
69
70
        /* Update the project */
71
        $this->model->issue->changeUpdatedBy($this->model->user->id);
72
73
        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...
74
    }
75
76
    /**
77
     * Update comment body.
78
     *
79
     * @param string $body
80
     *
81
     * @return Comment
82
     */
83
    public function updateBody($body)
84
    {
85
        $this->model->fill([
86
            'comment' => $body,
87
        ]);
88
89
        // Add event on successful save
90
        Comment::saved(function (Comment $comment) {
91
            $this->queueUpdate($comment, $this->user);
0 ignored issues
show
Documentation introduced by
$this->user is of type object<Tinyissue\Contracts\Model\UserInterface>, but the function expects a integer|object<Tinyissue\Model\User>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
92
        });
93
94
        return $this->save();
95
    }
96
97
    public function delete()
98
    {
99
        return $this->transaction('deleteComment');
100
    }
101
102
    /**
103
     * Delete a comment and its attachments.
104
     *
105
     * @return Comment
106
     *
107
     * @throws \Exception
108
     */
109
    protected function deleteComment()
110
    {
111
        $this->model->activity()->delete();
112
113
        foreach ($this->model->attachments as $attachment) {
114
            $attachment->updater()->delete();
115
        }
116
117
        // Add event on successful delete
118
        Comment::deleted(function (Comment $comment) {
119
            $this->queueDelete($comment, $this->user);
0 ignored issues
show
Documentation introduced by
$this->user is of type object<Tinyissue\Contracts\Model\UserInterface>, but the function expects a integer|object<Tinyissue\Model\User>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
120
        });
121
122
        return $this->model->delete();
123
    }
124
125
    /**
126
     * Insert add comment to message queue.
127
     *
128
     * @param Comment  $comment
129
     * @param int|User $changeBy
130
     *
131
     * @return void
132
     */
133
    public function queueAdd(Comment $comment, $changeBy)
134
    {
135
        // Skip message if issue closed
136
        if (!$comment->issue->isOpen()) {
137
            return;
138
        }
139
140
        return (new Queue())->queue(Queue::ADD_COMMENT, $comment, $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...
141
    }
142
143
    /**
144
     * Insert update comment to message queue.
145
     *
146
     * @param Comment  $comment
147
     * @param int|User $changeBy
148
     *
149
     * @return void
150
     */
151
    public function queueUpdate(Comment $comment, $changeBy)
152
    {
153
        // Skip message if issue closed or nothing changed in comment
154
        if (!$comment->issue->isOpen() || !$comment->isDirty()) {
155
            return;
156
        }
157
158
        return (new Queue())->queue(Queue::UPDATE_COMMENT, $comment, $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...
159
    }
160
161
    /**
162
     * Insert delete comment to message queue.
163
     *
164
     * @param Comment  $comment
165
     * @param int|User $changeBy
166
     *
167
     * @return void
168
     */
169
    public function queueDelete(Comment $comment, $changeBy)
170
    {
171
        // Skip message if issue closed
172
        if ($comment->issue instanceof Issue && !$comment->issue->isOpen()) {
173
            return;
174
        }
175
176
        return (new Queue())->queueDelete(Queue::DELETE_COMMENT, $comment, $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...
177
    }
178
}
179