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