src/Controller/DomainController.php line 46

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\Validator\Constraints\DateTime;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use \Ovh\Api;
  9. use App\Entity\Domain;
  10. class DomainController extends AbstractController
  11. {
  12.     // private $applicationKey = '0a662f6bdcd4bc30';
  13.     // private $applicationSecret = 'bc7696ead0d3972aa3cd9eec66205d74';
  14.     // private $consumerKey = '062e04951f29eb4c07da3417df07d86e';
  15.     private $applicationKey 'd0c1149e208ff4b7';
  16.     private $applicationSecret 'da693cf42b18070d53012640d3ca216c';
  17.     private $consumerKey 'b3c7479010c4e3ce407eaa634e22ae3e';
  18.     /**
  19.      * @Route("/domain", name="app_domain")
  20.      */
  21.     public function index(): Response
  22.     {
  23.         $ovh = new Api($this->applicationKey,
  24.                 $this->applicationSecret,
  25.                 'ovh-eu',
  26.                 $this->consumerKey);
  27.         $domains $ovh->get('/domain');
  28.         $domains_to_delete 0;
  29.         $time $ovh->get('/auth/time');
  30.         $em $this->getDoctrine()->getManager();
  31.         $domainInfos = array();
  32.         $num 0;
  33.         foreach ($domains as $key => $domain) {
  34.             $domainsDb $em->getRepository(Domain::class)->findOneBy(['nameDomain' => $domain]);
  35.             if(!$domainsDb && $num 42) {
  36.                 $domainInfos[$domain] = $ovh->get('/domain/'.$domain.'/serviceInfos');
  37.                 $dateRenew = new \DateTime($domainInfos[$domain]['expiration']);
  38.                 $domainDb = new Domain();
  39.                 $domainDb->setNameDomain$domain );
  40.                 $domainDb->setRenewAutomatic$domainInfos[$domain]['renew']['automatic'] );
  41.                 $domainDb->setDateRenew$dateRenew );
  42.                 $domainDb->setDeleteToExpired$domainInfos[$domain]['renew']['deleteAtExpiration'] );
  43.                 // $domainDb->setNameDomain($domain);
  44.                 $em->persist($domainDb);
  45.                 $em->flush();
  46.                 $num++;
  47.                 // if($num == 42) {
  48.                 //     dd(count($domainsDb));
  49.                 // }
  50.             }
  51.             // dd($domainsDb);
  52.             // $domainInfos[$domain] = $ovh->get('/domain/'.$domain.'/serviceInfos');
  53.         }
  54.         $domainsDb $em->getRepository(Domain::class)->findAll();
  55.         foreach ($domainsDb as $key => $domainDb) {
  56.             if($domainDb->isDeleteToExpired()) {
  57.                 $domains_to_delete++;
  58.             }
  59.             if( !in_array($domainDb->getNameDomain(), $domains) ) {
  60.         //         dd($domainDb->getNameDomain());
  61.                 $em->remove($domainDb);
  62.                 $em->flush();
  63.         //         // $index_domain = array_search($domainDb->getNameDomain(), $domains_to_delete); 
  64.         //         // unset($domains_to_delete[$index_domain]);
  65.             }
  66.         }
  67.         $date = new \DateTime('now');
  68.         $date->setTimestamp($time);
  69.         // dd($date->format('Y-m-d H:i:s'));
  70.         return $this->render('domain/index.html.twig', [
  71.             'time' => $time,
  72.             'date' => $date->format('Y-m-d H:i:s'),
  73.             'domains' => $domains,
  74.             'domainsDb' => $domainsDb,
  75.             // 'domainInfos' => $domainInfos,
  76.             'domainsNbr' => count($domainsDb),
  77.             'domainsNbrToDelete' => $domains_to_delete,
  78.             'pageClass' => 'domaines',
  79.             'controller_name' => 'DomainController',
  80.         ]);
  81.     }
  82.     /**
  83.      * @Route("/domain-infos/{domain}", methods={"GET"}, name="getDomainInfos")
  84.      * Cette fonction permet de récupérer les les infos d'un domaine
  85.      */
  86.     public function getDomainInfos($domain)
  87.     {
  88.         $error '';
  89.         $ovh = new Api($this->applicationKey,
  90.                 $this->applicationSecret,
  91.                 'ovh-eu',
  92.                 $this->consumerKey);
  93.         $domainInfos $ovh->get('/domain/'.$domain.'/serviceInfos');
  94.         // dd($date->format('Y-m-d H:i:s'));
  95.         if ($error != '') {
  96.             return new JsonResponse(['error' => $error], JsonResponse::HTTP_CREATED);
  97.         } else {
  98.             return new JsonResponse(['infos' => $domainInfos], JsonResponse::HTTP_CREATED);      
  99.         }
  100.     }
  101. }