Browse Source

Created notification service, update, and enum

Jean Sumara 5 years ago
parent
commit
9b9a730644

+ 4 - 1
src/App/Listener/ExportListener.php

@@ -4,6 +4,7 @@
 namespace Flowdat\Export\App\Listener;
 
 
+use Flowdat\Export\App\Enum\NotificationEnum;
 use Flowdat\Export\App\Factory\TokenFactory;
 use Flowdat\Export\App\Service\NotificationService;
 use Flowdat\Export\App\Service\OnuService;
@@ -49,8 +50,10 @@ class ExportListener
 
             $headers = TokenFactory::create($msgBody);
 
+            $notificationId = $msgBody['notificationId'];
+
             $notificationService = new NotificationService();
-            $notificationId = $notificationService->create($msgBody['endpointBase'], $headers, 'Processing ONUs to export', $msgBody['className'], $msgBody['tenancyId']);
+            $notificationService->update($msgBody['endpointBase'], $notificationId, ['message' => 'Processing ONUs to export' ,'status' => NotificationEnum::PROCESSING], $headers);
 
             $onuService = new OnuService();
             $onuService->exportOnus($msgBody, $headers, $notificationId);

+ 8 - 20
src/App/Service/NotificationService.php

@@ -8,34 +8,22 @@ class NotificationService
 
     /**
      * @param $endpoint
+     * @param $idNotification
+     * @param $body
      * @param $headers
-     * @param $message
-     * @param $hasFile
-     * @param $entity
-     * @param $tenancyId
      * @throws \Exception
      */
-    public function create($endpoint, $headers, $message, $entity, $tenancyId){
+    public function update($endpoint, $idNotification, $body, $headers){
         try{
-            $body = [
-                'message' => $message,
-                'entity' => $entity,
-                'status' => 1,
-                'tenancy' => $tenancyId,
-                'file' => ''
-            ];
 
             $requestService = new RequestService();
-            $response = $requestService->post($endpoint.'/api/notifications', $body, $headers);
-            if($response['statusCode'] != 201){
-                echo "Can't create a notification in Base \n";
+            $endpointComplete = "{$endpoint}/api/notifications/$idNotification";
+            $response = $requestService->put($endpointComplete, $body, $headers);
+            if($response['statusCode'] != 200){
+                echo "Can't update a notification in Base \n";
                 return;
             }
-            $notification = json_decode($response['response'], true);
-            $idNotification = $notification['id'];
-            echo "Created notification with ID: {$idNotification}".PHP_EOL;
-
-            return $idNotification;
+            echo "Updated notification with ID: {$idNotification}".PHP_EOL;
         }catch (\Exception $ex){
             throw new \Exception($ex->getMessage());
         }

+ 26 - 11
src/App/Service/OnuService.php

@@ -3,21 +3,24 @@
 
 namespace Flowdat\Export\App\Service;
 
-use Flowdat\Export\App\Factory\S3Factory;
-use Flowdat\Export\App\Factory\ContentTypeFactory;
+use Flowdat\Export\App\Enum\NotificationEnum;
 use Flowdat\Export\App\Factory\ExportFactory;
+use Flowdat\Export\App\Factory\S3Factory;
 
 class OnuService
 {
 
     private $requestService;
 
+    private $notificationService;
+
     /**
      * OnuService constructor.
      */
     public function __construct()
     {
         $this->requestService = new RequestService();
+        $this->notificationService = new NotificationService();
     }
 
     public function exportOnus($data, $headers, $notificationId){
@@ -79,12 +82,17 @@ class OnuService
                 $filePathS3 = S3Factory::send($filePath, $fileName, $nomeClienteBucket);
             }
 
-            $this->updateNotification($endpointBase, $headers, $notificationId, $filePathS3);
-
             unlink($filePath);
+
+            $this->updateNotification($endpointBase, $headers, $notificationId, $filePathS3);
             return true;
         } catch (\Exception $ex){
             echo $ex->getMessage();
+
+            $this->notificationService->update($endpointBase, $notificationId, [
+                'message' => $ex->getMessage(),
+                'status' => NotificationEnum::ERROR
+            ], $headers);
         }
 
         return true;
@@ -187,12 +195,19 @@ class OnuService
 
     public function updateNotification($endpointBase, $headers, $notificationId, $filePathS3){
         echo "Updating notification with ID ".$notificationId.PHP_EOL;
-        $this->requestService->put($endpointBase.'/api/notifications/'.$notificationId, [
-            'message' => 'File finished process',
-            'hasFile' => true,
-            'file' => $filePathS3,
-            'status' => 3
-        ], $headers);
-        echo "Finished update".PHP_EOL;
+        try {
+            $this->notificationService->update($endpointBase, $notificationId, [
+                'message' => 'File finished process',
+                'hasFile' => true,
+                'file' => $filePathS3,
+                'status' => NotificationEnum::FINISHED
+            ], $headers);
+            echo "Finished update".PHP_EOL;
+        } catch (\Exception $e) {
+            $this->notificationService->update($endpointBase, $notificationId, [
+                'message' => $e->getMessage(),
+                'status' => NotificationEnum::ERROR
+            ], $headers);
+        }
     }
 }

+ 12 - 0
src/Flowdat/Export/App/Enum/NotificationEnum.php

@@ -0,0 +1,12 @@
+<?php
+
+
+namespace Flowdat\Export\App\Enum;
+
+
+class NotificationEnum
+{
+    CONST PROCESSING = 2;
+    CONST FINISHED = 3;
+    CONST ERROR = 4;
+}