Blame view
app/Repositories/CommentRepository.php
5.52 KB
e77200db5 Initial commit |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
<?php namespace FootyRoom\Repositories; use FootyRoom\Support\AutoMapper; use FootyRoom\Core\Comment\Author; use FootyRoom\Core\Comment\Comment; use Illuminate\Database\Connection; use FootyRoom\Core\Comment\Revision; use FootyRoom\Core\Comment\CommentChange; class CommentRepository { /** * @var \Illuminate\Database\Connection */ protected $mysql; /** * Constructor. * * @param \Illuminate\Database\Connection $mysql */ public function __construct(Connection $mysql) { $this->mysql = $mysql; } /** * Finds comment by id. * * @param int $id * * @return \FootyRoom\Core\Comment\Comment|null */ public function findById($id) { $commentDto = $this->mysql ->table('comments as c') ->select([ 'c.id as id', 'c.discussion_id as discussionId', 'c.user_id as userId', 'c.author as name', 'c.content', 'c.html', 'c.parent as parentId', 'c.datetime as date', 'c.status', 'cm_guest_id.meta_value as guestId', 'cx_teamname.teamname as teamName', ]) ->leftJoin('wp_commentmeta as cm_guest_id', function ($join) { $join->on('cm_guest_id.comment_id', '=', 'c.id') ->where('meta_key', '=', 'guest_id'); }) ->leftJoin('wp_comments_extra as cx_teamname', function ($join) { $join->on('cx_teamname.comment_id', '=', 'c.id'); }) ->where('c.id', '=', $id) ->first(); if (!$commentDto){ return null; } $comment = AutoMapper::map($commentDto, Comment::class); AutoMapper::setValue($comment, 'isApproved', (bool) $commentDto->status); AutoMapper::setValue($comment, 'author', AutoMapper::map($commentDto, Author::class)); return $comment; } /** * Saves new comment. * * @param \FootyRoom\Core\Comment\Comment $comment * * @return int Id of new comment. */ public function create(Comment $comment) { $commentId = $this->mysql ->table('comments') ->insertGetId([ 'discussion_id' => $comment->getDiscussionId(), 'user_id' => $comment->getAuthor()->getUserId(), 'author' => $comment->getAuthor()->getName() ?: '', 'content' => $comment->getContent(), 'html' => $comment->getHtml(), 'parent' => $comment->getParentId(), 'datetime' => $comment->getDate()->format('c'), 'status' => $comment->isApproved() ? '1' : '0', ]); AutoMapper::setValue($comment, 'id', $commentId); return $commentId; } /* * Updates content of a specified comment. * * @param int $commentId * @param string $content * @param string $html */ public function updateContent($commentId, $content, $html) { $this->mysql ->table('comments') ->where('id', '=', $commentId) ->update([ 'content' => $content, 'html' => $html, ]); } /* * Updates approval comment. * * @param int $commentId * @param string $content */ public function updateApproval($commentId, $approved) { $this->mysql ->table('comments') ->where('id', '=', $commentId) ->update([ 'status' => $approved, ]); } /** * Saves comment revision. * * @param \FootyRoom\Core\Comment\Revision $revision */ public function createRevision(Revision $revision) { $json = json_encode([ 'date' => $revision->getDate()->format('c'), 'content' => $revision->getContent(), ]); $this->createMeta($revision->getCommentId(), 'revision', $json); } /** * Creates comment change. * * @param \FootyRoom\Core\Comment\CommentChange $change */ public function createChange(CommentChange $change) { $this->mysql ->table('wp_commentmeta') ->insert([ 'comment_id' => $change->getCommentId(), 'meta_key' => 'edited', 'meta_value' => json_encode([ 'user_id' => $change->getUserId(), 'username' => $change->getUsername(), 'action' => $change->getAction(), 'reason' => $change->getReason(), 'created_at' => $change->getDate()->format('c'), ]), ]); } /** * Create meta for specified comment. * * @param int $commentId * @param string $key * @param string $value */ protected function createMeta($commentId, $key, $value) { $this->mysql ->table('wp_commentmeta') ->insert([ 'comment_id' => $commentId, 'meta_key' => $key, 'meta_value' => $value, ]); } /** * Increments comment karma. * * @param int $commentId */ public function incrementKarma($commentId) { $this->mysql->update( 'UPDATE comments SET karma = karma + 1 WHERE id = ?', [$commentId] ); } /** * Decrements comment karma. * * @param int $commentId */ public function decrementKarma($commentId) { $this->mysql->update( 'UPDATE comments SET karma = karma - 1 WHERE id = ?', [$commentId] ); } } |