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); } } }