Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
73.33% |
22 / 30 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ProcessContextExtractor | |
73.33% |
22 / 30 |
|
0.00% |
0 / 4 |
27.59 | |
0.00% |
0 / 1 |
| extractProcessContext | |
76.92% |
10 / 13 |
|
0.00% |
0 / 1 |
8.79 | |||
| buildProcessContextFromArray | |
80.00% |
8 / 10 |
|
0.00% |
0 / 1 |
4.13 | |||
| addIntFieldIfPresent | |
50.00% |
1 / 2 |
|
0.00% |
0 / 1 |
4.12 | |||
| extractScopeId | |
60.00% |
3 / 5 |
|
0.00% |
0 / 1 |
6.60 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace BO\Zmscitizenapi\Services\Core; |
| 6 | |
| 7 | use Psr\Http\Message\ServerRequestInterface; |
| 8 | |
| 9 | class ProcessContextExtractor |
| 10 | { |
| 11 | public static function extractProcessContext(ServerRequestInterface $request, ?string $responseBody): array |
| 12 | { |
| 13 | $process = []; |
| 14 | |
| 15 | // 1) Start with what the client sent (request body) |
| 16 | $bodyData = $request->getParsedBody(); |
| 17 | if (is_array($bodyData)) { |
| 18 | $process = self::buildProcessContextFromArray($bodyData); |
| 19 | } |
| 20 | |
| 21 | // 2) Overwrite with what the API returns (response JSON), if any |
| 22 | if ($responseBody !== null && $responseBody !== '') { |
| 23 | $decoded = json_decode($responseBody, true); |
| 24 | if (json_last_error() === JSON_ERROR_NONE && is_array($decoded)) { |
| 25 | $fromResponse = self::buildProcessContextFromArray($decoded); |
| 26 | if (!empty($fromResponse)) { |
| 27 | $process = array_replace($process, $fromResponse); |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | if (empty($process)) { |
| 33 | return []; |
| 34 | } |
| 35 | |
| 36 | return ['process' => $process]; |
| 37 | } |
| 38 | |
| 39 | private static function buildProcessContextFromArray(array $data): array |
| 40 | { |
| 41 | $process = []; |
| 42 | |
| 43 | self::addIntFieldIfPresent($process, 'processId', $data, 'processId'); |
| 44 | self::addIntFieldIfPresent($process, 'officeId', $data, 'officeId'); |
| 45 | |
| 46 | $scopeId = self::extractScopeId($data); |
| 47 | if ($scopeId !== null) { |
| 48 | $process['scopeId'] = $scopeId; |
| 49 | } |
| 50 | |
| 51 | self::addIntFieldIfPresent($process, 'serviceId', $data, 'serviceId'); |
| 52 | |
| 53 | if (isset($data['displayNumber']) && is_string($data['displayNumber'])) { |
| 54 | $process['displayNumber'] = $data['displayNumber']; |
| 55 | } |
| 56 | |
| 57 | return $process; |
| 58 | } |
| 59 | |
| 60 | private static function addIntFieldIfPresent(array &$process, string $targetKey, array $source, string $sourceKey): void |
| 61 | { |
| 62 | if (isset($source[$sourceKey]) && is_numeric($source[$sourceKey])) { |
| 63 | $process[$targetKey] = (int)$source[$sourceKey]; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | private static function extractScopeId(array $data): ?int |
| 68 | { |
| 69 | if (isset($data['scope']['id']) && is_numeric($data['scope']['id'])) { |
| 70 | return (int)$data['scope']['id']; |
| 71 | } |
| 72 | |
| 73 | if (isset($data['scopeId']) && is_numeric($data['scopeId'])) { |
| 74 | return (int)$data['scopeId']; |
| 75 | } |
| 76 | |
| 77 | return null; |
| 78 | } |
| 79 | } |