From aeef46fbe2127a03c312a58de26a5eda8437e415 Mon Sep 17 00:00:00 2001
From: bayusamudra5502 <bayusamudra.55.02.com@gmail.com>
Date: Wed, 30 Nov 2022 14:33:52 +0700
Subject: [PATCH] fix: change env and adding endpoint to check soap conn

---
 .env.example                       |   8 ++-
 Controller/ExampleController.php   |  14 ----
 Controller/SubsciberController.php |   4 +-
 Controller/TestController.php      |  17 +++++
 Lib/Service/Api/FetchAPI.php       | 109 ++++++++++++++---------------
 Router/AppRoutes.php               |   3 +-
 Service/Soap/TestSoap.php          |  23 ++++++
 7 files changed, 104 insertions(+), 74 deletions(-)
 delete mode 100644 Controller/ExampleController.php
 create mode 100644 Controller/TestController.php
 create mode 100644 Service/Soap/TestSoap.php

diff --git a/.env.example b/.env.example
index d840b0e..ad73005 100644
--- a/.env.example
+++ b/.env.example
@@ -7,5 +7,9 @@ POSTGRES_USER=postgres
 POSTGRES_PASSWORD=root
 
 API_KEY=secret
-REST_HOST=http://webtune-soap:8080/webtune-soap
-REST_KEY=apahayo
\ No newline at end of file
+
+REST_API="http://webtune-rest:8888"
+REST_KEY=secret_turu
+
+SOAP_API=http://webtune-soap:8080/webtune-soap
+SOAP_KEY=secret_blebekblebek
diff --git a/Controller/ExampleController.php b/Controller/ExampleController.php
deleted file mode 100644
index 3815859..0000000
--- a/Controller/ExampleController.php
+++ /dev/null
@@ -1,14 +0,0 @@
-<?php
-
-namespace Controller;
-
-use Lib\Interfaces\Routing\IRequest;
-use Lib\Interfaces\Routing\IResponse;
-
-class ExampleController {
-  static function HalamanX(IRequest $req, IResponse $res) {
-    return $res->view("Pages/HalamanX", [
-      "kucing" => 12,
-    ]);
-  }
-}
diff --git a/Controller/SubsciberController.php b/Controller/SubsciberController.php
index 98abb2a..8cb55cb 100644
--- a/Controller/SubsciberController.php
+++ b/Controller/SubsciberController.php
@@ -143,7 +143,7 @@ class SubsciberController {
   }
 
   static function fetchSubscriptions(int $page,int $limit, int $user_id,$db){
-    $fetcher = new FetchAPI(getenv("REST_HOST"));
+    $fetcher = new FetchAPI(getenv("REST_API"));
     $param = [
       "page"=>$page,
       "limit"=>$limit,
@@ -217,7 +217,7 @@ class SubsciberController {
 
  //   $data = json_decode($output,true)["data"];
     //fetch max page
-    $fetcher = new FetchAPI(getenv("REST_HOST"));
+    $fetcher = new FetchAPI(getenv("REST_API"));
     $param = [
       "page-size"=>$limit,
     ];
diff --git a/Controller/TestController.php b/Controller/TestController.php
new file mode 100644
index 0000000..544075c
--- /dev/null
+++ b/Controller/TestController.php
@@ -0,0 +1,17 @@
+<?php
+
+namespace Controller;
+
+use Lib\Interfaces\Routing\IRequest;
+use Lib\Interfaces\Routing\IResponse;
+use Service\Soap\TestSoap;
+
+class TestController {
+  static function soapPing(IRequest $req, IResponse $res) {
+    $test = new TestSoap();
+
+    return $res->json([
+      "result" => $test->ping(),
+    ]);
+  }
+}
diff --git a/Lib/Service/Api/FetchAPI.php b/Lib/Service/Api/FetchAPI.php
index fedf7ed..33cd4e1 100644
--- a/Lib/Service/Api/FetchAPI.php
+++ b/Lib/Service/Api/FetchAPI.php
@@ -1,64 +1,63 @@
 <?php
-    namespace Lib\Service\Api;
 
-use PDO;
+namespace Lib\Service\Api;
 
-    class FetchAPI{
-        private string $host;
+class FetchAPI{
+    private string $host;
 
-        public function __construct(string $host)
-        {
-            $this->host = $host;
-        }
-
-        public function get(string $endpoint,array $param, array $headers){
-            $url = $this->host . $endpoint;
-            //add param
-            if(count($param)>0){
-                $url .= "?";
-            }
-            foreach($param as $key=>$value){
-                $url = $url . $key . "=" . $value . "&";
-            }
-            $ch = curl_init($url);
-            //set curl header
-            curl_setopt($ch, CURLOPT_RETURNTRANSFER,  1);
-            curl_setopt( $ch , CURLOPT_RETURNTRANSFER, true);
-            curl_setopt( $ch , CURLOPT_SSL_VERIFYPEER, false);
-            curl_setopt( $ch , CURLOPT_SSL_VERIFYHOST, false);
-            curl_setopt( $ch , CURLOPT_TIMEOUT, 10000);
-            //set http header
-            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
-        
-            $output = curl_exec($ch);
-            curl_close($ch);
+    public function __construct(string $host)
+    {
+        $this->host = $host;
+    }
 
-            return $output;
+    public function get(string $endpoint,array $param, array $headers){
+        $url = $this->host . $endpoint;
+        //add param
+        if(count($param)>0){
+            $url .= "?";
         }
+        foreach($param as $key=>$value){
+            $url = $url . $key . "=" . $value . "&";
+        }
+        $ch = curl_init($url);
+        //set curl header
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER,  1);
+        curl_setopt( $ch , CURLOPT_RETURNTRANSFER, true);
+        curl_setopt( $ch , CURLOPT_SSL_VERIFYPEER, false);
+        curl_setopt( $ch , CURLOPT_SSL_VERIFYHOST, false);
+        curl_setopt( $ch , CURLOPT_TIMEOUT, 10000);
+        //set http header
+        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
+    
+        $output = curl_exec($ch);
+        curl_close($ch);
 
-        public function post(string $endpoint,array $param, array $headers){
-            $url = $this->host . $endpoint;
-            //add param
-            $ch = curl_init($url);
-            //set curl header
-            curl_setopt($ch, CURLOPT_RETURNTRANSFER,  1);
-            curl_setopt( $ch , CURLOPT_RETURNTRANSFER, true);
-            curl_setopt( $ch , CURLOPT_SSL_VERIFYPEER, false);
-            curl_setopt( $ch , CURLOPT_SSL_VERIFYHOST, false);
-            curl_setopt( $ch , CURLOPT_TIMEOUT, 10000);
-            //set post payload
-            curl_setopt($ch, CURLOPT_POST, 1);
-            $post_payload = "";
-            foreach($param as $key=>$value){
-                $post_payload = $post_payload . $key . "=" . $value . "&";
-            }
-            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_payload);
-            //set http header
-            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
-        
-            $output = curl_exec($ch);
-            curl_close($ch);
+        return $output;
+    }
 
-            return $output;
+    public function post(string $endpoint,array $param, array $headers){
+        $url = $this->host . $endpoint;
+        //add param
+        $ch = curl_init($url);
+        //set curl header
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER,  1);
+        curl_setopt( $ch , CURLOPT_RETURNTRANSFER, true);
+        curl_setopt( $ch , CURLOPT_SSL_VERIFYPEER, false);
+        curl_setopt( $ch , CURLOPT_SSL_VERIFYHOST, false);
+        curl_setopt( $ch , CURLOPT_TIMEOUT, 10000);
+        //set post payload
+        curl_setopt($ch, CURLOPT_POST, 1);
+        $post_payload = "";
+        foreach($param as $key=>$value){
+            $post_payload = $post_payload . $key . "=" . $value . "&";
         }
-    }
\ No newline at end of file
+        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_payload);
+        //set http header
+        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
+    
+        $output = curl_exec($ch);
+        curl_close($ch);
+
+        return $output;
+    }
+}
\ No newline at end of file
diff --git a/Router/AppRoutes.php b/Router/AppRoutes.php
index bda1908..e99ece0 100644
--- a/Router/AppRoutes.php
+++ b/Router/AppRoutes.php
@@ -7,6 +7,7 @@ use Controller\AlbumController;
 use Controller\HomeController;
 use Controller\LaguController;
 use Controller\SubsciberController;
+use Controller\TestController;
 use Lib\Dependency;
 use Lib\Enums\Request\HttpMethod;
 use Lib\Interfaces\Routing\IRoute;
@@ -25,6 +26,7 @@ class AppRoutes
     $route = $this->route;
 
     $route->route(HttpMethod::Get, "/", [HomeController::class, "homepage"]);
+    $route->route(HttpMethod::Get, "/soap/ping", [TestController::class, "soapPing"]);
 
     //halaman lihat lagu
     $route->route(HttpMethod::Get, "/search", [LaguController::class, "searchLagu"]);
@@ -38,7 +40,6 @@ class AppRoutes
 
     // Auth
     $route->groupRoute("/auth", AuthRoutes::getRoutes());
-    $route->route(HttpMethod::Get, "/testing", [ExampleController::class, "HalamanX"]);
 
     // Album
     $route->route(HttpMethod::Get, "/album", [AlbumController::class, "getAlbums"]);
diff --git a/Service/Soap/TestSoap.php b/Service/Soap/TestSoap.php
new file mode 100644
index 0000000..8af3bc1
--- /dev/null
+++ b/Service/Soap/TestSoap.php
@@ -0,0 +1,23 @@
+<?php
+
+namespace Service\Soap;
+
+class TestSoap {
+  private SoapClientFactory $factory;
+
+  public function __construct() {
+    $wsdl = getenv("SOAP_API"). "/services/test?wsdl";
+    $this->factory = new SoapClientFactory($wsdl);
+  }
+
+  public function ping(
+    ?string $last_token = null
+  ): string {
+    $soap = $this->factory->generate_client($last_token);
+
+    return $soap->__soapCall(
+      "ping",
+      []
+    );
+  }
+}
\ No newline at end of file
-- 
GitLab