CommentRepository.php 5.52 KB
<?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]
        );
    }
}