GetShift::__invoke()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
cc 4
nc 4
nop 3
1
<?php
2
3
namespace Scheduler\Application\Service;
4
5
use Aura\Payload\Payload;
6
use Scheduler\Domain\Model\Shift\Shift;
7
use Scheduler\Domain\Model\Shift\ShiftCoworkersFinder;
8
use Scheduler\Domain\Model\Shift\ShiftMapper;
9
10
class GetShift
11
{
12
    private $payload;
13
    private $shiftMapper;
14
    private $shiftCoworkersFinder;
15
16
    public function __construct(
17
        ShiftMapper $shiftMapper,
18
        ShiftCoworkersFinder $shiftCoworkersFinder
19
    ) {
20
        $this->payload = new Payload();
21
        $this->shiftMapper = $shiftMapper;
22
        $this->shiftCoworkersFinder = $shiftCoworkersFinder;
23
    }
24
25
    public function __invoke($currentUser, $shiftId, $withCoworkers)
26
    {
27
        if (! $currentUser->isAuthenticated()) {
28
            return $this->payload->setStatus(Payload::NOT_AUTHENTICATED);
29
        }
30
31
        $shift = $this->shiftMapper->find($shiftId);
32
33
        if ($shift === null) {
34
            return $this->payload->setStatus(Payload::NOT_FOUND);
35
        }
36
37
        if ($withCoworkers) {
38
            $shift = $shift->withCoworkers(
39
                $this->shiftCoworkersFinder->findCoworkersForShift($shift)
40
            );
41
        }
42
43
        return $this->payload->setStatus(Payload::SUCCESS)
44
                             ->setOutput($shift);
45
    }
46
}
47