]> BookStack Code Mirror - bookstack/blob - app/Repos/CommentRepo.php
PSR2 fixes after running `./vendor/bin/phpcbf`
[bookstack] / app / Repos / CommentRepo.php
1 <?php namespace BookStack\Repos;
2
3 use BookStack\Comment;
4 use BookStack\Entity;
5
6 /**
7  * Class CommentRepo
8  * @package BookStack\Repos
9  */
10 class CommentRepo
11 {
12
13     /**
14      * @var Comment $comment
15      */
16     protected $comment;
17
18     /**
19      * CommentRepo constructor.
20      * @param Comment $comment
21      */
22     public function __construct(Comment $comment)
23     {
24         $this->comment = $comment;
25     }
26
27     /**
28      * Get a comment by ID.
29      * @param $id
30      * @return Comment|\Illuminate\Database\Eloquent\Model
31      */
32     public function getById($id)
33     {
34         return $this->comment->newQuery()->findOrFail($id);
35     }
36
37     /**
38      * Create a new comment on an entity.
39      * @param Entity $entity
40      * @param array $data
41      * @return Comment
42      */
43     public function create(Entity $entity, $data = [])
44     {
45         $userId = user()->id;
46         $comment = $this->comment->newInstance($data);
47         $comment->created_by = $userId;
48         $comment->updated_by = $userId;
49         $comment->local_id = $this->getNextLocalId($entity);
50         $entity->comments()->save($comment);
51         return $comment;
52     }
53
54     /**
55      * Update an existing comment.
56      * @param Comment $comment
57      * @param array $input
58      * @return mixed
59      */
60     public function update($comment, $input)
61     {
62         $comment->updated_by = user()->id;
63         $comment->update($input);
64         return $comment;
65     }
66
67     /**
68      * Delete a comment from the system.
69      * @param Comment $comment
70      * @return mixed
71      */
72     public function delete($comment)
73     {
74         return $comment->delete();
75     }
76
77     /**
78      * Get the next local ID relative to the linked entity.
79      * @param Entity $entity
80      * @return int
81      */
82     protected function getNextLocalId(Entity $entity)
83     {
84         $comments = $entity->comments(false)->orderBy('local_id', 'desc')->first();
85         if ($comments === null) {
86             return 1;
87         }
88         return $comments->local_id + 1;
89     }
90 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.