davide.cucurnia@vola.it
2024-02-07 59b0688842f1808c1a51f277d8605b3ec4a710de
commit | author | age
9f6455 1 <?php
DC 2
3 namespace App\Vola\Services\VolaFakeHTTPResponder;
4
5 use App\Vola\Classes\Utils;
6 use Cookie;
7 use GuzzleHttp\Promise\RejectedPromise;
8 use GuzzleHttp\Exception\ConnectException;
9 use Illuminate\Http\Request;
10 use Illuminate\Support\Arr;
11 use Illuminate\Support\Str;
12
13 class VolaFakeHTTPResponder
14 {
15     public array $data;
16     public array $headers;
17     private array $sso_XML;
18
19     public function __construct()
20     {
21         $this->sso_XML = config('devtools.fake_sso_profiles');
22     }
23
59b068 24     public function getModelBasedResponses($request, $testUser, $picasso = false): string|array
D 25     {
26         return 'test. funziono ma le logiche non sono state ancora implementate';
27     }
28
29     public function getResponses(Request $request, $testUser, $picasso = false): string|array
9f6455 30     {
DC 31             $uriParts = explode("?", $request->url());
32             $methodUri = Str::afterLast($uriParts[0], "/");
33             $reqParams = !empty($request->query()) ? $request->query() : [];
34             if (($methodUri == 'isLogged') || ($methodUri == 'islogged')) {
35                 // controllo cookie e conseguente risposta isLogged
36                 $loginResponse = ($picasso) ? $this->getLoggedPicasso($testUser) : $this->getLoggedLegacy($testUser);
37                 return $loginResponse;
38
39             } else if ($methodUri != "") {
40                 // controllo metodo e parametri e conseguente risposta
398fc7 41                 $userResponses = $this->getUserResponseSet($testUser, $picasso);
9f6455 42                 if (isset($userResponses[$methodUri])) {
DC 43                     $responseContent = $this->getResponseArgumentsBased($userResponses, $methodUri, $reqParams);
44                     return $responseContent;
398fc7 45                 } else {
D 46                     return 'user is not set for this method';
9f6455 47                 }
DC 48             } else {
398fc7 49                 return 'wrong-request-no-such-method';
9f6455 50             }
DC 51     }
52
59b068 53     public function getLoggedPicasso($testUser): string|array
9f6455 54     {
DC 55         if (isset($_COOKIE["SSOSESSIONID"])) {
398fc7 56             if (!isset($this->sso_XML[$testUser]["picasso"])) {
D 57                 return 'this picasso user doesnt exist. check your SSOSESSIONID value';
58             }
a4fbcb 59             $sampleResponseString = Arr::first($this->sso_XML[$testUser]["picasso"]["getWebcustomerInformation"]["parametri"]["k"] );
9f6455 60             $sampleResponseObject = Utils::convertXMLStrToArray($sampleResponseString);
DC 61             $resp = [
62                 'logged' => (string) 1,
63                 'errorCode' => (string) 0,
64                 'username' => (string) $sampleResponseObject["Username"],
65                 'next_user' => (string) 'N',
66                 //'token' => '415F2B31F1C15FA45C9A6E1CBEB0ADF3'
67             ];
68         } else {
69             $resp = [
70                 'logged' => (string) 0,
71                 'errorCode' => (string) 0,
72             ];
73         }
74         $xml = Utils::convertToXML($resp, $root = '<isLogged/>');
75         return $xml;
76     }
77
59b068 78     public function getLoggedLegacy($testUser): string|array
9f6455 79     {
DC 80         if (isset($_COOKIE["CAuthCookie"])) {
398fc7 81             if (!isset($this->sso_XML[$testUser]["sso"])) {
D 82                 return 'this user doesnt exist. check your CAuthCookie value';
83             }
a4fbcb 84             $sampleResponseString = Arr::first($this->sso_XML[$testUser]["sso"]["getWebcustomerInformation"]["parametri"]["k"] );
9f6455 85             $sampleResponseObject = Utils::convertXMLStrToArray($sampleResponseString);
DC 86             $resp = [
87                 'logged' => (string) 1,
88                 'errorCode' => (string) 0,
89                 'username' => (string) $sampleResponseObject["Username"],
90                 'token' => (string) '415F2B31F1C15FA45C9A6E1CBEB0ADF3',
91                 'next_user' => (string) 'N',
92             ];
93         } else {
94             $resp = [
95                 'logged' => (string) 0,
96                 'errorCode' => (string) 0,
97             ];
98         }
99         $xml = Utils::convertToXML($resp, $root = '<isLogged/>');
100         return $xml;
101     }
102
59b068 103     public function getResponseArgumentsBased($userMethods, $methodUri, $reqParams = []): string
9f6455 104     {
DC 105         $method = $userMethods[$methodUri];
106         $params = $method["parametri"];
107
108         //assumo che se la richiesta sia arrivata fin qui abbia tutti i mandatory nel url
109         foreach ($reqParams as $key => $value) {
110             //verifico se il metodo dell' utente ha la chiave che sto cercando
a4fbcb 111             if ($key != "k" && isset($params[$key])) {
9f6455 112                 if (array_key_exists($value, $params[$key])) {
398fc7 113                     \Log::channel('requests')->debug("[VolaFakeHTTPResponder] Inserisco xml specifico per key:$key ,params: " . print_r($params, 1) . " key: $key, value: $value, xml: " . print_r($params[$key][$value], 1));
9f6455 114                     return $params[$key][$value];
DC 115                 }
116             }
117         }
118         //xml di default (se richiede parametri di default conterrĂ  errori)
a4fbcb 119         $defaultResponse = Arr::first($params["k"]);
398fc7 120         \Log::channel('requests')->debug("[VolaFakeHTTPResponder] Inserisco xml di default $defaultResponse per method: $methodUri");
9f6455 121         return $defaultResponse;
DC 122     }
123
124     /**
125      * @param Request $request
126      * @param mixed $testUser
398fc7 127      * @param bool $picasso
9f6455 128      * @return array|mixed
DC 129      */
59b068 130     private function getUserResponseSet(mixed $testUser, bool $picasso): array
9f6455 131     {
398fc7 132         if ($picasso) {
9f6455 133             $userMethods = $this->sso_XML[$testUser]["picasso"] ?? [];
DC 134         } else {
135             $userMethods = $this->sso_XML[$testUser]["sso"] ?? [];
136         }
137         return $userMethods;
138     }
139
140 }