Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
97.26% |
71 / 73 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
| ProcessSave | |
97.26% |
71 / 73 |
|
60.00% |
3 / 5 |
20 | |
0.00% |
0 / 1 |
| readResponse | |
97.37% |
37 / 38 |
|
0.00% |
0 / 1 |
5 | |||
| getSuccessMessage | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| getConflictList | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
4 | |||
| writeUpdatedProcess | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
4 | |||
| shouldSendNotifications | |
92.31% |
12 / 13 |
|
0.00% |
0 / 1 |
5.01 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * |
| 5 | * @package Zmsadmin |
| 6 | * @copyright BerlinOnline Stadtportal GmbH & Co. KG |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | namespace BO\Zmsadmin; |
| 11 | |
| 12 | use BO\Mellon\Condition; |
| 13 | use BO\Slim\Render; |
| 14 | use BO\Zmsentities\Validator\ProcessValidator; |
| 15 | use BO\Zmsentities\Process as Entity; |
| 16 | use BO\Zmsadmin\Helper\AppointmentFormHelper; |
| 17 | |
| 18 | /** |
| 19 | * Update a process |
| 20 | */ |
| 21 | class ProcessSave extends BaseController |
| 22 | { |
| 23 | /** |
| 24 | * @SuppressWarnings(Param) |
| 25 | * @return String |
| 26 | */ |
| 27 | public function readResponse( |
| 28 | \Psr\Http\Message\RequestInterface $request, |
| 29 | \Psr\Http\Message\ResponseInterface $response, |
| 30 | array $args |
| 31 | ) { |
| 32 | $workstation = \App::$http->readGetResult('/workstation/', ['resolveReferences' => 2])->getEntity(); |
| 33 | |
| 34 | $validator = $request->getAttribute('validator'); |
| 35 | $input = $request->getParams(); |
| 36 | |
| 37 | $scope = Helper\AppointmentFormHelper::readSelectedScope($request, $workstation); |
| 38 | $processId = $validator->value($args['id'])->isNumber()->getValue(); |
| 39 | $process = \App::$http->readGetResult('/process/' . $processId . '/')->getEntity(); |
| 40 | $dateTime = ($process->isWithAppointment()) ? |
| 41 | (new \DateTime())->setTimestamp($process->getFirstAppointment()->date) : |
| 42 | \App::$now; |
| 43 | $shouldNotify = $this->shouldSendNotifications($input, $process); |
| 44 | $process->withUpdatedData($input, $dateTime, $scope); |
| 45 | |
| 46 | $validatedForm = ($process->isWithAppointment()) ? |
| 47 | ProcessReserve::getValidatedForm($validator, $process) : |
| 48 | ProcessQueue::getValidatedForm($validator, $process); |
| 49 | |
| 50 | if ($validatedForm['failed']) { |
| 51 | return \BO\Slim\Render::withJson( |
| 52 | $response, |
| 53 | $validatedForm |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | $process = $this->writeUpdatedProcess( |
| 58 | $input, |
| 59 | $process, |
| 60 | $validator, |
| 61 | $shouldNotify |
| 62 | ); |
| 63 | $appointment = $process->getFirstAppointment(); |
| 64 | $conflictList = ($process->isWithAppointment()) ? |
| 65 | static::getConflictList($scope->getId(), $appointment) : |
| 66 | null; |
| 67 | return \BO\Slim\Render::withHtml( |
| 68 | $response, |
| 69 | 'element/helper/messageHandler.twig', |
| 70 | array( |
| 71 | 'selectedprocess' => $process, |
| 72 | 'success' => $this->getSuccessMessage($process), |
| 73 | 'conflictlist' => $conflictList |
| 74 | ) |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | protected function getSuccessMessage(Entity $process) |
| 79 | { |
| 80 | return ($process->isWithAppointment()) ? 'process_updated' : 'process_withoutappointment_updated'; |
| 81 | } |
| 82 | |
| 83 | public static function getConflictList($scopeId, $appointment) |
| 84 | { |
| 85 | $conflictList = ScopeAvailabilityDay::readConflictList($scopeId, $appointment->getStartTime()); |
| 86 | $conflictList = ($conflictList && $conflictList->count()) ? |
| 87 | $conflictList |
| 88 | ->withTimeRangeByAppointment($appointment) |
| 89 | ->setConflictAmendment() |
| 90 | ->toConflictListByDay() : |
| 91 | null; |
| 92 | return (isset($conflictList)) ? $conflictList[$appointment->getStartTime()->format('Y-m-d')] : null; |
| 93 | } |
| 94 | |
| 95 | protected function writeUpdatedProcess($input, Entity $process, $validator, $notify = true) |
| 96 | { |
| 97 | $initiator = $validator->getParameter('initiator')->isString()->getValue(); |
| 98 | $process = \App::$http->readPostResult( |
| 99 | '/process/' . $process->id . '/' . $process->authKey . '/', |
| 100 | $process, |
| 101 | [ |
| 102 | 'initiator' => $initiator ?? 'admin', |
| 103 | 'slotType' => 'intern', |
| 104 | 'slotsRequired' => (isset($input['slotCount']) && 1 < $input['slotCount']) ? $input['slotCount'] : 0 |
| 105 | ] |
| 106 | )->getEntity(); |
| 107 | |
| 108 | if ($notify) { |
| 109 | AppointmentFormHelper::updateMailAndNotification($input, $process); |
| 110 | } |
| 111 | |
| 112 | return $process; |
| 113 | } |
| 114 | |
| 115 | private function shouldSendNotifications($requestData, \BO\Zmsentities\Schema\Entity $process) |
| 116 | { |
| 117 | $requestIds = $requestData['requests'] ?? []; |
| 118 | $currentRequestIds = []; |
| 119 | foreach ($process->getRequests() as $request) { |
| 120 | $currentRequestIds[] = $request['id']; |
| 121 | } |
| 122 | |
| 123 | if (array_merge(array_diff($requestIds, $currentRequestIds), array_diff($currentRequestIds, $requestIds))) { |
| 124 | return true; |
| 125 | } |
| 126 | |
| 127 | if ($process->getFirstClient()['familyName'] !== $requestData['familyName']) { |
| 128 | return true; |
| 129 | } |
| 130 | |
| 131 | $newDate = $requestData['selecteddate'] . ' ' |
| 132 | . str_replace('-', ':', $requestData['selectedtime']); |
| 133 | |
| 134 | if ($process->getFirstAppointment()->toDateTime()->format('Y-m-d H:i') !== $newDate) { |
| 135 | return true; |
| 136 | } |
| 137 | |
| 138 | return false; |
| 139 | } |
| 140 | } |