Passed
Push — master ( ab2678...bf154d )
by webdevetc
14:17
created

CommentsService::delete()   A

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\Services;
4
5
use Exception;
6
use WebDevEtc\BlogEtc\Events\CommentAdded;
7
use WebDevEtc\BlogEtc\Events\CommentApproved;
8
use WebDevEtc\BlogEtc\Events\CommentWillBeDeleted;
9
use WebDevEtc\BlogEtc\Models\Comment;
10
use WebDevEtc\BlogEtc\Models\Post;
11
use WebDevEtc\BlogEtc\Repositories\CommentsRepository;
12
13
/**
14
 * Class BlogEtcCategoriesService.
15
 *
16
 * Service class to handle most logic relating to Comment entries.
17
 */
18
class CommentsService
19
{
20
    // comment system types. Set these in config file
21
    public const COMMENT_TYPE_BUILT_IN = 'built_in';
22
    public const COMMENT_TYPE_DISQUS = 'disqus';
23
    public const COMMENT_TYPE_CUSTOM = 'custom';
24
    public const COMMENT_TYPE_DISABLED = 'disabled';
25
26
    /** @var CommentsRepository */
27
    private $repository;
28
29
    /**
30
     * CommentsService constructor.
31
     */
32
    public function __construct(CommentsRepository $repository)
33
    {
34
        $this->repository = $repository;
35
    }
36
37
    /**
38
     * BlogEtcCategoriesRepository repository - for query heavy method.
39
     */
40
    public function repository(): CommentsRepository
41
    {
42
        return $this->repository;
43
    }
44
45
    /**
46
     * Create a new comment.
47
     */
48
    public function create(
49
        Post $blogEtcPost,
50
        array $attributes,
51
        string $ip = null,
52
        int $userID = null
53
    ): Comment {
54
        $ip = config('blogetc.comments.save_ip_address')
55
            ? $ip : null;
56
57
        $authorWebsite = config('blogetc.comments.ask_for_author_website') && !empty($attributes['author_website'])
58
            ? $attributes['author_website']
59
            : null;
60
61
        $authorEmail = config('blogetc.comments.ask_for_author_website') && !empty($attributes['author_email'])
62
            ? $attributes['author_email']
63
            : null;
64
65
        $userID = config('blogetc.comments.save_user_id_if_logged_in')
66
            ? $userID
67
            : null;
68
69
        $approved = $this->autoApproved();
70
71
        $newComment = $this->repository->create(
72
            $blogEtcPost,
73
            $attributes,
74
            $ip,
75
            $authorWebsite,
76
            $authorEmail,
77
            $userID,
78
            $approved
79
        );
80
81
        event(new CommentAdded($blogEtcPost, $newComment));
82
83
        return $newComment;
84
    }
85
86
    /**
87
     * Are comments auto approved?
88
     */
89
    protected function autoApproved(): bool
90
    {
91
        return true === config('blogetc.comments.auto_approve_comments', true);
92
    }
93
94
    /**
95
     * Approve a blog comment.
96
     */
97
    public function approve(int $blogCommentID): Comment
98
    {
99
        $comment = $this->repository->approve($blogCommentID);
100
        event(new CommentApproved($comment));
101
102
        return $comment;
103
    }
104
105
    /**
106
     * Delete a blog comment.
107
     *
108
     * Returns the now deleted comment object
109
     *
110
     * @throws Exception
111
     */
112
    public function delete(int $blogCommentID): Comment
113
    {
114
        $comment = $this->find($blogCommentID, false);
115
        event(new CommentWillBeDeleted($comment));
116
        $comment->delete();
117
118
        return $comment;
119
    }
120
121
    /**
122
     * Find and return a comment by ID.
123
     */
124
    public function find(int $blogEtcCommentID, bool $onlyApproved = true): Comment
125
    {
126
        return $this->repository->find($blogEtcCommentID, $onlyApproved);
127
    }
128
}
129