CommentInProfileWallHandler.php 1.94 KB
<?php

namespace FootyRoom\App\Comment;

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

class CommentInProfileWallHandler
{
    /**
     * @var \FootyRoom\Repositories\WallRepository
     */
    protected $wallPostRepo;

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

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

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

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

    /**
     * Handler.
     *
     * @param \FootyRoom\App\Comment\CommentInProfileWallCommand $command
     *
     * @return int Id of new comment.
     */
    public function handle(CommentInProfileWallCommand $command)
    {
        $user = $this->userRepo->findById($command->authorUserId);

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

        $wall = $this->wallPostRepo->findByUserId($command->commentableId);

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