CreatePollHandler.php 982 Bytes
<?php

namespace FootyRoom\App\Poll;

use FootyRoom\Core\Poll\Poll;
use FootyRoom\Core\Poll\Choice;
use FootyRoom\Repositories\Poll\PollRepository;

class CreatePollHandler
{
    /**
     * @var \FootyRoom\Repositories\Poll\PollRepository
     */
    protected $pollRepo;

    /**
     * Constructor.
     *
     * @param \Illuminate\Contracts\Events\Dispatcher $events
     */
    public function __construct(PollRepository $pollRepo)
    {
        $this->pollRepo = $pollRepo;
    }

    /**
     * Handler.
     *
     * @param \FootyRoom\App\Poll\CreatePollCommand $command
     *
     * @return int Poll id
     */
    public function handle(CreatePollCommand $command)
    {
        $choices = [];

        foreach ($command->choices as $key => $choice) {
            $choices[] = new Choice($key, $choice->text, $choice->image);
        }

        $poll = new Poll($choices, $command->userId);

        $this->pollRepo->create($poll);

        return $poll->getId();
    }
}