Completed
Push — develop ( 9ca5e1...fd1e5f )
by Baptiste
02:49
created

AddEventHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 3
crap 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace PersonalGalaxy\Calendar\Handler;
5
6
use PersonalGalaxy\Calendar\{
7
    Command\AddEvent,
8
    Repository\EventRepository,
9
    Repository\AgendaRepository,
10
    Entity\Event,
11
    Exception\EventCannotBeDeclaredInThePast,
12
    Exception\AgendaNotFound,
13
};
14
use Innmind\TimeContinuum\TimeContinuumInterface;
15
16
final class AddEventHandler
17
{
18
    private $events;
19
    private $agendas;
20
    private $clock;
21
22 3
    public function __construct(
23
        EventRepository $events,
24
        AgendaRepository $agendas,
25
        TimeContinuumInterface $clock
26
    ) {
27 3
        $this->events = $events;
28 3
        $this->agendas = $agendas;
29 3
        $this->clock = $clock;
30 3
    }
31
32 3
    public function __invoke(AddEvent $wished): void
33
    {
34 3
        if ($this->clock->now()->aheadOf($wished->pointInTime())) {
35 1
            throw new EventCannotBeDeclaredInThePast($wished->pointInTime());
36
        }
37
38 2
        if (!$this->agendas->has($wished->agenda())) {
39 1
            throw new AgendaNotFound($wished->agenda());
40
        }
41
42 1
        $this->events->add(
43 1
            Event::add(
44 1
                $wished->identity(),
45 1
                $wished->agenda(),
46 1
                $wished->name(),
47 1
                $wished->pointInTime()
48
            )
49
        );
50 1
    }
51
}
52