From 56f8c80f9a610c80810b24718340bc9cfd14df07 Mon Sep 17 00:00:00 2001
From: Filippo Bertilotti <filippobertilotti@gmail.com>
Date: Mon, 02 Sep 2024 10:14:35 +0200
Subject: [PATCH] creazione view nuova per inserimento database e refactor

---
 app/Vola/Services/VolaFakeHTTPResponder/VolaFakeHTTPResponder.php |   85 +++++++++++++++++++++++++++++++-----------
 1 files changed, 62 insertions(+), 23 deletions(-)

diff --git a/app/Vola/Services/VolaFakeHTTPResponder/VolaFakeHTTPResponder.php b/app/Vola/Services/VolaFakeHTTPResponder/VolaFakeHTTPResponder.php
index f2f68c2..a0cd65e 100644
--- a/app/Vola/Services/VolaFakeHTTPResponder/VolaFakeHTTPResponder.php
+++ b/app/Vola/Services/VolaFakeHTTPResponder/VolaFakeHTTPResponder.php
@@ -13,52 +13,73 @@
 class VolaFakeHTTPResponder
 {
     public array $data;
-    public array $headers;
-    private array $sso_XML;
+    private array $xmlResponses;
 
     public function __construct()
     {
-        $this->sso_XML = config('devtools.fake_sso_profiles');
+        $this->xmlResponses = config('devtools.fake_sso_profiles');
     }
 
-    public function getResponses(Request $request, $testUser, $picasso = false)
+    public function getModelBasedResponses(Request $request): array
+    {
+        $picassoRequest = (str_starts_with($request->getRequestUri(), '/picasso/',));
+        $profile = Utils::getRequestedUser($request);
+
+        if (isset($profile)) {
+
+            $responseContent["data"] = 'test. funziono ma le logiche non sono state ancora implementate';
+            $responseContent["status"] = 200;
+            \RequestLogger::logProcessedRequest($request, $responseContent, (($picassoRequest) ? 'Picasso' : 'SSO Legacy') );
+            return $responseContent;
+
+        } else {
+            \RequestLogger::logRejectedRequest($request, $request->cookies);
+            return [
+                "status" => 400,
+                "data" => $this->getErrorMessage('no-cookie')
+            ];
+        }
+
+    }
+
+    public function getResponses(Request $request, $selectedUser, $picasso = false): string|array
     {
             $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);
+                $loginResponse = ($picasso) ? $this->getLoggedPicasso($selectedUser) : $this->getLoggedLegacy($selectedUser);
                 return $loginResponse;
 
             } else if ($methodUri != "") {
                 // controllo metodo e parametri e conseguente risposta
-                $userResponses = $this->getUserResponseSet($testUser, $picasso);
+                $userResponses = $this->getUserResponseGroup($selectedUser, $picasso);
                 if (isset($userResponses[$methodUri])) {
                     $responseContent = $this->getResponseArgumentsBased($userResponses, $methodUri, $reqParams);
                     return $responseContent;
                 } else {
-                    return 'user is not set for this method';
+                    return $methodUri . " " . $selectedUser . " " . $this->getErrorMessage('no-method-on-user');
                 }
             } else {
-                return 'wrong-request-no-such-method';
+                return $this->getErrorMessage('no-method');
             }
     }
 
