CreatePollHandler.php
982 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?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();
}
}