OverrideUpdate.php 2.37 KB
Newer Older
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php

namespace Source\UseCases\Overrides\OverrideUpdate;

use Carbon\Carbon;
use Source\Entities\Override;
use Source\Gateways\Doors\DoorsRepository;
use Source\Gateways\Users\UsersRepository;
use Source\Exceptions\EntityNotFoundException;
use Source\Gateways\Overrides\OverridesRepository;
use Source\UseCases\Overrides\OverrideCreate\Presenter;
use Source\UseCases\Overrides\OverrideCreate\ResponseModel;

class OverrideUpdate implements OverrideUpdateUseCase
{
    /**
     * @var \Source\Gateways\Overrides\OverridesRepository
     */
    protected OverridesRepository $overrides;

    /**
     * @var \Source\Gateways\Users\UsersRepository
     */
    protected UsersRepository $users;

    /**
     * @var \Source\Gateways\Doors\DoorsRepository
     */
    protected DoorsRepository $doors;

    public function __construct(OverridesRepository $overrides, UsersRepository $users, DoorsRepository $doors)
    {
        $this->overrides = $overrides;
        $this->users = $users;
        $this->doors = $doors;
    }

    /**
     * @inheritDoc
     */
    public function update(string $overrideId, array $attributes, Presenter $presenter): void
    {
        $override = $this->overrides->get($overrideId);
        if (!$override) {
            throw new EntityNotFoundException('Override does not exist.');
        }

        $reason = $attributes['reason'] ?? $override->getReason();
        $start = $attributes['start'] ?? null;
        $end = $attributes['end'] ?? null;
        if ($start) {
            $start = new Carbon($start);
        } else {
            $start = $override->getStart();
        }
        if ($end) {
            $end = new Carbon($end);
        } else {
            $end = $override->getEnd();
        }

        $override = $this->overrides->updateOverride($overrideId, new Override(
            $override->getId(),
            $reason,
            $override->getUserId(),
            $override->getDoorId(),
            $override->getType(),
            $start,
            $end
        ));

        $response = new ResponseModel();

        if (!$override) {
            $response->addError('Something went wrong when adding the override');
        } else {
            $response->setOverride($override);
        }

        if ($response->hasError()) {
            $presenter->presentError($response);
        } else {
            $presenter->present($response);
        }
    }
}