Commentable::deleteComment()   A
last analyzed

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
namespace Yoeunes\Commentable\Traits;
4
5
use Yoeunes\Commentable\CommentBuilder;
6
use Illuminate\Database\Eloquent\Builder;
7
use Illuminate\Database\Query\JoinClause;
8
use Yoeunes\Commentable\CommentQueryBuilder;
9
use Illuminate\Database\Eloquent\Relations\Relation;
10
11
trait Commentable
12
{
13
    /**
14
     * This model has many comments.
15
     *
16
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
17
     */
18
    public function comments()
19
    {
20
        return $this->morphMany(config('commentable.comment'), 'commentable');
0 ignored issues
show
Bug introduced by
It seems like morphMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
21
    }
22
23
    public function commentsCount()
24
    {
25
        return $this->comments()->count();
26
    }
27
28
    public function isCommented()
29
    {
30
        return $this->comments()->exists();
31
    }
32
33
    public function isCommentedByUser(int $user_id)
34
    {
35
        return $this->comments()->where('user_id', $user_id)->exists();
36
    }
37
38
    public function scopeOrderByCommentsCount(Builder $query, string $direction = 'asc')
39
    {
40
        return $query
41
            ->leftJoin('comments', function (JoinClause $join) {
42
                $join
43
                    ->on('comments.commentable_id', $this->getTable() . '.id')
0 ignored issues
show
Bug introduced by
It seems like getTable() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
44
                    ->where('comments.commentable_type', in_array(__CLASS__, Relation::morphMap()) ? array_search(__CLASS__, Relation::morphMap()) : __CLASS__);
45
            })
46
            ->addSelect(DB::raw('COUNT(comments.id) as count_comments'))
47
            ->groupBy($this->getTable(). '.id')
0 ignored issues
show
Bug introduced by
It seems like getTable() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
48
            ->orderBy('count_comments', $direction);
49
    }
50
51
    public function deleteComment(int $comment_id)
52
    {
53
        return $this->comments()->where('id', $comment_id)->delete();
54
    }
55
56
    public function resetComments()
57
    {
58
        return $this->comments()->delete();
59
    }
60
61
    public function deleteCommentsForUser(int $user_id)
62
    {
63
        return $this->comments()->where('user_id', $user_id)->delete();
64
    }
65
66
    public function updateComment(int $comment_id, string $comment)
67
    {
68
        return $this->comments()->where('id', $comment_id)->update(['comment' => $comment]);
69
    }
70
71
    public function getCommentBuilder()
72
    {
73
        return (new CommentBuilder())
74
            ->commentable($this);
75
    }
76
77
    /**
78
     * @return CommentQueryBuilder
79
     */
80
    public function getCommentQueryBuilder()
81
    {
82
        return new CommentQueryBuilder($this->comments());
83
    }
84
85
    public function commenters()
86
    {
87
        return $this->morphToMany(config('commentable.user'), 'commentable', 'comments');
0 ignored issues
show
Bug introduced by
It seems like morphToMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
88
    }
89
}
90