CommentInPostHandler.php 1.87 KB
<?php

namespace FootyRoom\App\Comment;

use FootyRoom\Core\Comment\CommentPolicy;
use FootyRoom\Repositories\PostRepository;
use FootyRoom\Repositories\UserRepository;

class CommentInPostHandler
{
    /**
     * @var \FootyRoom\Core\Comment\CommentingService
     */
    protected $commentingService;

    /**
     * @var \FootyRoom\Repositories\UserRepository
     */
    protected $userRepo;

    /**
     * @var \FootyRoom\Repositories\PostRepository
     */
    protected $postRepo;

    /**
     * @var \FootyRoom\Core\Comment\CommentPolicy
     */
    protected $commentPolicy;

    /**
     * Constructor.
     *
     * @param \FootyRoom\Repositories\PostRepository $postRepo
     * @param \FootyRoom\Repositories\UserRepository $userRepo
     * @param \FootyRoom\App\Comment\CommentingService $commentingService
     * @param \FootyRoom\Core\Comment\CommentPolicy $commentPolicy
     */
    public function __construct(
        PostRepository $postRepo,
        UserRepository $userRepo,
        CommentingService $commentingService,
        CommentPolicy $commentPolicy
    ) {
        $this->postRepo = $postRepo;
        $this->userRepo = $userRepo;
        $this->commentingService = $commentingService;
        $this->commentPolicy = $commentPolicy;
    }

    /**
     * Posts the comment.
     *
     * @param \FootyRoom\App\Comment\CommentCommand $command
     *
     * @return int
     */
    public function handle(CommentCommand $command)
    {
        $user = $this->userRepo->findById($command->authorUserId);

        $this->commentPolicy->canComment($user, true);

        $post = $this->postRepo->findById($command->commentableId);

        return $this->commentingService->commentSimple(
            $command->content,
            $command->images,
            $post,
            $command->parentId,
            $command->authorUserId,
            $command->authorName
        );
    }
}