davide.cucurnia@vola.it
2024-02-05 ca6434daa9ea96df47c3197391a75a06466ebfc0
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
24     public function getResponses(Request $request, $testUser, $picasso = false)
25     {
26             $uriParts = explode("?", $request->url());
27             $methodUri = Str::afterLast($uriParts[0], "/");
28             $reqParams = !empty($request->query()) ? $request->query() : [];
29             if (($methodUri == 'isLogged') || ($methodUri == 'islogged')) {
30                 // controllo cookie e conseguente risposta isLogged
31                 $loginResponse = ($picasso) ? $this->getLoggedPicasso($testUser) : $this->getLoggedLegacy($testUser);
32                 return $loginResponse;
33
34             } else if ($methodUri != "") {
35                 // controllo metodo e parametri e conseguente risposta
398fc7 36                 $userResponses = $this->getUserResponseSet($testUser, $picasso);
9f6455 37                 if (isset($userResponses[$methodUri])) {
DC 38                     $responseContent = $this->getResponseArgumentsBased($userResponses, $methodUri, $reqParams);
39                     return $responseContent;
398fc7 40                 } else {
D 41                     return 'user is not set for this method';
9f6455 42                 }
DC 43             } else {
398fc7 44                 return 'wrong-request-no-such-method';
9f6455 45             }
DC 46     }
47
48     public function getLoggedPicasso($testUser)
49     {
50         if (isset($_COOKIE["SSOSESSIONID"])) {
398fc7 51             if (!isset($this->sso_XML[$testUser]["picasso"])) {
D 52                 return 'this picasso user doesnt exist. check your SSOSESSIONID value';
53             }
a4fbcb 54             $sampleResponseString = Arr::first($this->sso_XML[$testUser]["picasso"]["getWebcustomerInformation"]["parametri"]["k"] );
9f6455 55             $sampleResponseObject = Utils::convertXMLStrToArray($sampleResponseString);
DC 56             $resp = [
57                 'logged' => (string) 1,
58                 'errorCode' => (string) 0,
59                 'username' => (string) $sampleResponseObject["Username"],
60                 'next_user' => (string) 'N',
61                 //'token' => '415F2B31F1C15FA45C9A6E1CBEB0ADF3'
62             ];
63         } else {
64             $resp = [
65                 'logged' => (string) 0,
66                 'errorCode' => (string) 0,
67             ];
68         }
69         $xml = Utils::convertToXML($resp, $root = '<isLogged/>');
70         return $xml;
71     }
72
73     public function getLoggedLegacy($testUser)
74     {
75         if (isset($_COOKIE["CAuthCookie"])) {
398fc7 76             if (!isset($this->sso_XML[$testUser]["sso"])) {
D 77                 return 'this user doesnt exist. check your CAuthCookie value';
78             }
a4fbcb 79             $sampleResponseString = Arr::first($this->sso_XML[$testUser]["sso"]["getWebcustomerInformation"]["parametri"]["k"] );
9f6455 80             $sampleResponseObject = Utils::convertXMLStrToArray($sampleResponseString);
DC 81             $resp = [
82                 'logged' => (string) 1,
83                 'errorCode' => (string) 0,
84                 'username' => (string) $sampleResponseObject["Username"],
85                 'token' => (string) '415F2B31F1C15FA45C9A6E1CBEB0ADF3',
86                 'next_user' => (string) 'N',
87             ];
88         } else {
89             $resp = [
90                 'logged' => (string) 0,
91                 'errorCode' => (string) 0,
92             ];
93         }
94         $xml = Utils::convertToXML($resp, $root = '<isLogged/>');
95         return $xml;
96     }
97
98     public function getResponseArgumentsBased($userMethods, $methodUri, $reqParams = [])
99     {
100         $method = $userMethods[$methodUri];
101         $params = $method["parametri"];
102
103         //assumo che se la richiesta sia arrivata fin qui abbia tutti i mandatory nel url
104         foreach ($reqParams as $key => $value) {
105             //verifico se il metodo dell' utente ha la chiave che sto cercando
a4fbcb 106             if ($key != "k" && isset($params[$key])) {
9f6455 107                 if (array_key_exists($value, $params[$key])) {
398fc7 108                     \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 109                     return $params[$key][$value];
DC 110                 }
111             }
112         }
113         //xml di default (se richiede parametri di default conterrĂ  errori)
a4fbcb 114         $defaultResponse = Arr::first($params["k"]);
398fc7 115         \Log::channel('requests')->debug("[VolaFakeHTTPResponder] Inserisco xml di default $defaultResponse per method: $methodUri");
9f6455 116         return $defaultResponse;
DC 117     }
118
119     /**
120      * @param Request $request
121      * @param mixed $testUser
398fc7 122      * @param bool $picasso
9f6455 123      * @return array|mixed
DC 124      */
398fc7 125     private function getUserResponseSet(mixed $testUser, bool $picasso)
9f6455 126     {
398fc7 127         if ($picasso) {
9f6455 128             $userMethods = $this->sso_XML[$testUser]["picasso"] ?? [];
DC 129         } else {
130             $userMethods = $this->sso_XML[$testUser]["sso"] ?? [];
131         }
132         return $userMethods;
133     }
134
135 }