Преглед на файлове

Created service and pagination to get a ONU and changed composer.lock to do update from oauth bundle

Jean Sumara преди 5 години
родител
ревизия
de8e05c086
променени са 2 файла, в които са добавени 108 реда и са изтрити 0 реда
  1. 72 0
      src/FTTHBundle/Service/OnuExportService.php
  2. 36 0
      src/FTTHBundle/Utils/PageTrait.php

+ 72 - 0
src/FTTHBundle/Service/OnuExportService.php

@@ -0,0 +1,72 @@
+<?php
+
+
+namespace FTTHBundle\Service;
+
+
+use FTTHBundle\Entity\ONU;
+use FTTHBundle\Utils\PageTrait;
+use PhpAmqpLib\Message\AMQPMessage;
+use Symfony\Component\DependencyInjection\ContainerInterface;
+
+class OnuExportService
+{
+    use PageTrait;
+
+    private $container;
+
+    /**
+     * OnuExportService constructor.
+     * @param ContainerInterface $container
+     */
+    public function __construct(ContainerInterface $container)
+    {
+        $this->container = $container;
+    }
+
+    public function send($tenancyId, $format){
+        $amqpChannel = $this->container->get('amqp.channel');
+
+        try{
+            $msgBody = serialize([
+                'tenancyId' => $tenancyId,
+                'format' => $format,
+                'endpointToken' => 'https://'.getenv('HOST_BASE')."/oauth/v2/token",
+                'endpointOnus' => 'https://'.getenv('HOST_FTTH')."/api/onus-export",
+                'params' => [
+                    'client_id' => getenv('OAUTH_CLIENT_ID'),
+                    'client_secret' => getenv('OAUTH_CLIENT_SECRET'),
+                    'grant_type' => 'client_credentials'
+                ]
+            ]);
+            $amqpChannel->basic_publish(new AMQPMessage((string) $msgBody), '','export.ftth');
+        }catch (\Exception $ex){
+
+        }
+    }
+
+    public function getOnusWithCostumer($tenancyId = 1, $currentPage = 1, $limit = 2){
+        $entityManager = $this->container->get('doctrine')->getManager();
+        $onuRepository = $entityManager->getRepository(ONU::class);
+
+
+        $currentPage = $this->getPage($currentPage);
+        $limit = $this->getLimit($limit);
+        $offset = $this->getOffset($currentPage, $limit);
+        $total = 0;
+
+        $result = $onuRepository->getAllOnusToExport($tenancyId, $offset, $limit);
+        if ($result) {
+            $total = $onuRepository->getAllOnusToExportCount($tenancyId);
+        }
+
+        return [
+            'data' => $result,
+            'currentPage' => $currentPage,
+            'limit' => $limit,
+            'total' => $total,
+            'pages' => ceil($total / $limit),
+            'hasNext' =>  ceil($total / $limit) > $currentPage
+        ];
+    }
+}

+ 36 - 0
src/FTTHBundle/Utils/PageTrait.php

@@ -0,0 +1,36 @@
+<?php
+
+
+namespace FTTHBundle\Utils;
+
+
+trait PageTrait
+{
+    public function getPage($page = 1)
+    {
+        if ($page < 1) {
+            $page = 1;
+        }
+
+        return floor($page);
+    }
+
+    public function getLimit($limit = 20)
+    {
+        if ($limit < 1 || $limit > 20) {
+            $limit = 20;
+        }
+
+        return floor($limit);
+    }
+
+    public function getOffset($page, $limit)
+    {
+        $offset = 0;
+        if ($page != 0 && $page != 1) {
+            $offset = ($page - 1) * $limit;
+        }
+
+        return $offset;
+    }
+}