Quellcode durchsuchen

Added ext-json on composer file and created a TokenFactory to make a request to base module to enable the costumer.

Jean Sumara Leopoldo vor 4 Jahren
Ursprung
Commit
2090dbc136

Datei-Diff unterdrückt, da er zu groß ist
+ 1 - 1
app/DoctrineMigrations/src/action.yml


+ 2 - 1
composer.json

@@ -178,7 +178,8 @@
         "symfony/web-server-bundle": "^3.3",
         "twig/extensions": "^1.5",
         "twig/twig": "^2.0",
-        "voryx/restgeneratorbundle": "dev-master"
+        "voryx/restgeneratorbundle": "dev-master",
+      "ext-json": "*"
     },
     "require-dev": {
         "phpunit/phpunit": "^6.4",

+ 30 - 0
src/FTTHBundle/Command/ONURecoverCommand.php

@@ -2,7 +2,13 @@
 
 namespace FTTHBundle\Command;
 
+use Base\AdminBundle\Checks\CheckParametersAsURL;
+use Buzz\Exception\RequestException;
+use Buzz\Message\Request as HttpRequest;
+use Buzz\Message\RequestInterface as HttpRequestInterface;
+use Buzz\Message\Response as HttpResponse;
 use FTTHBundle\Entity\ONU;
+use FTTHBundle\Factory\TokenFactory;
 use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
 use Symfony\Component\Console\Input\InputArgument;
 use Symfony\Component\Console\Input\InputInterface;
@@ -50,6 +56,8 @@ class ONURecoverCommand extends ContainerAwareCommand
                     $onu->getLogOLT()->setDate(new \DateTime());
                     $onu->setDeletedAt(null);
                     $em->flush();
+
+                    $this->recoverClient($onu->getClientId());
                 }
 
                 //enable filters again
@@ -63,4 +71,26 @@ class ONURecoverCommand extends ContainerAwareCommand
             echo("\nTRACE: \n" . $e->getTraceAsString() . "\n\n");
         }
     }
+
+    private function recoverClient($id){
+        $baseUrl = CheckParametersAsURL::checkParametersUrl($this->getContainer(), 'url_base');
+        if(!is_null($baseUrl)){
+            echo 'Recovering costumer'.PHP_EOL;
+            $ws = $this->getContainer()->get('webservice');
+            $urlRequest = "{$baseUrl}/api/clients/recover/{$id}";
+            $headers = TokenFactory::create($baseUrl, $ws);
+            try {
+                $request = new HttpRequest(HttpRequestInterface::METHOD_POST, $urlRequest);
+                $request->setHeaders($headers);
+                $response = new HttpResponse();
+                $ws->getHttpClient()->setTimeout(20);
+                $ws->getHttpClient()->send($request, $response);
+                $response = $response->getContent();
+            } catch (RequestException $ex) {
+                $response = '';
+                $ws->log($ex->getMessage(), 'error');
+            }
+            echo $response.PHP_EOL;
+        }
+    }
 }

+ 40 - 0
src/FTTHBundle/Factory/TokenFactory.php

@@ -0,0 +1,40 @@
+<?php
+
+
+namespace FTTHBundle\Factory;
+
+use Buzz\Message\RequestInterface as HttpRequestInterface;
+
+class TokenFactory
+{
+    public static function create($baseUrl, $ws){
+        if(!is_null($baseUrl)){
+            $endpointToken = $baseUrl."/oauth/v2/token";
+
+            $params =  [
+                'client_id' => getenv('OAUTH_CLIENT_ID'),
+                'client_secret' => getenv('OAUTH_CLIENT_SECRET'),
+                'grant_type' => 'client_credentials'
+            ];
+
+            try {
+                $requestToken = $ws->makeRequest($endpointToken, HttpRequestInterface::METHOD_POST, $params);
+                $token = json_decode($requestToken, true);
+
+                $headers = [
+                    'Authorization' => "Bearer {$token['access_token']}",
+                    'ClientId' => $params['client_id'],
+                    'ClientSecret' => $params['client_secret'],
+                    'Microservice' => 1,
+                    'Content-Type' => 'application/x-www-form-urlencoded'
+                ];
+
+                return $headers;
+            } catch (\Exception $ex) {
+                throw new \Exception($ex->getMessage());
+            }
+        }
+
+        return null;
+    }
+}