GetHoursWorkedInWeek   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 3
dl 0
loc 38
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __invoke() 0 24 4
1
<?php
2
3
namespace Scheduler\Application\Service;
4
5
use Aura\Payload\Payload;
6
use Scheduler\Domain\Model\Shift\HoursWorkedCalculator;
7
use Scheduler\Domain\Model\User\User;
8
9
class GetHoursWorkedInWeek
10
{
11
    const INVALID_DATE_MESSAGE = "must be a valid string representation of a date";
12
13
    private $payload;
14
    private $calculator;
15
16
    public function __construct(HoursWorkedCalculator $calculator)
17
    {
18
        $this->payload = new Payload();
19
        $this->calculator = $calculator;
20
    }
21
22
    public function __invoke(User $currentUser, $employeeId, $weekOf)
23
    {
24
        if (! $currentUser->isAuthenticated()) {
25
            return $this->payload->setStatus(Payload::NOT_AUTHENTICATED);
26
        }
27
28
        if ($currentUser->getId() !== $employeeId) {
29
            return $this->payload->setStatus(Payload::NOT_AUTHORIZED);
30
        }
31
32
        try {
33
            $weekOf = new \DateTimeImmutable($weekOf);
34
        } catch (\Exception $e) {
35
            return $this->payload->setStatus(Payload::NOT_VALID)
36
                                 ->setMessages([
37
                                     "date" => self::INVALID_DATE_MESSAGE
38
                                 ]);
39
        }
40
41
        $summary = $this->calculator->calculateHoursWorkedInWeek($currentUser, $weekOf);
42
43
        return $this->payload->setStatus(Payload::SUCCESS)
44
                             ->setOutput($summary);
45
    }
46
}
47