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\NullUser; |
9
|
|
|
use Scheduler\Domain\Model\User\User; |
10
|
|
|
use Scheduler\Domain\Model\User\UserMapper; |
11
|
|
|
|
12
|
|
|
class UpdateShift |
13
|
|
|
{ |
14
|
|
|
const INVALID_DATE_MESSAGE = "must be a valid string representation of a date"; |
15
|
|
|
const SHIFT_NOT_FOUND_MESSAGE = "There is no shift for the specified id"; |
16
|
|
|
const USER_NOT_FOUND_MESSAGE = "There is no employee for the specified id"; |
17
|
|
|
|
18
|
|
|
private $payload; |
19
|
|
|
private $shiftMapper; |
20
|
|
|
private $userMapper; |
21
|
|
|
|
22
|
|
|
public function __construct(ShiftMapper $shiftMapper, UserMapper $userMapper) |
23
|
|
|
{ |
24
|
|
|
$this->payload = new Payload(); |
25
|
|
|
$this->shiftMapper = $shiftMapper; |
26
|
|
|
$this->userMapper = $userMapper; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function __invoke(User $currentUser, $shiftId, $employeeId, $start, $end, $break) { |
30
|
|
|
if (! $currentUser->isAuthenticated()) { |
31
|
|
|
return $this->payload->setStatus(Payload::NOT_AUTHENTICATED); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
if ($currentUser->getRole() !== "manager") { |
35
|
|
|
return $this->payload->setStatus(Payload::NOT_AUTHORIZED); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$invalid = []; |
39
|
|
|
try { |
40
|
|
|
$start = new DateTime($start); |
41
|
|
|
} catch (\Exception $e) { |
42
|
|
|
$invalid["start"] = self::INVALID_DATE_MESSAGE; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
try { |
46
|
|
|
$end = new DateTime($end); |
47
|
|
|
} catch (\Exception $e) { |
48
|
|
|
$invalid["end"] = self::INVALID_DATE_MESSAGE; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
View Code Duplication |
if (count($invalid)) { |
|
|
|
|
52
|
|
|
return $this->payload |
53
|
|
|
->setStatus(Payload::NOT_VALID) |
54
|
|
|
->setInput([$start, $end]) |
55
|
|
|
->setMessages($invalid); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$shift = $this->shiftMapper->find($shiftId); |
59
|
|
|
|
60
|
|
|
if ($shift === null) { |
61
|
|
|
return $this->payload->setStatus(Payload::NOT_FOUND) |
62
|
|
|
->setMessages(self::SHIFT_NOT_FOUND_MESSAGE); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if ($employeeId !== null) { |
66
|
|
|
$employee = $this->userMapper->find($employeeId); |
67
|
|
|
|
68
|
|
|
if ($employee instanceof NullUser) { |
69
|
|
|
return $this->payload->setStatus(Payload::NOT_FOUND) |
70
|
|
|
->setMessages(self::USER_NOT_FOUND_MESSAGE); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$shift = $shift->assignTo($employee); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$shift = $shift->changeStartTime($start) |
77
|
|
|
->changeEndTime($end) |
78
|
|
|
->changeBreak($break); |
79
|
|
|
|
80
|
|
|
$this->shiftMapper->update($shift); |
81
|
|
|
|
82
|
|
|
return $this->payload->setStatus(Payload::UPDATED) |
83
|
|
|
->setOutput($shift); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
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.