CreateRevisionWhenCommentEdited.php 1.2 KB
<?php

namespace FootyRoom\Core\Comment;

use FootyRoom\Repositories\CommentRepository;
use DateTime;

class CreateRevisionWhenCommentEdited
{
    /**
     * @var \FootyRoom\Repositories\CommentRepository
     */
    protected $commentRepo;

    /**
     * Constructor.
     *
     * @param \FootyRoom\Repositories\CommentRepository $commentRepo
     */
    public function __construct(CommentRepository $commentRepo)
    {
        $this->commentRepo = $commentRepo;
    }

    /**
     * Handler.
     *
     * @param \FootyRoom\Core\Comment\CommentEdited $event
     */
    public function handle(CommentEdited $event)
    {
        $revision = new Revision($event->comment->getId(), $event->oldHtml, new DateTime());

        $this->commentRepo->createRevision($revision);

        // Record this change if it's not performed by original author.
        if ($event->editor->getUserId() !== $event->comment->getAuthor()->getUserId()) {
            $change = new CommentChange(
                $event->comment->getId(),
                'edited',
                $event->editor->getUserId(),
                $event->editor->getUsername()
            );

            $this->commentRepo->createChange($change);
        }
    }
}