123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- <?php
- namespace Flowdat\Export\App\Service;
- 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){
- try{
- $tenancyId = $data['tenancyId'];
- $format = $data['format'];
- $endpointBase = $data['endpointBase'];
- $endpointOnus = $data['endpointOnus'];
- $onus = $this->getOnus($endpointOnus, $headers, $tenancyId);
- $clientsIds = array_map(function ($onu){
- return $onu['clientId'];
- }, $onus);
- $clientsIds = array_unique($clientsIds);
- $clients = $this->getClients($clientsIds, $endpointBase, $headers);
- foreach($onus as $key => $onu){
- $onus[$key]['created'] = $onus[$key]['created']['date'];
- $onus[$key]['updated'] = is_array($onus[$key]['updated']) ? $onus[$key]['updated']['date'] : null;
- if(isset($clients[$onu['clientId']])){
- $client = $clients[$onu['clientId']];
- $onus[$key]['externalId'] = $client['externalId'];
- $onus[$key]['clientName'] = $client['name'];
- }
- $onus[$key] = array_reverse($onus[$key]);
- }
- $header = [
- 'Nombre Cliente',
- 'External ID Cliente',
- 'Client ID',
- 'Pon Serial Number',
- 'Serial Number',
- 'Estado',
- 'NAP',
- 'OLT',
- 'Código de Activación',
- 'VoIP',
- 'CATV',
- 'Position',
- 'LINK',
- 'SLOT',
- 'Validación Radius',
- 'MAC',
- 'IP',
- 'Creada',
- 'Actualizada'
- ];
- $fileName = $data['filename'];
- $filePath = ExportFactory::create($format, $fileName, $onus, $header);
- $filePathS3 = '';
- if(is_file($filePath)){
- $filePathS3 = S3Factory::send($filePath, $fileName);
- }
- 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;
- }
- /**
- * @param $endpoint
- * @param $tenancyId
- * @param $headers
- * @return bool|mixed
- * @throws \Exception
- */
- public function getOnus($endpoint, $headers, $tenancyId){
- try {
- $page = 1;
- $limit = 1000;
- $onus = [];
- $this->makeRequestAndInsertOnu($endpoint, $headers, $onus, $page, $limit, $tenancyId);
- return $onus;
- } catch (\Exception $ex) {
- throw new \Exception($ex->getMessage());
- }
- }
- public function makeRequestAndInsertOnu($endpoint, $headers, &$onus, $currentPage, $limit, $tenancyId){
- try{
- $params = [
- 'filters' => [
- 'currentPage' => $currentPage,
- 'limit' => $limit,
- 'tenancyId' => $tenancyId
- ]
- ];
- $requestOnu = $this->requestService->get($this->requestService->buildUrl($endpoint, $params), $headers);
- if($requestOnu['statusCode'] != 200){
- echo "Isn't possible to get a some ONUS \n";
- return true;
- }
- $response = json_decode($requestOnu['response'], true);
- $qtdPage = $response['pages'];
- echo "Getting ONUs {$currentPage}/{$qtdPage}".PHP_EOL;
- foreach ($response['data'] as $onu){
- array_push($onus, $onu);
- }
- if($response['hasNext']){
- $currentPage++;
- return $this->makeRequestAndInsertOnu($endpoint, $headers, $onus, $currentPage, $limit, $tenancyId);
- }
- return true;
- }catch (\Exception $ex){
- throw new \Exception($ex->getMessage());
- }
- }
- public function getClients($allClientsIds, $endpointBase, $headers){
- try{
- $clientsIdsChunk = array_chunk($allClientsIds, 1000);
- $clients = [];
- $qtdClientsPage = count($clientsIdsChunk);
- $i = 1;
- foreach ($clientsIdsChunk as $clientIds){
- $params = [
- 'filters' => [
- 'qb-criteria' => true,
- 'qb-ids' => implode(",", $clientIds)
- ]
- ];
- $url = $this->requestService->buildUrl($endpointBase.'/api/clients', $params);
- $requestClients = $this->requestService->get($url, $headers);
- if($requestClients['statusCode'] != 200){
- echo "Isn't possible get the clients \n";
- continue;
- }
- echo "Getting Clients $i/$qtdClientsPage".PHP_EOL;
- $i++;
- $response = json_decode($requestClients['response'], true);
- foreach ($response as $client){
- $clients[$client['id']] = ['id' => $client['id'], 'name' => $client['name'], 'externalId' => $client['externalId']];
- }
- }
- return $clients;
- }catch (\Exception $ex){
- throw new \Exception($ex->getMessage());
- }
- }
- public function updateNotification($endpointBase, $headers, $notificationId, $filePathS3){
- echo "Updating notification with ID ".$notificationId.PHP_EOL;
- try {
- $this->notificationService->update($endpointBase, $notificationId, [
- 'message' => 'Finalizó la exportación de ONUs',
- '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);
- }
- }
- }
|