<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController as Controller;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Routing\RouterInterface;
use App\Entity\PrestataireRepository;
use App\Entity\TransactionRepository;
use App\Entity\DepartementRepository;
use App\Entity\CreditPackRepository;
use App\Entity\CategorieRepository;
use App\Entity\DemandeRepository;
use App\Misc\EntityUrlGenerator;
use App\Entity\VilleRepository;
use App\Entity\PageRepository;
use App\Entity\UserRepository;
use App\Entity\UrlRepository;
use App\Misc\Flashes;
use App\Entity\Prestataire;
use App\Entity\Transaction;
use App\Entity\Departement;
use App\Entity\CreditPack;
use App\Entity\Categorie;
use App\Form\DemandeType;
use App\Misc\Options;
use App\Entity\Ville;
use App\Entity\Demande;
use App\Entity\Page;
use App\Entity\User;
use App\Entity\Url;
use Symfony\Component\HttpFoundation\Response;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
class AbstractController extends Controller
{
public function __construct(
protected MailerInterface $mailer,
private EntityUrlGenerator $entityUrlGenerator,
private RouterInterface $router,
private RequestStack $requestStack,
private EntityManagerInterface $entityManager
) {
}
/**
* @return EntityManagerInterface
*/
public function getEm(): EntityManagerInterface
{
return $this->entityManager;
}
/**
* @return PageRepository
*/
public function getPageRepository(): PageRepository
{
return $this->getEm()->getRepository(Page::class);
}
/**
* @return UrlRepository
*/
public function getUrlRepository(): UrlRepository
{
return $this->getEm()->getRepository(Url::class);
}
/**
* @return CategorieRepository
*/
public function getCategorieRepository(): CategorieRepository
{
return $this->getEm()->getRepository(Categorie::class);
}
/**
* @return DemandeRepository
*/
public function getDemandeRepository(): DemandeRepository
{
return $this->getEm()->getRepository(Demande::class);
}
/**
* @return DepartementRepository
*/
public function getDepartementRepository(): DepartementRepository
{
return $this->getEm()->getRepository(Departement::class);
}
/**
* @return PrestataireRepository
*/
public function getPrestataireRepository(): PrestataireRepository
{
return $this->getEm()->getRepository(Prestataire::class);
}
/**
* @return VilleRepository
*/
public function getVilleRepository(): VilleRepository
{
return $this->getEm()->getRepository(Ville::class);
}
/**
* @return TransactionRepository
*/
public function getTransactionRepository(): TransactionRepository
{
return $this->entityManager->getRepository(Transaction::class);
}
/**
* @return CreditPackRepository
*/
public function getCreditPack(): CreditPackRepository
{
return $this->getEm()->getRepository(CreditPack::class);
}
/**
* @return UserRepository
*/
public function getUserRepository(): UserRepository
{
return $this->getEm()->getRepository(User::class);
}
/**
* @return EntityUrlGenerator
*/
public function getUrlGenerator(): EntityUrlGenerator
{
return $this->entityUrlGenerator;
}
/**
* @return RouterInterface
*/
public function getRouter(): RouterInterface
{
return $this->router;
}
/**
* @return SessionInterface
*/
public function getSession(): SessionInterface
{
return $this->requestStack->getSession();
}
/**
* @return MailerInterface
*/
public function getMailer(): MailerInterface
{
return $this->mailer;
}
/**
* @param Transaction $transaction
*/
public function setInvoiceNumber(Transaction $transaction): void
{
/** @var Option $option */
$number = $this->getOption('invoice_number');
$invoiceNumber = $transaction->getDate()->format('Ym').$number;
$transaction->setInvoiceNumber($invoiceNumber);
$this->setOption('invoice_number', $number + 1);
}
/**
* @param Demande $demande
* @param Prestataire $prestataire
*/
public function removeFromPrestaCart(Demande $demande, Prestataire $prestataire): void
{
/** @var TransactionRepository $repo */
$repo = $this->getEM()->getRepository(Transaction::class);
// remove demande from current prestataire cart
$cart = $repo->getCart($prestataire->getId());
if ($cart) {
$cart->removeDemande($demande);
}
$this->getEM()->flush();
}
/**
* @param Transaction|null $cart
*
* @return float
*/
public function getCartPrix(?Transaction $cart): float
{
$prix = 0.0;
if ($cart && $cart->getDemandes()) {
/** @var Demande $demande */
foreach ($cart->getDemandes() as $demande) {
$prix += $demande->getPrix();
}
}
return $prix;
}
/**
* @param Transaction|null $cart
*
* @return float
*/
public function getCartCredits(?Transaction $cart): float
{
$credits = 0.0;
if ($cart && $cart->getDemandes()) {
/** @var Demande $demande */
foreach ($cart->getDemandes() as $demande) {
$credits += $demande->getCredits();
}
}
return $credits;
}
/**
* @return Prestataire|null
*/
public function getPrestataire(): ?Prestataire
{
/** @var User $user */
$user = $this->getUser();
if ($user && $user->isPrestataire()) {
return $user->getPrestataire();
}
return null;
}
public function throw404(): Response
{
// throw new NotFoundHttpException();
return $this->render('bundles/TwigBundle/Exception/error404.html.twig');
}
public function getDemandeForm(?Demande $demande = null, bool $preDemande = false, ?int $cid = null, ?bool $header = false)
{
$categories = $this->getCategorieRepository()->getCategorieTree();
if (!$demande) {
$demande = new Demande();
}
if ($cid) {
$category = $this->getEm()->getRepository(Categorie::class)->find($cid);
$demande->setCategorie($category);
}
$preForm = $this->createForm(DemandeType::class, $demande, array(
'action' => $this->generateUrl('public_demande_form'),
'em' => $this->entityManager,
'categories' => $categories,
'groups' => array_flip([
'diagnostics' => 72,
'expertises' => 73,
'etudes' => 74,
'solutions' => 75,
'energies' => 76
]),
'pre_demande' => $preDemande,
'header' => $header,
));
return $preForm;
}
public function getOption(string $key): mixed
{
/** @var Options $options */
$options = new Options($this->getEm());
return $options->get($key);
}
public function setOption(string $key, mixed $value): void
{
/** @var Options $options */
$options = new Options($this->getEm());
$options->setOption($key, $value);
}
public function getOffset(?int $page, ?int $limit): ?int
{
if ($limit !== null) {
$page = $page ?: 1;
return ($page - 1) * $limit;
}
return null;
}
/**
* @return Paginator
*/
public function getPaginator()
{
return $this->get('knp_paginator');
}
/**
* @param mixed $token
*/
public function validateUserToken($token): User|false
{
if (!$token || !$token->getData()) {
return false;
}
/** @var User|null $user */
$user = $this->getEM()->getRepository(User::class)->find($token->getData());
if (!$user) {
return false;
}
return $user;
}
/**
* Implémentation PHP de l'algorithme de Luhn basée sur la version XSLT officielle (https://xml.insee.fr/schema/siret.html#controles)
* Les indices commencent à 1, les chiffres d'indice impair sont multipliés par 2
*/
protected function isValidSiretChecksum(string $numero, int $somme = 0, int $indice = 1): bool
{
// Cas de base : plus de chiffres à traiter
if (strlen($numero) === 0) {
return ($somme % 10) === 0;
}
$premierChiffre = (int)substr($numero, 0, 1);
$resteNumero = substr($numero, 1);
if ($indice % 2 === 0) {
// Cas des chiffres d'indice pair : ajouter directement
return $this->isValidSiretChecksum($resteNumero, $somme + $premierChiffre, $indice + 1);
} else {
// Cas des chiffres d'indice impair : multiplier par 2 et additionner les chiffres
$double = $premierChiffre * 2;
$doubleStr = str_pad((string)$double, 2, '0', STR_PAD_LEFT);
$sommeChiffres = (int)substr($doubleStr, 0, 1) + (int)substr($doubleStr, 1, 1);
return $this->isValidSiretChecksum($resteNumero, $somme + $sommeChiffres, $indice + 1);
}
}
}