src/Manager/WsProgressManager.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\Manager;
  3. use App\Entity\Adherent\MagasinCoordonnees;
  4. use App\Entity\User\AdminUser;
  5. use DateTime;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use JetBrains\PhpStorm\NoReturn;
  8. use Psr\Log\LoggerInterface;
  9. use Symfony\Component\DependencyInjection\ContainerInterface;
  10. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  11. use Symfony\Component\HttpFoundation\JsonResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  14. use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
  15. use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
  16. class WsProgressManager
  17. {
  18.     const USERNAME "espadh@all";
  19.     const PASSWORD "Mozart/AO@59!";
  20.     private ?object $tokenStorage;
  21.     private DateTime $today;
  22.     private string $url "https://allwslanpp.groupeall.com:40443/Grall/";
  23.      public function __construct(
  24.         private readonly SessionInterface       $session,
  25.         private readonly LoggerInterface        $progressLogger,
  26.         ContainerInterface                      $container,
  27.         private readonly EntityManagerInterface $entityManager,
  28.         private readonly ParameterBagInterface  $parameterBag
  29.     )
  30.     {
  31.         $env $this->parameterBag->get('env');
  32.         $this->today = (new DateTime('now'));
  33.         if ($env === "prod" || $env === "dev") {
  34.             $this->url "https://allws.groupeall.com:40443/Grall/";
  35.         } elseif ($env === "preprod") {
  36.             $this->url "https://allwspp.groupeall.com:40443/Grall/";
  37.         }
  38.         $this->tokenStorage $container->get('security.token_storage');
  39.     }
  40.     public function getProgressUsername(): string
  41.     {
  42.         return self::USERNAME;
  43.     }
  44.     /**
  45.      * @param $url
  46.      * @return void
  47.      * Set Url
  48.      */
  49.     public function setUrl($url)
  50.     {
  51.         $this->url $url;
  52.     }
  53.     public function getUrl(): string
  54.     {
  55.         return $this->url;
  56.     }
  57.     public function getCurrentUser()
  58.     {
  59.         if (is_null($this->tokenStorage->getToken()) || $this->tokenStorage->getToken() instanceof AnonymousToken || $this->tokenStorage->getToken()->getUser() instanceof AdminUser) {
  60.             return 'C9920';
  61.         }
  62.         return $this->tokenStorage->getToken()->getUser()->getOriginalKadherent();
  63.     }
  64.     public function auth()
  65.     {
  66.         $this->logout();
  67.         $datas http_build_query([
  68.             'j_username' => self::USERNAME,
  69.             'j_password' => self::PASSWORD
  70.         ]);
  71.         $ch curl_init($this->url 'static/auth/j_spring_security_check');
  72.         curl_setopt($chCURLOPT_HTTPHEADER, ['Content-Type:application/x-www-form-urlencoded']);
  73.         curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  74.         curl_setopt($chCURLOPT_POSTFIELDS$datas);
  75.         curl_setopt($chCURLOPT_SSL_VERIFYHOST0);
  76.         curl_setopt($chCURLOPT_SSL_VERIFYPEER0);
  77.         curl_setopt($chCURLOPT_HEADER1);
  78.         $response curl_exec($ch);
  79.         preg_match_all('/^Set-Cookie:\s*([^;]*)/mi'$response$matches);
  80.         $this->progressLogger->info('Connexion', ['Connecté avec l\'adhérent : ' => $this->getCurrentUser()]);
  81.         $this->progressLogger->info('Response : ' $response, ['Connecté avec l\'adhérent : ' => $this->getCurrentUser()]);
  82.         if (isset($matches[1]) && !empty($matches[1])) {
  83.             $this->session->set('wsProgress_Timer', new \DateTime('now'));
  84.             $this->session->set('wsProgress_Token'$matches[1][0]);
  85.             $this->progressLogger->info('Reception du token :' $matches[1][0], ['Connecté avec l\'adhérent' => $this->getCurrentUser()]);
  86.             $this->moderationMode(true);
  87.         } else {
  88.             $this->progressLogger->info("Erreur : Pas de match pour créer la session", ['Connecté avec l\'adhérent' => $this->getCurrentUser()]);
  89.             return false;
  90.         }
  91.     }
  92.     public function logout()
  93.     {
  94.         $ch curl_init($this->url 'static/auth/j_spring_security_logout');
  95.         curl_setopt($chCURLOPT_HTTPHEADER, ['Content-Type:application/x-www-form-urlencoded']);
  96.         curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  97.         curl_setopt($chCURLOPT_SSL_VERIFYHOST0);
  98.         curl_setopt($chCURLOPT_SSL_VERIFYPEER0);
  99.         curl_setopt($chCURLOPT_FRESH_CONNECT1);
  100.         curl_setopt($chCURLOPT_FOLLOWLOCATIONtrue);
  101.         curl_setopt($chCURLOPT_COOKIE$this->session->get('wsProgress_Token'));
  102.         $deco curl_exec($ch);
  103.         $returnHeader curl_getinfo($ch);
  104.         $this->progressLogger->info("Deconnexion sur le token : " $this->session->get('wsProgress_Token'), ['Connecté avec l\'adhérent' => $this->getCurrentUser()]);
  105.         $this->session->remove('wsProgress_Token');
  106.         $this->session->remove('wsProgress_Timer');
  107.     }
  108.     public function moderationMode(bool $moderation true)
  109.     {
  110.         if ($moderation != $this->session->get('wsProgress_Moderation')) {
  111.             ini_set('memory_limit''-1');
  112.             $moderation = ($moderation) ? "1" "0";
  113.             $datas json_encode(['moderation' => $moderation]);
  114.             $ch curl_init($this->url 'web/moderation');
  115.             curl_setopt($chCURLOPT_HTTPHEADER, ['Content-Type:application/json']);
  116.             curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  117.             curl_setopt($chCURLOPT_POSTFIELDS$datas);
  118.             curl_setopt($chCURLOPT_SSL_VERIFYHOST0);
  119.             curl_setopt($chCURLOPT_SSL_VERIFYPEER0);
  120.             curl_setopt($chCURLOPT_COOKIE$this->session->get('wsProgress_Token'));
  121.             $response json_decode(curl_exec($ch), true);
  122.             $returnHeader curl_getinfo($ch);
  123.             if ($returnHeader['http_code'] == 302 && preg_match('/auth\/login.jsp/'$returnHeader['redirect_url'])) {
  124.                 $this->progressLogger->info("Modération 302 : " $this->session->get('wsProgress_Token'), [$this->getCurrentUser()]);
  125.                 $this->auth();
  126.                 curl_setopt($chCURLOPT_COOKIE$this->session->get('wsProgress_Token'));
  127.                 $response json_decode(curl_exec($ch), true);
  128.             }
  129.             if (!empty($response)) {
  130.                 if (isset($response['moderation']['moderation']) && !empty($response['moderation']['moderation'])) {
  131.                     $this->session->set('wsProgress_Moderation'$response['moderation']['moderation']);
  132.                 }
  133.             }
  134.             curl_close($ch);
  135.         }
  136.     }
  137.     /**
  138.      * @return string|void
  139.      */
  140.     public function codeToCentrale(string $codeAdherent)
  141.     {
  142.         if (substr(strtolower($codeAdherent), 01) == "c") {
  143.             return "ao";
  144.         } else if (substr(strtolower($codeAdherent), 01) == "r" || substr(strtolower($codeAdherent), 01) == "l") {
  145.             return "rev";
  146.         } else if (substr(strtolower($codeAdherent), 01) == "b") {
  147.             return "be";
  148.         }
  149.     }
  150.     public function selectTenant(string $codeAdherent)
  151.     {
  152.         $datas null;
  153.         switch ($this->codeToCentrale($codeAdherent)) {
  154.             case "ao":
  155.                 $datas json_encode(['tenant' => 'ao_fr']);
  156.                 $tenant 'ao_fr';
  157.                 break;
  158.             case "be":
  159.                 $datas json_encode(['tenant' => 'ao_be']);
  160.                 $tenant 'ao_be';
  161.                 break;
  162.             case "rev":
  163.                 if (str_starts_with(strtoupper($codeAdherent), 'L')) {
  164.                     $datas json_encode(['tenant' => 'rev_lu']);
  165.                     $tenant 'rev_lu';
  166.                 } else {
  167.                     $datas json_encode(['tenant' => 'rev_fr']);
  168.                     $tenant 'rev_fr';
  169.                 }
  170.                 break;
  171.         }
  172.         if ($this->session->get('wsProgress_Tenant') != $tenant) {
  173.             $this->progressLogger->info('Selection du tenant. Adherent : ' $codeAdherent ' => ' $tenant, ['Connecté avec l\'adhérent' => $this->getCurrentUser()]);
  174.             $this->progressLogger->info('Selection du tenant.' $this->url 'web/tenant', ['Connecté avec l\'adhérent' => $this->getCurrentUser()]);
  175.             ini_set('memory_limit''-1');
  176.             $ch curl_init($this->url 'web/tenant');
  177.             curl_setopt($chCURLOPT_HTTPHEADER, ['Content-Type:application/json']);
  178.             curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  179.             curl_setopt($chCURLOPT_POSTFIELDS$datas);
  180.             curl_setopt($chCURLOPT_SSL_VERIFYHOST0);
  181.             curl_setopt($chCURLOPT_SSL_VERIFYPEER0);
  182.             curl_setopt($chCURLOPT_FOLLOWLOCATIONfalse);
  183.             curl_setopt($chCURLOPT_COOKIE$this->session->get('wsProgress_Token'));
  184.             $this->progressLogger->info('Select Tenant :' $tenant ' sur le token : ' $this->session->get('wsProgress_Token'), ['Connecté avec l\'adhérent' => $this->getCurrentUser()]);
  185.             $response json_decode(curl_exec($ch), true);
  186.             $returnHeader curl_getinfo($ch);
  187.             if ($returnHeader['http_code'] == 302 && preg_match('/auth\/login.jsp/'$returnHeader['redirect_url'])) {
  188.                 $this->progressLogger->info('http_code 302 aucun session => reconnexion', ['Connecté avec l\'adhérent' => $this->getCurrentUser()]);
  189.                 $this->auth();
  190.                 curl_setopt($chCURLOPT_COOKIE$this->session->get('wsProgress_Token'));
  191.                 $response json_decode(curl_exec($ch), true);
  192.             }
  193.             $this->progressLogger->info('Response :' json_encode($response), ['Connecté avec l\'adhérent' => $this->getCurrentUser()]);
  194.             if (!empty($response)) {
  195.                 if (isset($response['tenant']['tenant']) && !empty($response['tenant']['tenant'])) {
  196.                     $this->session->set('wsProgress_Tenant'$response['tenant']['tenant']);
  197.                 }
  198.             }
  199.             curl_close($ch);
  200.         }
  201.     }
  202.     public function findBy(QueryProgress $queryProgress): array|\mixedjson_decode
  203.     {
  204.         $this->refreshSessionProgress();
  205.         ini_set('memory_limit''-1');
  206.         $this->progressLogger->info('Requete : ' $this->url 'rest/Grall/' $queryProgress->getTable() . '?filter=' $queryProgress->getQuery(), ['token' => $this->session->get('wsProgress_Token'), 'Connecté avec l\'adhérent' => $this->getCurrentUser()]);
  207.         $ch curl_init($this->url 'rest/Grall/' $queryProgress->getTable() . '?filter=' $queryProgress->getQuery());
  208.         curl_setopt($chCURLOPT_HTTPHEADER, ['Content-Type:application/json']);
  209.         curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  210.         curl_setopt($chCURLOPT_SSL_VERIFYHOST0);
  211.         curl_setopt($chCURLOPT_SSL_VERIFYPEER0);
  212.         curl_setopt($chCURLOPT_FRESH_CONNECT1);
  213.         curl_setopt($chCURLOPT_COOKIE$this->session->get('wsProgress_Token'));
  214.         $datas = (new ResultatProgress($queryProgress->getTable(), curl_exec($ch), $queryProgress->getSelect()))->getResult();
  215.         $returnHeader curl_getinfo($ch);
  216.         $this->progressLogger->info('Response : ' json_encode($datas), ['token' => $this->session->get('wsProgress_Token'), 'Connecté avec l\'adhérent' => $this->getCurrentUser()]);
  217.         if ($returnHeader['http_code'] == 302 && preg_match('/auth\/login.jsp/'$returnHeader['redirect_url'])) {
  218.             $this->progressLogger->info('http_code 302 pas de session au moment de la requete => reconnexion', ['Connecté avec l\'adhérent' => $this->getCurrentUser()]);
  219.             $this->auth();
  220.             curl_setopt($chCURLOPT_COOKIE$this->session->get('wsProgress_Token'));
  221.             $datas = (new ResultatProgress($queryProgress->getTable(), curl_exec($ch), $queryProgress->getSelect()))->getResult();
  222.         }
  223.         curl_close($ch);
  224.         return $datas;
  225.     }
  226.     /**
  227.      * @return mixed
  228.      */
  229.     public
  230.     function findOneBy(QueryProgress $queryProgress)
  231.     {
  232.         return current($this->findBy($queryProgress));
  233.     }
  234.     /**
  235.      * @param $table
  236.      * @param $object
  237.      * @return array
  238.      */
  239.     public
  240.     function update($table$object)
  241.     {
  242.         $this->refreshSessionProgress();
  243.         $datas = (new ResultatProgress($table$object))->arrayToProgress();
  244.         ini_set('memory_limit''-1');
  245.         $ch curl_init($this->url 'rest/Grall/' $table);
  246.         curl_setopt($chCURLOPT_HTTPHEADER, ['Content-Type:application/json']);
  247.         curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  248.         curl_setopt($chCURLOPT_CUSTOMREQUEST"PUT");
  249.         curl_setopt($chCURLOPT_POSTFIELDS$datas);
  250.         curl_setopt($chCURLOPT_SSL_VERIFYHOST0);
  251.         curl_setopt($chCURLOPT_HTTP_VERSIONCURL_HTTP_VERSION_1_1);
  252.         curl_setopt($chCURLOPT_SSL_VERIFYPEER0);
  253.         curl_setopt($chCURLOPT_FRESH_CONNECT1);
  254.         curl_setopt($chCURLOPT_COOKIE$this->session->get('wsProgress_Token'));
  255.         $datas = (new ResultatProgress($tablecurl_exec($ch)))->getUpdateResult();
  256.         $returnHeader curl_getinfo($ch);
  257.         if ($returnHeader['http_code'] == 302 && preg_match('/auth\/login.jsp/'$returnHeader['redirect_url'])) {
  258.             $this->auth();
  259.             curl_setopt($chCURLOPT_COOKIE$this->session->get('wsProgress_Token'));
  260.             $datas = (new ResultatProgress($tablecurl_exec($ch)))->getUpdateResult();
  261.         }
  262.         curl_close($ch);
  263.         return $datas;
  264.     }
  265.     public function insert($table$object)
  266.     {
  267.         $this->refreshSessionProgress();
  268.         $datas = (new ResultatProgress($table$object))->arrayToProgress();
  269.         ini_set('memory_limit''-1');
  270.         $ch curl_init($this->url 'rest/Grall/' $table);
  271.         curl_setopt($chCURLOPT_HTTPHEADER, ['Content-Type:application/json']);
  272.         curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  273.         curl_setopt($chCURLOPT_CUSTOMREQUEST"POST");
  274.         curl_setopt($chCURLOPT_POSTFIELDS$datas);
  275.         curl_setopt($chCURLOPT_SSL_VERIFYHOST0);
  276.         curl_setopt($chCURLOPT_SSL_VERIFYPEER0);
  277.         curl_setopt($chCURLOPT_FRESH_CONNECT1);
  278.         curl_setopt($chCURLOPT_COOKIE$this->session->get('wsProgress_Token'));
  279.         $datas = (new ResultatProgress($tablecurl_exec($ch)))->getUpdateResult();
  280.         $returnHeader curl_getinfo($ch);
  281.         if ($returnHeader['http_code'] == 302 && preg_match('/auth\/login.jsp/'$returnHeader['redirect_url'])) {
  282.             $this->auth();
  283.             curl_setopt($chCURLOPT_COOKIE$this->session->get('wsProgress_Token'));
  284.             $datas = (new ResultatProgress($tablecurl_exec($ch)))->getUpdateResult();
  285.         }
  286.         curl_close($ch);
  287.         return $datas;
  288.     }
  289.     public function delete($table$object)
  290.     {
  291.         $this->refreshSessionProgress();
  292.         $datas = (new ResultatProgress($table$object))->arrayToProgress();
  293.         ini_set('memory_limit''-1');
  294.         $ch curl_init($this->url 'rest/Grall/' $table);
  295.         curl_setopt($chCURLOPT_HTTPHEADER, ['Content-Type:application/json']);
  296.         curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  297.         curl_setopt($chCURLOPT_CUSTOMREQUEST"DELETE");
  298.         curl_setopt($chCURLOPT_POSTFIELDS$datas);
  299.         curl_setopt($chCURLOPT_SSL_VERIFYHOST0);
  300.         curl_setopt($chCURLOPT_SSL_VERIFYPEER0);
  301.         curl_setopt($chCURLOPT_FRESH_CONNECT1);
  302.         curl_setopt($chCURLOPT_COOKIE$this->session->get('wsProgress_Token'));
  303.         $datas = (new ResultatProgress($tablecurl_exec($ch)))->getUpdateResult();
  304.         $returnHeader curl_getinfo($ch);
  305.         if ($returnHeader['http_code'] == 302 && preg_match('/auth\/login.jsp/'$returnHeader['redirect_url'])) {
  306.             $this->auth();
  307.             curl_setopt($chCURLOPT_COOKIE$this->session->get('wsProgress_Token'));
  308.             $datas = (new ResultatProgress($tablecurl_exec($ch)))->getUpdateResult();
  309.         }
  310.         curl_close($ch);
  311.         return $datas;
  312.     }
  313.     /**
  314.      * @return string
  315.      */
  316.     public
  317.     function getCookieId()
  318.     {
  319.         if ($this->session->has('wsProgress_Token')) {
  320.             return $this->session->get('wsProgress_Token');
  321.         } else {
  322.             $this->auth();
  323.             return $this->session->get('wsProgress_Token');
  324.         }
  325.     }
  326.     /**
  327.      * @return mixed|null
  328.      */
  329.     public
  330.     function getCurrentTenant()
  331.     {
  332.         if (!empty($this->session->get('wsProgress_Tenant'))) {
  333.             return $this->session->get('wsProgress_Tenant');
  334.         } else {
  335.             return null;
  336.         }
  337.     }
  338.     public function updateDateMajProgressLines($datas)
  339.     {
  340.         $datas['D_MAJ'] = $this->today->format('Y-m-d');
  341.         $datas['O_MAJ'] = $this->getProgressUsername();
  342.         $datas['H_MAJ'] = strtotime($this->today->format('H:i:s'));
  343.         return $datas;
  344.     }
  345.     public function refreshSessionProgress(): void
  346.     {
  347.         if (!$this->session->has('wsProgress_Timer') || $this->getTotalMinutes($this->session->get('wsProgress_Timer')->diff($this->today)) >= 30) {
  348.             $this->auth();
  349.             $this->selectTenant($this->getCurrentUser());
  350.         }
  351.     }
  352.     private function getTotalMinutes($dateInterval): float|int
  353.     {
  354.         return ($dateInterval->24 60) + ($dateInterval->60) + $dateInterval->i;
  355.     }
  356. }