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

AddEventHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 32
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A __invoke() 0 16 3
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