-    public function getLoggedPicasso($testUser)
+    public function getLoggedPicasso($selectedUser): string|array
     {
         if (isset($_COOKIE["SSOSESSIONID"])) {
-            if (!isset($this->sso_XML[$testUser]["picasso"])) {
-                return 'this picasso user doesnt exist. check your SSOSESSIONID value';
+            if (!isset($this->xmlResponses[$selectedUser]["picasso"])) {
+                return $this->getErrorMessage('no-picasso-user');
             }
-            $sampleResponseString = Arr::first($this->sso_XML[$testUser]["picasso"]["getWebcustomerInformation"]["parametri"]["k"] );
+            $sampleResponseString = Arr::first($this->xmlResponses[$selectedUser]["picasso"]["getWebcustomerInformation"]["parametri"]["k"] );
             $sampleResponseObject = Utils::convertXMLStrToArray($sampleResponseString);
             $resp = [
                 'logged' => (string) 1,
                 'errorCode' => (string) 0,
                 'username' => (string) $sampleResponseObject["Username"],
                 'next_user' => (string) 'N',
-                //'token' => '415F2B31F1C15FA45C9A6E1CBEB0ADF3'
+                'token' => (string) 'xno:' . $selectedUser,
             ];
         } else {
             $resp = [
@@ -70,19 +91,19 @@
         return $xml;
     }
 
-    public function getLoggedLegacy($testUser)
+    public function getLoggedLegacy($selectedUser): string|array
     {
         if (isset($_COOKIE["CAuthCookie"])) {
-            if (!isset($this->sso_XML[$testUser]["sso"])) {
-                return 'this user doesnt exist. check your CAuthCookie value';
+            if (!isset($this->xmlResponses[$selectedUser]["sso"])) {
+                return $this->getErrorMessage('no-sso-user');
             }
-            $sampleResponseString = Arr::first($this->sso_XML[$testUser]["sso"]["getWebcustomerInformation"]["parametri"]["k"] );
+            $sampleResponseString = Arr::first($this->xmlResponses[$selectedUser]["sso"]["getWebcustomerInformation"]["parametri"]["k"] );
             $sampleResponseObject = Utils::convertXMLStrToArray($sampleResponseString);
             $resp = [
                 'logged' => (string) 1,
                 'errorCode' => (string) 0,
                 'username' => (string) $sampleResponseObject["Username"],
-                'token' => (string) '415F2B31F1C15FA45C9A6E1CBEB0ADF3',
+                'token' => (string) 'xno:' . $selectedUser,
                 'next_user' => (string) 'N',
             ];
         } else {
@@ -95,7 +116,7 @@
         return $xml;
     }
 
-    public function getResponseArgumentsBased($userMethods, $methodUri, $reqParams = [])
+    public function getResponseArgumentsBased($userMethods, $methodUri, $reqParams = []): string
     {
         $method = $userMethods[$methodUri];
         $params = $method["parametri"];
@@ -118,18 +139,36 @@
 
     /**
      * @param Request $request
-     * @param mixed $testUser
+     * @param mixed $selectedUser
      * @param bool $picasso
      * @return array|mixed
      */
-    private function getUserResponseSet(mixed $testUser, bool $picasso)
+    private function getUserResponseGroup(mixed $selectedUser, bool $picasso): array
     {
         if ($picasso) {
-            $userMethods = $this->sso_XML[$testUser]["picasso"] ?? [];
+            $userMethods = $this->xmlResponses[$selectedUser]["picasso"] ?? [];
         } else {
-            $userMethods = $this->sso_XML[$testUser]["sso"] ?? [];
+            $userMethods = $this->xmlResponses[$selectedUser]["sso"] ?? [];
         }
         return $userMethods;
     }
 
+    private function getErrorMessage(string $code): string
+    {
+        switch ($code) {
+            case 'no-cookie':
+                return 'No cookie No Party. Send your CAuthCookie or SESSIONID cookie';
+            case 'no-sso-user':
+                return 'This sso user doesnt exist. Check your CAuthCookie cookie value';
+            case 'no-picasso-user':
+                return 'This picasso user doesnt exist. Check your SSOSESSIONID coookie value';
+            case 'no-method-on-user':
+                return 'This method response is not set for this user. Check this server database.';
+            case 'no-method':
+                return 'Couldnt detect requested method. Check your request url.';
+            default:
+                return 'Couldnt detect proper response.';
+        }
+    }
+
 }

--
Gitblit v1.8.0