EditPollHandler.php 1.25 KB
<?php

namespace FootyRoom\App\Poll;

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

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

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

    /**
     * @var \Illuminate\Contracts\Events\Dispatcher
     */
    protected $events;

    /**
     * Constructor.
     *
     * @param \FootyRoom\Repositories\UserRepository $userRepo
     * @param \FootyRoom\Repositories\Poll\PollRepository $pollRepo
     */
    public function __construct(UserRepository $userRepo, PollRepository $pollRepo)
    {
        $this->userRepo = $userRepo;
        $this->pollRepo = $pollRepo;
    }

    /**
     * Handler.
     *
     * @param \FootyRoom\App\Poll\EditPollCommand $command
     */
    public function handle(EditPollCommand $command)
    {
        $poll = $this->pollRepo->findById($command->pollId);

        $choices = [];

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

        $poll->edit($choices);

        $this->pollRepo->update($poll);
    }
}