<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Validator\Constraints\DateTime;
use Symfony\Component\HttpFoundation\JsonResponse;
use \Ovh\Api;
use App\Entity\Domain;
class DomainController extends AbstractController
{
// private $applicationKey = '0a662f6bdcd4bc30';
// private $applicationSecret = 'bc7696ead0d3972aa3cd9eec66205d74';
// private $consumerKey = '062e04951f29eb4c07da3417df07d86e';
private $applicationKey = 'd0c1149e208ff4b7';
private $applicationSecret = 'da693cf42b18070d53012640d3ca216c';
private $consumerKey = 'b3c7479010c4e3ce407eaa634e22ae3e';
/**
* @Route("/domain", name="app_domain")
*/
public function index(): Response
{
$ovh = new Api($this->applicationKey,
$this->applicationSecret,
'ovh-eu',
$this->consumerKey);
$domains = $ovh->get('/domain');
$domains_to_delete = 0;
$time = $ovh->get('/auth/time');
$em = $this->getDoctrine()->getManager();
$domainInfos = array();
$num = 0;
foreach ($domains as $key => $domain) {
$domainsDb = $em->getRepository(Domain::class)->findOneBy(['nameDomain' => $domain]);
if(!$domainsDb && $num < 42) {
$domainInfos[$domain] = $ovh->get('/domain/'.$domain.'/serviceInfos');
$dateRenew = new \DateTime($domainInfos[$domain]['expiration']);
$domainDb = new Domain();
$domainDb->setNameDomain( $domain );
$domainDb->setRenewAutomatic( $domainInfos[$domain]['renew']['automatic'] );
$domainDb->setDateRenew( $dateRenew );
$domainDb->setDeleteToExpired( $domainInfos[$domain]['renew']['deleteAtExpiration'] );
// $domainDb->setNameDomain($domain);
$em->persist($domainDb);
$em->flush();
$num++;
// if($num == 42) {
// dd(count($domainsDb));
// }
}
// dd($domainsDb);
// $domainInfos[$domain] = $ovh->get('/domain/'.$domain.'/serviceInfos');
}
$domainsDb = $em->getRepository(Domain::class)->findAll();
foreach ($domainsDb as $key => $domainDb) {
if($domainDb->isDeleteToExpired()) {
$domains_to_delete++;
}
if( !in_array($domainDb->getNameDomain(), $domains) ) {
// dd($domainDb->getNameDomain());
$em->remove($domainDb);
$em->flush();
// // $index_domain = array_search($domainDb->getNameDomain(), $domains_to_delete);
// // unset($domains_to_delete[$index_domain]);
}
}
$date = new \DateTime('now');
$date->setTimestamp($time);
// dd($date->format('Y-m-d H:i:s'));
return $this->render('domain/index.html.twig', [
'time' => $time,
'date' => $date->format('Y-m-d H:i:s'),
'domains' => $domains,
'domainsDb' => $domainsDb,
// 'domainInfos' => $domainInfos,
'domainsNbr' => count($domainsDb),
'domainsNbrToDelete' => $domains_to_delete,
'pageClass' => 'domaines',
'controller_name' => 'DomainController',
]);
}
/**
* @Route("/domain-infos/{domain}", methods={"GET"}, name="getDomainInfos")
* Cette fonction permet de récupérer les les infos d'un domaine
*/
public function getDomainInfos($domain)
{
$error = '';
$ovh = new Api($this->applicationKey,
$this->applicationSecret,
'ovh-eu',
$this->consumerKey);
$domainInfos = $ovh->get('/domain/'.$domain.'/serviceInfos');
// dd($date->format('Y-m-d H:i:s'));
if ($error != '') {
return new JsonResponse(['error' => $error], JsonResponse::HTTP_CREATED);
} else {
return new JsonResponse(['infos' => $domainInfos], JsonResponse::HTTP_CREATED);
}
}
}