Lines
83.65%
87 / 104
Methods
50.00%
1 / 2
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| getError | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| handleException | 83.49% | 86 / 103 | 0.00% | 0 / 1 | 41.83 | ||
| 10 | class ExceptionService | |
| 11 | { | |
| 12 | private static function getError(string $key): array | |
| 13 | { | |
| 14 | return ErrorMessages::get($key); | |
| 15 | } | |
| 16 | ||
| 17 | /** | |
| 18 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) | |
| 19 | * @TODO: Consider using a strategy pattern or error handler chain to reduce method complexity | |
| 20 | */ | |
| 21 | public static function handleException(\Exception $e): never | |
| 22 | { | |
| 23 | $exceptionName = json_decode(json_encode($e), true)['template'] ?? null; | |
| 24 | ||
| 25 | if ($e instanceof RequestException) { | |
| 26 | $error = self::getError('zmsClientCommunicationError'); | |
| 27 | } else { | |
| 28 | switch ($exceptionName) { | |
| 29 | // Zmsslim exception | |
| 30 | case 'Slim\\Exception\\HttpNotFoundException': | |
| 31 | $error = self::getError('notFound'); | |
| 32 | ||
| 33 | break; | |
| 34 | ||
| 35 | // ZmsClient exception | |
| 36 | case 'BO\\Zmsclient\\Exception': | |
| 37 | $error = self::getError('zmsClientCommunicationError'); | |
| 38 | ||
| 39 | break; | |
| 40 | // Missing mail template exceptions | |
| 41 | case 'Twig\\Error\\RuntimeError': | |
| 42 | $error = self::getError('mailNotFound'); | |
| 43 | ||
| 44 | break; | |
| 45 | case 'Twig\\Error\\LoaderError': | |
| 46 | $error = self::getError('mailNotFound'); | |
| 47 | ||
| 48 | break; | |
| 49 | case 'BO\\Zmsbackend\\Mail\\Exception\\MailNotFound': | |
| 50 | $error = self::getError('mailNotFound'); | |
| 51 | ||
| 52 | break; | |
| 53 | // Process exceptions | |
| 54 | case 'BO\\Zmsbackend\\Process\\Exception\\ProcessNotFound': | |
| 55 | $error = self::getError('appointmentNotFound'); | |
| 56 | ||
| 57 | break; | |
| 58 | case 'BO\\Zmsbackend\\Process\\Exception\\AuthKeyMatchFailed': | |
| 59 | case 'BO\\Zmsbackend\\Process\\Exception\\ExternalUserIdMatchFailed': | |
| 60 | $error = self::getError('authKeyMismatch'); | |
| 61 | ||
| 62 | break; | |
| 63 | case 'BO\\Zmsbackend\\Process\\Exception\\ProcessAlreadyCalled': | |
| 64 | $error = self::getError('processAlreadyCalled'); | |
| 65 | ||
| 66 | break; | |
| 67 | case 'BO\\Zmsbackend\\Process\\Exception\\ProcessNotReservedAnymore': | |
| 68 | $error = self::getError('processNotReservedAnymore'); | |
| 69 | ||
| 70 | break; | |
| 71 | case 'BO\\Zmsbackend\\Process\\Exception\\ProcessNotPreconfirmedAnymore': | |
| 72 | $error = self::getError('processNotPreconfirmedAnymore'); | |
| 73 | ||
| 74 | break; | |
| 75 | case 'BO\\Zmsbackend\\Process\\Exception\\ProcessDeleteFailed': | |
| 76 | $error = self::getError('processDeleteFailed'); | |
| 77 | ||
| 78 | break; | |
| 79 | case 'BO\\Zmsbackend\\Process\\Exception\\ProcessInvalid': | |
| 80 | $error = self::getError('processInvalid'); | |
| 81 | ||
| 82 | break; | |
| 83 | case 'BO\\Zmsbackend\\Process\\Exception\\ProcessAlreadyExists': | |
| 84 | $error = self::getError('processAlreadyExists'); | |
| 85 | ||
| 86 | break; | |
| 87 | case 'BO\\Zmsbackend\\Process\\Exception\\EmailRequired': | |
| 88 | $error = self::getError('emailIsRequired'); | |
| 89 | ||
| 90 | break; | |
| 91 | case 'BO\\Zmsbackend\\Process\\Exception\\TelephoneRequired': | |
| 92 | $error = self::getError('telephoneIsRequired'); | |
| 93 | ||
| 94 | break; | |
| 95 | case 'BO\\Zmsbackend\\Process\\Exception\\MoreThanAllowedAppointmentsPerMail': | |
| 96 | $error = self::getError('tooManyAppointmentsWithSameMail'); | |
| 97 | ||
| 98 | break; | |
| 99 | case 'BO\\Zmsbackend\\Process\\Exception\\MoreThanAllowedSlotsPerAppointment': | |
| 100 | $error = self::getError('tooManySlotsPerAppointment'); | |
| 101 | ||
| 102 | break; | |
| 103 | case 'BO\\Zmsbackend\\Process\\Exception\\MoreThanAllowedQuantityPerService': | |
| 104 | $error = self::getError('tooManyServicesPerAppointment'); | |
| 105 | ||
| 106 | break; | |
| 107 | case 'BO\\Zmsbackend\\Process\\Exception\\PreconfirmationExpired': | |
| 108 | $error = self::getError('preconfirmationExpired'); | |
| 109 | ||
| 110 | break; | |
| 111 | case 'BO\\Zmsbackend\\Process\\Exception\\ApiclientInvalid': | |
| 112 | $error = self::getError('invalidApiClient'); | |
| 113 | ||
| 114 | break; | |
| 115 | // Calendar exceptions | |
| 116 | case 'BO\\Zmsbackend\\Calendar\\Exception\\InvalidFirstDay': | |
| 117 | $error = self::getError('invalidDateRange'); | |
| 118 | ||
| 119 | break; | |
| 120 | case 'BO\\Zmsbackend\\Calendar\\Exception\\AppointmentsMissed': | |
| 121 | $error = self::getError('noAppointmentForThisScope'); | |
| 122 | break; | |
| 123 | case 'BO\\Zmsbackend\\Calendar\\Exception\\CalendarWithoutScopes': | |
| 124 | $error = self::getError('noAppointmentForThisScope'); | |
| 125 | break; | |
| 126 | // Other entity exceptions | |
| 127 | case 'BO\\Zmsbackend\\Department\\Exception\\DepartmentNotFound': | |
| 128 | $error = self::getError('departmentNotFound'); | |
| 129 | ||
| 130 | break; | |
| 131 | case 'BO\\Zmsbackend\\Organisation\\Exception\\OrganisationNotFound': | |
| 132 | $error = self::getError('organisationNotFound'); | |
| 133 | ||
| 134 | break; | |
| 135 | case 'BO\\Zmsbackend\\Provider\\Exception\\ProviderNotFound': | |
| 136 | $error = self::getError('providerNotFound'); | |
| 137 | ||
| 138 | break; | |
| 139 | case 'BO\\Zmsbackend\\Request\\Exception\\RequestNotFound': | |
| 140 | $error = self::getError('requestNotFound'); | |
| 141 | ||
| 142 | break; | |
| 143 | case 'BO\\Zmsbackend\\Scope\\Exception\\ScopeNotFound': | |
| 144 | $error = self::getError('scopeNotFound'); | |
| 145 | ||
| 146 | break; | |
| 147 | case 'BO\\Zmsbackend\\Source\\Exception\\SourceNotFound': | |
| 148 | $error = self::getError('sourceNotFound'); | |
| 149 | ||
| 150 | break; | |
| 151 | case 'BO\\Zmsentities\\Exception\\SchemaValidation': | |
| 152 | $error = self::getError('invalidSchema'); | |
| 153 | ||
| 154 | break; | |
| 155 | case 'BO\\Zmsbackend\\Useraccount\\Exception\\InvalidCredentials': | |
| 156 | $error = self::getError('invalidCredentials'); | |
| 157 | ||
| 158 | break; | |
| 159 | ||
| 160 | // Use original message for unmapped exceptions | |
| 161 | default: | |
| 162 | $error = [ | |
| 163 | 'errorCode' => $exceptionName ?? 'unknown', | |
| 164 | 'errorMessage' => $e->getMessage(), | |
| 165 | 'statusCode' => $e->getCode() ?: 500 | |
| 166 | ]; | |
| 167 | } | |
| 168 | } | |
| 169 | ||
| 170 | throw new \RuntimeException($error['errorCode'] . ': ' . $error['errorMessage'], $error['statusCode'], $e); | |
| 171 | } | |
| 172 | } |