NotifyWhenCommentPosted.php 3.74 KB
<?php

namespace FootyRoom\Core\Notification;

use FootyRoom\Core\Comment\CommentPosted;
use FootyRoom\Core\Match\Match;
use FootyRoom\Core\ForumPost\ForumPost;
use FootyRoom\Core\Post\Post;
use FootyRoom\Core\Wall\Wall;
use FootyRoom\Support\UrlGenerator;

class NotifyWhenCommentPosted
{
    /**
     * @var \FootyRoom\Core\Notification\CommentNotifier
     */
    protected $notifier;

    /**
     * @var \FootyRoom\Support\UrlGenerator
     */
    protected $urlGenerator;

    /**
     * Constructor.
     *
     * @param \FootyRoom\Core\Notification\CommentNotifier $notifier
     * @param \FootyRoom\Support\UrlGenerator $urlGenerator
     */
    public function __construct(CommentNotifier $notifier, UrlGenerator $urlGenerator)
    {
        $this->notifier = $notifier;
        $this->urlGenerator = $urlGenerator;
    }

    /**
     * Notify users of a reply to a comment.
     *
     * @param \FootyRoom\Core\Comment\CommentPosted $event
     */
    public function handle(CommentPosted $event)
    {
        if (!$event->comment->getParentId() &&
            !$event->commentable instanceof Wall &&
            !$event->commentable instanceof ForumPost
        ) {
            return;
        }

        $content = $this->createPayload($event);

        if ($event->commentable instanceof ForumPost) {
            $this->notifier->notifyForumReply(
                $event->commentable->getId(),
                $event->comment->getAuthor()->getUserId(),
                $event->comment->getId(),
                $content
            );
        } elseif ($event->commentable instanceof Wall && !$event->comment->getParentId()) {
            $this->notifier->notifyProfileWallComment(
                $event->commentable->getId(),
                $event->comment->getAuthor()->getUserId(),
                $event->comment->getId(),
                $content
            );
        } elseif ($event->commentable instanceof Wall) {
            $this->notifier->notifyProfileWallReply(
                $event->comment->getParentId(),
                $event->comment->getAuthor()->getUserId(),
                $event->comment->getId(),
                $event->commentable->getId(),
                $content
            );
        } else {
            $this->notifier->notifyReply(
                $event->comment->getParentId(),
                $event->comment->getAuthor()->getUserId(),
                $event->comment->getId(),
                $content
            );
        }
    }

    /**
     * Create payload for comment notification.
     *
     * @param \FootyRoom\Core\Comment\CommentPosted $comment
     *
     * @return string
     */
    protected function createPayload(CommentPosted $event)
    {
        $meta = [
            'username'      => $event->comment->getAuthor()->getName(),
            'user_id'       => $event->comment->getAuthor()->getUserId(),
            'object_id'     => "{$event->comment->getId()}",
            'object_type'   => 'comment',
        ];

        if ($event->commentable instanceof Post) {
            $meta['title'] = $this->getTitleForPost($event->commentable);
        } elseif ($event->commentable instanceof Match) {
            $meta['title'] = $this->getTitleForMatch($event->commentable);
        }

        $url = $this->urlGenerator->fromEntity($event->commentable);

        if (isset($url)) {
            $separator = strpos($url, '?') === false ? '?' : '&';
            $meta['url'] = "{$url}{$separator}commentId={$event->comment->getId()}";
        }

        return json_encode($meta);
    }

    protected function getTitleForMatch(Match $match)
    {
        return $match->getHomeTeam()->getName().' vs '.$match->getAwayTeam()->getName();
    }

    protected function getTitleForPost(Post $post)
    {
        return $post->getTitle();
    }
}