CommentsRepository::approve()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
dl 0
loc 7
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace WebDevEtc\BlogEtc\Repositories;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\ModelNotFoundException;
7
use WebDevEtc\BlogEtc\Exceptions\CommentNotFoundException;
8
use WebDevEtc\BlogEtc\Models\Comment;
9
use WebDevEtc\BlogEtc\Models\Post;
10
11
class CommentsRepository
12
{
13
    /**
14
     * @var Comment
15
     */
16
    private $model;
17
18
    /**
19
     * BlogEtcCommentsRepository constructor.
20
     */
21
    public function __construct(Comment $model)
22
    {
23
        $this->model = $model;
24
    }
25
26
    /**
27
     * Approve a blog comment.
28
     */
29
    public function approve(int $blogCommentID): Comment
30
    {
31
        $comment = $this->find($blogCommentID, false);
32
        $comment->approved = true;
33
        $comment->save();
34
35
        return $comment;
36
    }
37
38
    /**
39
     * Find and return a comment by ID.
40
     *
41
     * If $onlyApproved is true, then it will only return an approved comment
42
     * If it is false then it can return it even if not yet approved
43
     */
44
    public function find(int $blogEtcCommentID, bool $onlyApproved = true): Comment
45
    {
46
        try {
47
            $queryBuilder = $this->query(true);
48
49
            if (!$onlyApproved) {
50
                $queryBuilder->withoutGlobalScopes();
51
            }
52
53
            return $queryBuilder->findOrFail($blogEtcCommentID);
54
        } catch (ModelNotFoundException $e) {
55
            throw new CommentNotFoundException('Unable to find blog post comment with ID: '.$blogEtcCommentID);
56
        }
57
    }
58
59
    /**
60
     * Return new instance of the Query Builder for this model.
61
     */
62
    public function query(bool $eagerLoad = false): Builder
63
    {
64
        $queryBuilder = $this->model->newQuery();
65
66
        if (true === $eagerLoad) {
67
            $queryBuilder->with('post');
68
        }
69
70
        return $queryBuilder;
71
    }
72
73
    /**
74
     * Create a comment.
75
     */
76
    public function create(
77
        Post $post,
78
        array $attributes,
79
        string $ip = null,
80
        string $authorWebsite = null,
81
        string $authorEmail = null,
82
        int $userID = null,
83
        bool $autoApproved = false
84
    ): Comment {
85
        // TODO - inject the model object, put into repo, generate $attributes
86
        $newComment = new Comment($attributes);
87
88
        $newComment->ip = $ip;
89
        $newComment->author_website = $authorWebsite;
90
        $newComment->author_email = $authorEmail;
91
        $newComment->user_id = $userID;
92
        $newComment->approved = $autoApproved;
93
94
        $post->comments()->save($newComment);
95
96
        return $newComment;
97
    }
98
}
99