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

Updater::delete()   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 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\Issue\Comment;
13
14
use Tinyissue\Contracts\Model\UserInterface;
15
use Tinyissue\Model\Activity;
16
use Tinyissue\Model\Project\Issue\Attachment;
17
use Tinyissue\Model\Project\Issue\Comment;
18
use Tinyissue\Model\User;
19
use Tinyissue\Repository\RepositoryUpdater;
20
21
class Updater extends RepositoryUpdater
22
{
23
    public function __construct(Comment $model)
24
    {
25
        $this->model = $model;
26
    }
27
28
    /**
29
     * Create new comment.
30
     *
31
     * @param array $input
32
     *
33
     * @return $this
34
     */
35
    public function create(array $input)
36
    {
37
        $fill = [
38
            'created_by' => $this->model->user->id,
39
            'project_id' => $this->model->project->id,
40
            'issue_id'   => $this->model->issue->id,
41
            'comment'    => $input['comment'],
42
        ];
43
44
        $this->model->fill($fill);
45
46
        // Add event on successful save
47
//        static::saved(function (Comment $comment) {
48
////            $this->queueAdd($comment, $comment->user);// TODO
49
//        });
50
51
        $this->save();
52
53
        /* Add to user's activity log */
54
        $this->saveToActivity([
55
            'type_id'   => Activity::TYPE_COMMENT,
56
            'parent_id' => $this->model->project->id,
57
            'item_id'   => $this->model->issue->id,
58
            'user_id'   => $this->model->user->id,
59
        ]);
60
61
        /* Add attachments to issue */
62
        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...
63
            $input['upload_token'],
64
            $this->model->user->id,
65
            $this->model->issue->id,
66
            $this->model->id
67
        );
68
69
        /* Update the project */
70
        $this->model->issue->changeUpdatedBy($this->model->user->id);
71
72
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Tinyissue\Repository\Project\Issue\Comment\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...
73
    }
74
75
    /**
76
     * Update comment body.
77
     *
78
     * @param string        $body
79
     * @param UserInterface $user
80
     *
81
     * @return Eloquent\Model
82
     */
83
    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...
84
    {
85
        $this->model->fill([
86
            'comment' => $body,
87
        ]);
88
89
        // Add event on successful save
90
//        static::saved(function (Issue\Comment $comment) use ($user) {
91
//            // @TODO
92
////            $this->queueUpdate($comment, $user);
93
//        });
94
95
        return $this->save();
96
    }
97
98
    public function delete()
99
    {
100
        return $this->transaction('deleteComment');
101
    }
102
103
    /**
104
     * Delete a comment and its attachments.
105
     *
106
     * @param UserInterface $user
0 ignored issues
show
Bug introduced by
There is no parameter named $user. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
107
     *
108
     * @return Eloquent\Model
109
     *
110
     * @throws \Exception
111
     */
112
    protected function deleteComment()
113
    {
114
        $this->model->activity()->delete();
115
116
        foreach ($this->model->attachments as $attachment) {
117
//            $path = $this->getUploadStorage($this->model->project_id, $attachment->upload_token);
118
//            $attachment->deleteFile($path, $attachment->filename);
119
            $attachment->updater()->delete();
120
        }
121
122
        // Add event on successful delete
123
//        static::deleted(function (Issue\Comment $comment) {
124
            // @TODO
125
//            $this->queueDelete($comment, $this->getLoggedUser());
126
//        });
127
128
        return $this->model->delete();
129
    }
130
}
131