src/Services/GeneralService.php line 209

Open in your IDE?
  1. <?php
  2. namespace EADPlataforma\Services;
  3. use Psr\Container\ContainerInterface;
  4. use Symfony\Component\HttpFoundation\RequestStack;
  5. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  6. use EADPlataforma\Entity\Session;
  7. use EADPlataforma\Enum\ServicesEnum;
  8. /**
  9.  * GeneralService
  10.  */
  11. class GeneralService
  12. {   
  13.     /**
  14.      * @var ContainerInterface
  15.      */
  16.     protected $container;
  17.     /**
  18.      * @var RequestStack
  19.      */
  20.     protected $requestStack;
  21.     /**
  22.      * @var UrlGeneratorInterface
  23.      */
  24.     protected $router;
  25.     /**
  26.      * @var Array
  27.      */
  28.     protected $eadToken;
  29.     /**
  30.      * @var Object
  31.      */
  32.     protected $dataTokens;
  33.     /**
  34.      * @var Array
  35.      */
  36.     protected $services = [];
  37.     /**
  38.      * Constructor
  39.      *
  40.      * @param ContainerInterface $container
  41.      */
  42.     public function __construct(
  43.         ContainerInterface $container
  44.         RequestStack $requestStack
  45.         UrlGeneratorInterface $router
  46.     )
  47.     {
  48.         $this->container $container;
  49.         $this->requestStack $requestStack;
  50.         $this->router $router;
  51.         $this->eadToken $this->container->getParameter('ead-token');
  52.         $this->dataTokens json_decode($this->container->getParameter('ead-services'));
  53.     }
  54.     public function isSandbox()
  55.     {
  56.         return ($this->eadToken['sandbox'] == ServicesEnum::YES);
  57.     }
  58.     public function getTokenCron()
  59.     {
  60.         return $this->container->getParameter('ead-cron');
  61.     }
  62.     public function getServiceAccess(int $serviceType, ?int $sandbox ServicesEnum::NO)
  63.     {
  64.         $keyService "{$serviceType}{$sandbox}";
  65.         if(!empty($this->services[$keyService])){
  66.             return $this->services[$keyService];
  67.         }
  68.         if($this->isSandbox()){
  69.             $sandbox ServicesEnum::YES;
  70.         }
  71.         $mainServices = [
  72.             ServicesEnum::IP_API,
  73.             ServicesEnum::RDSTATION,
  74.             ServicesEnum::LOGIN,
  75.         ];
  76.         if($serviceType == ServicesEnum::AWS_SECRET){
  77.             $this->services[$keyService] = json_decode(
  78.                 $this->container->getParameter('ead-sget')
  79.             );
  80.             return $this->services[$keyService];
  81.         }
  82.         if($sandbox != ServicesEnum::YES){
  83.             if($serviceType == ServicesEnum::AWS_DYNAMODB_GET){
  84.                 $this->services[$keyService] = json_decode(
  85.                     $this->container->getParameter('ead-dget')
  86.                 );
  87.                 return $this->services[$keyService];
  88.             }
  89.             if(!in_array($serviceType$mainServices)){
  90.                 $serviceSecretId ServicesEnum::SERVICES_NAME[$serviceType];
  91.                 $serviceSecretId "{$serviceSecretId}/production";
  92.                 $awsSecretsManager $this->getService("Aws\\AwsSecretsManager");
  93.                 $info $awsSecretsManager->getServiceInfo($serviceSecretId);
  94.                 if(!empty($info)){
  95.                     $this->services[$keyService] = $info;
  96.                     return $this->services[$keyService];
  97.                 }
  98.             }
  99.         }
  100.         if(!empty($this->dataTokens)){
  101.             if(isset($this->dataTokens->{$serviceType})){
  102.                 return $this->dataTokens->{$serviceType};
  103.             }
  104.         }
  105.         throw new \Exception("Token not found for service: {$serviceType}");
  106.         return;
  107.     }
  108.     public function getUserFromEADAdmin($email$clientId$json false){
  109.         $info $this->getServiceAccess(ServicesEnum::LOGIN);
  110.         $data = [
  111.             "action" => "user",
  112.             "email" => $email,
  113.             "cliente_id" => $clientId,
  114.         ];
  115.         $data http_build_query($data);
  116.         $url "https://eadmin.eadplataforma.com/modulos/api/?{$data}";
  117.         $ch curl_init();
  118.         curl_setopt($chCURLOPT_URL$url);
  119.         curl_setopt($chCURLOPT_POST0);
  120.         curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  121.         curl_setopt($chCURLOPT_HTTPHEADER, [
  122.             'Authorization: Token token="' $info->token '"'
  123.         ]);
  124.         $response curl_exec($ch);
  125.         curl_close($ch);
  126.         
  127.         if (!$json){
  128.             $response json_decode($responsetrue);
  129.         }
  130.         
  131.         return $response;
  132.     }
  133.     public function getContainer()
  134.     {
  135.         return $this->container;
  136.     }
  137.     public function getRequest()
  138.     {
  139.         return $this->requestStack->getCurrentRequest();
  140.     }
  141.     public function getClientIp()
  142.     {
  143.         $request $this->getRequest();
  144.         if($request){
  145.             return $request->getClientIp();
  146.         }
  147.         return;
  148.     }
  149.     public function getRouter()
  150.     {
  151.         return $this->router;
  152.     }
  153.     public function getPath()
  154.     {
  155.         return $this->container->getParameter('kernel.project_dir');
  156.     }
  157.     public function getPublicPath()
  158.     {
  159.         return "{$this->getPath()}/public/";
  160.     }
  161.     public function getAssetsPath()
  162.     {
  163.         return "{$this->getPath()}/assets/";
  164.     }
  165.     public function getService(string $serviceName)
  166.     {
  167.         $service $this->container->get("EADPlataforma\Services\\{$serviceName}");
  168.         return $service;
  169.     }
  170.     public function getUtil(string $utilName)
  171.     {
  172.         $util $this->container->get("EADPlataforma\Util\\{$utilName}");
  173.         return $util;
  174.     }
  175.     public function setCookie($cookieName$value$time null, ?bool $useMd5 true)
  176.     {
  177.         if($useMd5){
  178.             $cookieName md5($cookieName);
  179.         }
  180.         $cookieName "edp_{$cookieName}";
  181.         $request $this->requestStack->getCurrentRequest();
  182.         $host $request->headers->get('host');
  183.         if(empty($time)){
  184.             $time time() + (10 365 24 60 60);
  185.         }
  186.         $options = [
  187.             "expires" => $time,
  188.             "path" => "/",
  189.             "domain" => $host,
  190.             "secure" => true,
  191.             "httponly" => true,
  192.             "samesite" => "None",
  193.         ];
  194.         // setcookie($cookieName, $value, $options);
  195.         setcookie($cookieName$value$time'/'$host);
  196.     }
  197.     public function getCookie($cookieName, ?bool $useMd5 true)
  198.     {
  199.         if($useMd5){
  200.             $cookieName md5($cookieName);
  201.         }
  202.         $cookieName "edp_{$cookieName}";
  203.         $request $this->requestStack->getCurrentRequest();
  204.         $cookies $request->cookies;
  205.         if($cookies->has($cookieName)){
  206.             return $cookies->get($cookieName);
  207.         }
  208.         return;
  209.     }
  210.     public function deleteCookie($cookieName, ?bool $useMd5 true)
  211.     {
  212.         if($useMd5){
  213.             $cookieName md5($cookieName);
  214.         }
  215.         $cookieName "edp_{$cookieName}";
  216.         
  217.         $request $this->requestStack->getCurrentRequest();
  218.         $host $request->headers->get('host');
  219.         
  220.         setcookie($cookieNamenull, -1'/'$host); 
  221.     }
  222.     public function getCookieHashIdentify()
  223.     {
  224.         $cookieName 'hashcartoff';
  225.         $hashIdentify $this->getCookie($cookieName);
  226.         if(empty($hashIdentify)){
  227.             $hashIdentify md5(rand() . strtotime(date('Y-m-d H:i:s')));
  228.             $hashIdentify .= md5(rand() . password_hash($hashIdentifyPASSWORD_DEFAULT));
  229.             $this->setCookie($cookieName$hashIdentify);
  230.         }
  231.         return $hashIdentify;
  232.     }
  233.     public function generateUrl(string $routeName, ?array $params = [])
  234.     {
  235.         return $this->router->generate($routeName, (!empty($params) ? $params : []));
  236.     }
  237.     public function logoffWS(Session $sessionstring $clientId)
  238.     {
  239.         $url "https://metrics.eadplataforma.app/ws/user/token/invalidate";
  240.         $curl curl_init();
  241.         curl_setopt($curlCURLOPT_URL$url);
  242.         curl_setopt($curlCURLOPT_TIMEOUT50);
  243.             
  244.         $headers = [
  245.             "Accept: application/json",
  246.             "Authorization: {$this->getTokenCron()}",
  247.         ];
  248.         $userToken md5($clientId) . md5($session->getId() . $session->getToken());
  249.         $data = [
  250.             "token" => $userToken,
  251.         ];
  252.         curl_setopt($curlCURLOPT_HTTPHEADER$headers);
  253.         curl_setopt($curlCURLOPT_CUSTOMREQUEST"POST");
  254.         curl_setopt($curlCURLOPT_POSTFIELDSjson_encode($data));
  255.         curl_setopt($curlCURLOPT_FOLLOWLOCATIONtrue);
  256.         curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
  257.         curl_setopt($curlCURLOPT_SSL_VERIFYPEERfalse);
  258.         
  259.         $response curl_exec($curl);
  260.         $error curl_error($curl);
  261.         curl_close($curl);
  262.         return;
  263.     }
  264. }