src/Controller/AbstractController.php line 246

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController as Controller;
  4. use Symfony\Component\HttpFoundation\RequestStack;
  5. use Symfony\Component\Mailer\MailerInterface;
  6. use Symfony\Component\Routing\RouterInterface;
  7. use App\Entity\PrestataireRepository;
  8. use App\Entity\TransactionRepository;
  9. use App\Entity\DepartementRepository;
  10. use App\Entity\CreditPackRepository;
  11. use App\Entity\CategorieRepository;
  12. use App\Entity\DemandeRepository;
  13. use App\Misc\EntityUrlGenerator;
  14. use App\Entity\VilleRepository;
  15. use App\Entity\PageRepository;
  16. use App\Entity\UserRepository;
  17. use App\Entity\UrlRepository;
  18. use App\Misc\Flashes;
  19. use App\Entity\Prestataire;
  20. use App\Entity\Transaction;
  21. use App\Entity\Departement;
  22. use App\Entity\CreditPack;
  23. use App\Entity\Categorie;
  24. use App\Form\DemandeType;
  25. use App\Misc\Options;
  26. use App\Entity\Ville;
  27. use App\Entity\Demande;
  28. use App\Entity\Page;
  29. use App\Entity\User;
  30. use App\Entity\Url;
  31. use Symfony\Component\HttpFoundation\Response;
  32. use Doctrine\ORM\EntityManagerInterface;
  33. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  34. class AbstractController extends Controller
  35. {
  36. public function __construct(
  37. protected MailerInterface $mailer,
  38. private EntityUrlGenerator $entityUrlGenerator,
  39. private RouterInterface $router,
  40. private RequestStack $requestStack,
  41. private EntityManagerInterface $entityManager
  42. ) {
  43. }
  44. /**
  45. * @return EntityManagerInterface
  46. */
  47. public function getEm(): EntityManagerInterface
  48. {
  49. return $this->entityManager;
  50. }
  51. /**
  52. * @return PageRepository
  53. */
  54. public function getPageRepository(): PageRepository
  55. {
  56. return $this->getEm()->getRepository(Page::class);
  57. }
  58. /**
  59. * @return UrlRepository
  60. */
  61. public function getUrlRepository(): UrlRepository
  62. {
  63. return $this->getEm()->getRepository(Url::class);
  64. }
  65. /**
  66. * @return CategorieRepository
  67. */
  68. public function getCategorieRepository(): CategorieRepository
  69. {
  70. return $this->getEm()->getRepository(Categorie::class);
  71. }
  72. /**
  73. * @return DemandeRepository
  74. */
  75. public function getDemandeRepository(): DemandeRepository
  76. {
  77. return $this->getEm()->getRepository(Demande::class);
  78. }
  79. /**
  80. * @return DepartementRepository
  81. */
  82. public function getDepartementRepository(): DepartementRepository
  83. {
  84. return $this->getEm()->getRepository(Departement::class);
  85. }
  86. /**
  87. * @return PrestataireRepository
  88. */
  89. public function getPrestataireRepository(): PrestataireRepository
  90. {
  91. return $this->getEm()->getRepository(Prestataire::class);
  92. }
  93. /**
  94. * @return VilleRepository
  95. */
  96. public function getVilleRepository(): VilleRepository
  97. {
  98. return $this->getEm()->getRepository(Ville::class);
  99. }
  100. /**
  101. * @return TransactionRepository
  102. */
  103. public function getTransactionRepository(): TransactionRepository
  104. {
  105. return $this->entityManager->getRepository(Transaction::class);
  106. }
  107. /**
  108. * @return CreditPackRepository
  109. */
  110. public function getCreditPack(): CreditPackRepository
  111. {
  112. return $this->getEm()->getRepository(CreditPack::class);
  113. }
  114. /**
  115. * @return UserRepository
  116. */
  117. public function getUserRepository(): UserRepository
  118. {
  119. return $this->getEm()->getRepository(User::class);
  120. }
  121. /**
  122. * @return EntityUrlGenerator
  123. */
  124. public function getUrlGenerator(): EntityUrlGenerator
  125. {
  126. return $this->entityUrlGenerator;
  127. }
  128. /**
  129. * @return RouterInterface
  130. */
  131. public function getRouter(): RouterInterface
  132. {
  133. return $this->router;
  134. }
  135. /**
  136. * @return SessionInterface
  137. */
  138. public function getSession(): SessionInterface
  139. {
  140. return $this->requestStack->getSession();
  141. }
  142. /**
  143. * @return MailerInterface
  144. */
  145. public function getMailer(): MailerInterface
  146. {
  147. return $this->mailer;
  148. }
  149. /**
  150. * @param Transaction $transaction
  151. */
  152. public function setInvoiceNumber(Transaction $transaction): void
  153. {
  154. /** @var Option $option */
  155. $number = $this->getOption('invoice_number');
  156. $invoiceNumber = $transaction->getDate()->format('Ym').$number;
  157. $transaction->setInvoiceNumber($invoiceNumber);
  158. $this->setOption('invoice_number', $number + 1);
  159. }
  160. /**
  161. * @param Demande $demande
  162. * @param Prestataire $prestataire
  163. */
  164. public function removeFromPrestaCart(Demande $demande, Prestataire $prestataire): void
  165. {
  166. /** @var TransactionRepository $repo */
  167. $repo = $this->getEM()->getRepository(Transaction::class);
  168. // remove demande from current prestataire cart
  169. $cart = $repo->getCart($prestataire->getId());
  170. if ($cart) {
  171. $cart->removeDemande($demande);
  172. }
  173. $this->getEM()->flush();
  174. }
  175. /**
  176. * @param Transaction|null $cart
  177. *
  178. * @return float
  179. */
  180. public function getCartPrix(?Transaction $cart): float
  181. {
  182. $prix = 0.0;
  183. if ($cart && $cart->getDemandes()) {
  184. /** @var Demande $demande */
  185. foreach ($cart->getDemandes() as $demande) {
  186. $prix += $demande->getPrix();
  187. }
  188. }
  189. return $prix;
  190. }
  191. /**
  192. * @param Transaction|null $cart
  193. *
  194. * @return float
  195. */
  196. public function getCartCredits(?Transaction $cart): float
  197. {
  198. $credits = 0.0;
  199. if ($cart && $cart->getDemandes()) {
  200. /** @var Demande $demande */
  201. foreach ($cart->getDemandes() as $demande) {
  202. $credits += $demande->getCredits();
  203. }
  204. }
  205. return $credits;
  206. }
  207. /**
  208. * @return Prestataire|null
  209. */
  210. public function getPrestataire(): ?Prestataire
  211. {
  212. /** @var User $user */
  213. $user = $this->getUser();
  214. if ($user && $user->isPrestataire()) {
  215. return $user->getPrestataire();
  216. }
  217. return null;
  218. }
  219. public function throw404(): Response
  220. {
  221. // throw new NotFoundHttpException();
  222. return $this->render('bundles/TwigBundle/Exception/error404.html.twig');
  223. }
  224. public function getDemandeForm(?Demande $demande = null, bool $preDemande = false, ?int $cid = null, ?bool $header = false)
  225. {
  226. $categories = $this->getCategorieRepository()->getCategorieTree();
  227. if (!$demande) {
  228. $demande = new Demande();
  229. }
  230. if ($cid) {
  231. $category = $this->getEm()->getRepository(Categorie::class)->find($cid);
  232. $demande->setCategorie($category);
  233. }
  234. $preForm = $this->createForm(DemandeType::class, $demande, array(
  235. 'action' => $this->generateUrl('public_demande_form'),
  236. 'em' => $this->entityManager,
  237. 'categories' => $categories,
  238. 'groups' => array_flip([
  239. 'diagnostics' => 72,
  240. 'expertises' => 73,
  241. 'etudes' => 74,
  242. 'solutions' => 75,
  243. 'energies' => 76
  244. ]),
  245. 'pre_demande' => $preDemande,
  246. 'header' => $header,
  247. ));
  248. return $preForm;
  249. }
  250. public function getOption(string $key): mixed
  251. {
  252. /** @var Options $options */
  253. $options = new Options($this->getEm());
  254. return $options->get($key);
  255. }
  256. public function setOption(string $key, mixed $value): void
  257. {
  258. /** @var Options $options */
  259. $options = new Options($this->getEm());
  260. $options->setOption($key, $value);
  261. }
  262. public function getOffset(?int $page, ?int $limit): ?int
  263. {
  264. if ($limit !== null) {
  265. $page = $page ?: 1;
  266. return ($page - 1) * $limit;
  267. }
  268. return null;
  269. }
  270. /**
  271. * @return Paginator
  272. */
  273. public function getPaginator()
  274. {
  275. return $this->get('knp_paginator');
  276. }
  277. /**
  278. * @param mixed $token
  279. */
  280. public function validateUserToken($token): User|false
  281. {
  282. if (!$token || !$token->getData()) {
  283. return false;
  284. }
  285. /** @var User|null $user */
  286. $user = $this->getEM()->getRepository(User::class)->find($token->getData());
  287. if (!$user) {
  288. return false;
  289. }
  290. return $user;
  291. }
  292. /**
  293. * Implémentation PHP de l'algorithme de Luhn basée sur la version XSLT officielle (https://xml.insee.fr/schema/siret.html#controles)
  294. * Les indices commencent à 1, les chiffres d'indice impair sont multipliés par 2
  295. */
  296. protected function isValidSiretChecksum(string $numero, int $somme = 0, int $indice = 1): bool
  297. {
  298. // Cas de base : plus de chiffres à traiter
  299. if (strlen($numero) === 0) {
  300. return ($somme % 10) === 0;
  301. }
  302. $premierChiffre = (int)substr($numero, 0, 1);
  303. $resteNumero = substr($numero, 1);
  304. if ($indice % 2 === 0) {
  305. // Cas des chiffres d'indice pair : ajouter directement
  306. return $this->isValidSiretChecksum($resteNumero, $somme + $premierChiffre, $indice + 1);
  307. } else {
  308. // Cas des chiffres d'indice impair : multiplier par 2 et additionner les chiffres
  309. $double = $premierChiffre * 2;
  310. $doubleStr = str_pad((string)$double, 2, '0', STR_PAD_LEFT);
  311. $sommeChiffres = (int)substr($doubleStr, 0, 1) + (int)substr($doubleStr, 1, 1);
  312. return $this->isValidSiretChecksum($resteNumero, $somme + $sommeChiffres, $indice + 1);
  313. }
  314. }
  315. }