| Total Complexity | 79 |
| Total Lines | 492 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 1 | Features | 1 |
Complex classes like TraitBackEndRomania often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TraitBackEndRomania, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | trait TraitBackEndRomania |
||
| 22 | { |
||
| 23 | use \danielgp\io_operations\InputOutputCurl; |
||
| 24 | use \danielgp\efactura\TraitBasic; |
||
| 25 | |||
| 26 | private array $arraystandardMesageFilters = [ |
||
| 27 | [ |
||
| 28 | 'Code' => 'E', |
||
| 29 | 'Detail' => 'raspuns cu erorile si semnatura MF, livrat in urma transmiterii unei facturi' |
||
| 30 | . ', daca NU este validata de sistem', |
||
| 31 | 'Value' => 'ERORI FACTURA', |
||
| 32 | ], |
||
| 33 | [ |
||
| 34 | 'Code' => 'P', |
||
| 35 | 'Detail' => 'factura primita in calitate de cunparator de la un emitent' |
||
| 36 | . ' care a folosit sistemul national' |
||
| 37 | . ' privind factura electronica RO e-factura', |
||
| 38 | 'Value' => 'FACTURA PRIMITA', |
||
| 39 | ], |
||
| 40 | [ |
||
| 41 | 'Code' => 'T', |
||
| 42 | 'Detail' => 'raspuns cu factura originala si semnatura MF' |
||
| 43 | . ', transmis in urma transmiterii unei facturi, daca este validata de sistem', |
||
| 44 | 'Value' => 'FACTURA TRIMISA', |
||
| 45 | ], |
||
| 46 | [ |
||
| 47 | 'Code' => 'R', |
||
| 48 | 'Detail' => 'mesaj de la cumparator catre emitentul facturii' |
||
| 49 | . ' (încărcate în sistem prin serviciul de upload ca RASP)', |
||
| 50 | 'Value' => 'MESAJ CUMPARATOR PRIMIT / MESAJ CUMPARATOR TRANSMIS', |
||
| 51 | ], |
||
| 52 | ]; |
||
| 53 | // below variable has to be initialized by class consuming this Trait |
||
| 54 | protected array $arraySolutionCustomSettings = [ |
||
| 55 | 'ArrayElectronicInvoiceSecrets' => [ |
||
| 56 | 'Client_ID' => '', |
||
| 57 | 'Client_Secret' => '', |
||
| 58 | 'Token' => '', |
||
| 59 | ], |
||
| 60 | 'ArrayFolders' => [ |
||
| 61 | 'Generated XML' => '', |
||
| 62 | 'Uploaded XML' => '', |
||
| 63 | 'Downloaded ZIP' => '', |
||
| 64 | ], |
||
| 65 | 'ArrayStrategyForUpload' => [ |
||
| 66 | 'B2B' => 'File base name end with|__b2b.xml', |
||
| 67 | 'B2C' => 'File base name end with|__b2c.xml', |
||
| 68 | 'B2G' => 'File base name end with|__b2g.xml', |
||
| 69 | ], |
||
| 70 | 'IntegerNumberOfDaysToCheckMessages' => 0, |
||
| 71 | 'IntegerTaxIdentificationNumber' => 0, |
||
| 72 | 'StringElectronicInvoiceEnvironment' => '', |
||
| 73 | 'StringElectronicInvoiceStandard' => 'UBL', |
||
| 74 | 'StringOptionalUploadParameters' => '', // could be one of the following: extern=DA | autofactura=DA | executare=DA |
||
| 75 | ]; |
||
| 76 | |||
| 77 | private function authorizeConnectionToLegalAuthority(): void |
||
| 78 | { |
||
| 79 | $this->checkPrerequisite([ |
||
| 80 | 'Empty Secrets Array' => $this->arraySolutionCustomSettings['ArrayElectronicInvoiceSecrets'], |
||
| 81 | ]); |
||
| 82 | $strRelevantUrl = $this->arraySettings['Infrastructure']['RO']['Servers']['Login'] |
||
| 83 | . $this->arraySettings['Infrastructure']['RO']['Calls']['Login']['Authorisation'] |
||
| 84 | . '?' . implode('&', [ |
||
| 85 | 'client_id=' . $this->arraySolutionCustomSettings['ArrayElectronicInvoiceSecrets']['Client_ID'], |
||
| 86 | 'client_secret=' . $this->arraySolutionCustomSettings['ArrayElectronicInvoiceSecrets']['Client_Secret'], |
||
| 87 | 'response_type=' . 'code', |
||
| 88 | 'redirect_uri=' . $this->arraySettings['Infrastructure']['RO']['Servers']['Redirect'] |
||
| 89 | . $this->arraySettings['Infrastructure']['RO']['Calls']['Redirect'], |
||
| 90 | ]); |
||
| 91 | header('Location: ' . $strRelevantUrl); |
||
| 92 | } |
||
| 93 | |||
| 94 | private function buildHeaderAsArray(): array |
||
| 99 | ]; |
||
| 100 | } |
||
| 101 | |||
| 102 | private function checkPrerequisite(array $arrayAssignedChecks): void |
||
| 103 | { |
||
| 104 | $arrayErrorMessages = []; |
||
| 105 | foreach ($arrayAssignedChecks as $strLabelCheck => $elementChecked) { |
||
| 106 | switch($strLabelCheck) { |
||
| 107 | case 'Empty Environment': |
||
| 108 | if ($elementChecked == '') { |
||
| 109 | $arrayErrorMessages[] = 'Environment is NOT allowed to be empty' |
||
| 110 | . ', please ensure proper environment from your custom class...'; |
||
| 111 | } |
||
| 112 | break; |
||
| 113 | case 'Empty Secrets Array': |
||
| 114 | if ($elementChecked == []) { |
||
| 115 | $arrayErrorMessages[] = 'Secrets array is NOT allowed to be empty' |
||
| 116 | . ', please ensure proper building from your custom class...'; |
||
| 117 | } |
||
| 118 | break; |
||
| 119 | case 'Message Filter Value': |
||
| 120 | $arrayAllowedValues = array_column($this->arraystandardMesageFilters, 'Value'); |
||
| 121 | if (($elementChecked !== '') && !in_array($elementChecked, $arrayAllowedValues)) { |
||
| 122 | $arrayErrorMessages[] = vsprintf('Message Filter provided has a value of %s that is not allowed' |
||
| 123 | . ' as can only be one of the following: %s' |
||
| 124 | . ', please ensure proper value is given from your custom class...', [ |
||
| 125 | $elementChecked, |
||
| 126 | '"' . implode('" or "', array_keys($arrayAllowedValues)) . '"', |
||
| 127 | ]); |
||
| 128 | } |
||
| 129 | break; |
||
| 130 | case 'Message Type Value': |
||
| 131 | $arrayAllowedValues = array_keys($this->arraySettings['Infrastructure']['RO']['Calls']['Content']['Message']); |
||
| 132 | if (!in_array($elementChecked, $arrayAllowedValues)) { |
||
| 133 | $arrayErrorMessages[] = vsprintf('Message Type provided has a value of %s that is not allowed' |
||
| 134 | . ' as can only be one of the following: %s' |
||
| 135 | . ', please ensure proper value is given from your custom class...', [ |
||
| 136 | $elementChecked, |
||
| 137 | '"' . implode('" or "', array_keys($arrayAllowedValues)) . '"', |
||
| 138 | ]); |
||
| 139 | } |
||
| 140 | break; |
||
| 141 | case 'Non-Standard Environment Value': |
||
| 142 | $arrayAllowedValues = ['prod', 'test']; |
||
| 143 | if (($elementChecked !== '') && !in_array($elementChecked, $arrayAllowedValues)) { |
||
| 144 | $arrayErrorMessages[] = vsprintf('Environment provided has a value of %s that is not allowed' |
||
| 145 | . ' as can only be one of the following: %s' |
||
| 146 | . ', please ensure proper value is given from your custom class...', [ |
||
| 147 | $elementChecked, |
||
| 148 | '"' . implode('" or "', $arrayAllowedValues) . '"', |
||
| 149 | ]); |
||
| 150 | } |
||
| 151 | break; |
||
| 152 | case 'Zero Value': |
||
| 153 | if ($elementChecked === 0) { |
||
| 154 | $arrayErrorMessages[] = 'Tax Identification Number is not allowed to be 0' |
||
| 155 | . ', please ensure you pass proper value from your custom class...'; |
||
| 156 | } |
||
| 157 | break; |
||
| 158 | } |
||
| 159 | } |
||
| 160 | if ($arrayErrorMessages != []) { |
||
| 161 | error_log(implode(PHP_EOL, $arrayErrorMessages)); |
||
| 162 | throw new \RuntimeException(implode(PHP_EOL, $arrayErrorMessages)); |
||
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 166 | private function decideRelevantUniformResourceLocatorForUpload(string $strFileName): string |
||
| 167 | { |
||
| 168 | $strLabel = $this->setLabelBasedOnRule($strFileName); |
||
| 169 | error_log(sprintf('For given file %s following Label %s has been established!', $strFileName, $strLabel)); |
||
| 170 | $strUrlForUpload = $this->arraySettings['Infrastructure']['RO']['Calls']['Content']['Upload'][$strLabel]; |
||
| 171 | return vsprintf($strUrlForUpload, [ |
||
| 172 | $this->arraySolutionCustomSettings['StringElectronicInvoiceStandard'], |
||
| 173 | $this->arraySolutionCustomSettings['IntegerTaxIdentificationNumber'], |
||
| 174 | $this->arraySolutionCustomSettings['StringOptionalUploadParameters'], |
||
| 175 | ]); |
||
| 176 | } |
||
| 177 | |||
| 178 | private function exposeFeedbackOnFileProcessed(int $intFileFoundNo, int $intFileProcessed): void |
||
| 179 | { |
||
| 180 | if ($intFileFoundNo === 0) { |
||
| 181 | error_log('NO files were found to be processed.'); |
||
| 182 | } elseif ($intFileFoundNo == $intFileProcessed) { |
||
| 183 | error_log(sprintf('All %u files found were processed successfully!', $intFileFoundNo)); |
||
| 184 | } else { |
||
| 185 | error_log(vsprintf('Out of %u files found only %u were processed successfully, hence %u were missed.' |
||
| 186 | . ' Check your source folder where residual files are still there.', [ |
||
| 187 | $intFileFoundNo, |
||
| 188 | $intFileProcessed, |
||
| 189 | ($intFileFoundNo - $intFileProcessed), |
||
| 190 | ])); |
||
| 191 | } |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Work In Progress =================> UNIFNISHED !!! |
||
| 196 | * |
||
| 197 | * @param string $strType |
||
| 198 | * @param array $arrayParameters |
||
| 199 | * @return void |
||
| 200 | */ |
||
| 201 | protected function getElectronicInvoiceMessages(string $strType, array $arrayParameters): void |
||
| 202 | { |
||
| 203 | $this->getServerDiscutionValidations([ |
||
| 204 | 'Message Type Value', $strType, |
||
| 205 | 'Message Parameters' => $arrayParameters, |
||
| 206 | ]); |
||
| 207 | $this->getServerMessageParametersValidation($strType, $arrayParameters); |
||
| 208 | $this->authorizeConnectionToLegalAuthority(); |
||
| 209 | $strContentBranch = ''; |
||
| 210 | $arrayFinalParameterValues = []; |
||
| 211 | $strFilter = ''; |
||
| 212 | switch($strType) { |
||
| 213 | case 'ListAll': |
||
| 214 | $strContentBranch = 'ListAllMessages'; |
||
| 215 | $arrayFinalParameterValues[] = $arrayParameters['Days']; |
||
| 216 | $arrayFinalParameterValues[] = $this->arraySolutionCustomSettings['IntegerTaxIdentificationNumber']; |
||
| 217 | $arrayFinalParameterValues[] = $this->getMessageFilterCode($strFilter); |
||
| 218 | break; |
||
| 219 | case 'ListSinglePage': |
||
| 220 | $strContentBranch = 'ListMessagesPaged'; |
||
| 221 | $arrayFinalParameterValues[] = $arrayParameters['StartTime']; |
||
| 222 | $arrayFinalParameterValues[] = $arrayParameters['EndTime']; |
||
| 223 | $arrayFinalParameterValues[] = $arrayParameters['Page']; |
||
| 224 | $arrayFinalParameterValues[] = $this->getMessageFilterCode($strFilter); |
||
| 225 | break; |
||
| 226 | case 'Single': |
||
| 227 | $strContentBranch = 'SingleMessageStatus'; |
||
| 228 | $arrayFinalParameterValues[] = $arrayParameters['LoadingId']; |
||
| 229 | break; |
||
| 230 | } |
||
| 231 | $strRelevantUrl = $this->arraySettings['Infrastructure']['RO']['Servers']['Content']['OAuth2'] |
||
| 232 | . $this->arraySolutionCustomSettings['StringElectronicInvoiceEnvironment'] |
||
| 233 | . vsprintf($this->arraySettings['Infrastructure']['RO']['Calls']['Content'][$strContentBranch], $arrayFinalParameterValues); |
||
| 234 | error_log(vsprintf('Relevant URL for Message reading of type %s is %s', [ |
||
| 235 | $strType, |
||
| 236 | $strRelevantUrl, |
||
| 237 | ])); |
||
| 238 | // sent XML content using CURL and capture feedback |
||
| 239 | $arrayFeedback = $this->getContentFromUrlThroughCurl($strRelevantUrl, [ |
||
| 240 | 'HttpHeader' => $this->buildHeaderAsArray(), |
||
| 241 | ]); |
||
| 242 | if ($arrayFeedback['errNo'] === 0) { |
||
| 243 | if (json_validate($arrayFeedback['response'])) { |
||
| 244 | $arrayFeedback['response'] = json_decode($arrayFeedback['response'], true, 512, \JSON_OBJECT_AS_ARRAY); |
||
| 245 | $intMessageNo = 0; |
||
| 246 | error_log('Response = ' . json_encode($arrayFeedback['response'])); |
||
| 247 | if (array_key_exists('eroare', $arrayFeedback['response'])) { |
||
| 248 | error_log('Error response = ' . $arrayFeedback['response']['eroare']); |
||
| 249 | } else { |
||
| 250 | foreach ($arrayFeedback['response']['mesaje'] as $arrayMessageDetails) { |
||
| 251 | $strTargetFile = $this->arraySolutionCustomSettings['ArrayFolders']['Downloaded ZIP'] |
||
| 252 | . strval($arrayMessageDetails['id']) . '.zip'; |
||
| 253 | if (!file_exists($strTargetFile)) { |
||
| 254 | $this->getElectronicInvoiceResponseFile($arrayMessageDetails['id'], $strTargetFile); |
||
| 255 | } |
||
| 256 | error_log('Current message is ' . json_encode($arrayMessageDetails)); |
||
| 257 | $intMessageNo++; |
||
| 258 | } |
||
| 259 | error_log(sprintf('%u messages were processed!', strval($intMessageNo))); |
||
| 260 | } |
||
| 261 | } else { |
||
| 262 | error_log('Response is expected to be a JSON but is not...'); |
||
| 263 | error_log(json_encode($arrayFeedback)); |
||
| 264 | } |
||
| 265 | } else { |
||
| 266 | error_log($arrayFeedback['errNo'] . ' => ' . $arrayFeedback['errMsg']); |
||
| 267 | } |
||
| 268 | } |
||
| 269 | |||
| 270 | protected function getElectronicInvoiceResponseFile(int $intResponseId, string $strTargetFile): void |
||
| 293 | ])); |
||
| 294 | } |
||
| 295 | } |
||
| 296 | |||
| 297 | private function getMessageFilterCode(string $strFilter): string |
||
| 298 | { |
||
| 299 | $strReturn = ''; |
||
| 300 | if ($strFilter != '') { |
||
| 301 | $strReturn = '&filtru=' . array_column($this->arraystandardMesageFilters, 'Code', 'Value')[$strFilter]; |
||
| 302 | } |
||
| 303 | return $strReturn; |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * implementing strong validations to avoid issues later in the logic |
||
| 308 | * |
||
| 309 | * @param array $arrayAdditionalValidations |
||
| 310 | */ |
||
| 311 | private function getServerDiscutionValidations(array $arrayAdditionalValidations = []): void |
||
| 312 | { |
||
| 313 | $arrayUniversalValidations = [ |
||
| 314 | 'Zero Value' => $this->arraySolutionCustomSettings['IntegerTaxIdentificationNumber'], |
||
| 315 | 'Empty Environment' => $this->arraySolutionCustomSettings['StringElectronicInvoiceEnvironment'], |
||
| 316 | 'Non-Standard Environment Value' => $this->arraySolutionCustomSettings['StringElectronicInvoiceEnvironment'], |
||
| 317 | ]; |
||
| 318 | $arrayValidations = $arrayUniversalValidations; |
||
| 319 | if ($arrayAdditionalValidations != []) { |
||
| 320 | $arrayValidations = array_merge($arrayValidations, $arrayAdditionalValidations); |
||
| 321 | } |
||
| 322 | $this->checkPrerequisite($arrayValidations); |
||
| 323 | } |
||
| 324 | |||
| 325 | private function getServerMessageParametersValidation(string $strType, array $arrayParameters): void |
||
| 435 | } |
||
| 436 | } |
||
| 437 | |||
| 438 | private function setLabelBasedOnRule(string $strFileName): string |
||
| 439 | { |
||
| 440 | $arrayKnownLabels = ['B2B', 'B2C', 'B2G']; |
||
| 441 | $arrayKnownRules = [ |
||
| 442 | 'File base name end with', |
||
| 443 | 'File base name starts with', |
||
| 444 | ]; |
||
| 445 | $strLabel = ''; |
||
| 446 | foreach ($arrayKnownLabels as $strCurrentLabel) { |
||
| 447 | $arrayPieces = explode('|', $this->arraySolutionCustomSettings['ArrayStrategyForUpload'][$strCurrentLabel]); |
||
| 448 | switch($arrayPieces[0]) { |
||
| 449 | case 'File base name end with': |
||
| 450 | if (str_ends_with($strFileName, $arrayPieces[1])) { |
||
| 451 | $strLabel = $strCurrentLabel; |
||
| 452 | } |
||
| 453 | break; |
||
| 454 | case 'File base name starts with': |
||
| 455 | if (str_starts_with($strFileName, $arrayPieces[1])) { |
||
| 456 | $strLabel = $strCurrentLabel; |
||
| 457 | } |
||
| 458 | break; |
||
| 459 | default: |
||
| 460 | throw \RuntimeException('Unknown/missconfigured rule given' |
||
|
|
|||
| 461 | . ', please ensure only proper rules (' |
||
| 462 | . 'that is one of these values "' . implode('", "', $arrayKnownRules) . '"' |
||
| 463 | . ') are configured in your custom class...'); |
||
| 464 | break; |
||
| 465 | } |
||
| 466 | } |
||
| 467 | return $strLabel; |
||
| 468 | } |
||
| 469 | |||
| 470 | protected function uploadElectronicInvoicesFromFolder(): void |
||
| 513 | } |
||
| 514 | } |
||
| 515 |