GetShiftsInTimePeriod::__invoke()   B
last analyzed

Complexity

Conditions 6
Paths 10

Size

Total Lines 35

Duplication

Lines 6
Ratio 17.14 %

Importance

Changes 0
Metric Value
dl 6
loc 35
rs 8.7377
c 0
b 0
f 0
cc 6
nc 10
nop 3
1
<?php
2
3
namespace Scheduler\Application\Service;
4
5
use DateTime;
6
use Aura\Payload\Payload;
7
use Scheduler\Domain\Model\Shift\ShiftMapper;
8
use Scheduler\Domain\Model\User\User;
9
10
class GetShiftsInTimePeriod
11
{
12
    const INVALID_DATE_MESSAGE = "must be a valid string representation of a date";
13
14
    private $payload;
15
    private $shiftMapper;
16
17
    public function __construct(ShiftMapper $shiftMapper)
18
    {
19
        $this->payload = new Payload();
20
        $this->shiftMapper = $shiftMapper;
21
    }
22
23
    public function __invoke(User $user, $start, $end)
24
    {
25
        if (! $user->isAuthenticated()) {
26
            return $this->payload->setStatus(Payload::NOT_AUTHENTICATED);
27
        }
28
29
        if ($user->getRole() !== "manager") {
30
            return $this->payload->setStatus(Payload::NOT_AUTHORIZED);
31
        }
32
33
        $invalid = [];
34
        try {
35
            $start = new DateTime($start);
36
        } catch (\Exception $e) {
37
            $invalid["start"] = self::INVALID_DATE_MESSAGE;
38
        }
39
40
        try {
41
            $end = new DateTime($end);
42
        } catch (\Exception $e) {
43
            $invalid["end"] = self::INVALID_DATE_MESSAGE;
44
        }
45
46 View Code Duplication
        if (count($invalid)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
            return $this->payload
48
                            ->setStatus(Payload::NOT_VALID)
49
                            ->setInput([$start, $end])
50
                            ->setMessages($invalid);
51
        }
52
53
        $shifts = $this->shiftMapper->findShiftsInTimePeriod($start, $end);
54
55
        return $this->payload->setStatus(Payload::SUCCESS)
56
                             ->setOutput($shifts);
57
    }
58
}
59