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