GetShiftsInTimePeriod   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 12.24 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 3
dl 6
loc 49
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B __invoke() 6 35 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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