src/Controller/AbstractController.php line 233

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 Demande $demande
  151. * @param Prestataire $prestataire
  152. */
  153. public function removeFromPrestaCart(Demande $demande, Prestataire $prestataire): void
  154. {
  155. /** @var TransactionRepository $repo */
  156. $repo = $this->getEM()->getRepository(Transaction::class);
  157. // remove demande from current prestataire cart
  158. $cart = $repo->getCart($prestataire->getId());
  159. if ($cart) {
  160. $cart->removeDemande($demande);
  161. }
  162. $this->getEM()->flush();
  163. }
  164. /**
  165. * @param Transaction|null $cart
  166. *
  167. * @return float
  168. */
  169. public function getCartPrix(?Transaction $cart): float
  170. {
  171. $prix = 0.0;
  172. if ($cart && $cart->getDemandes()) {
  173. /** @var Demande $demande */
  174. foreach ($cart->getDemandes() as $demande) {
  175. $prix += $demande->getPrix();
  176. }
  177. }
  178. return $prix;
  179. }
  180. /**
  181. * @param Transaction|null $cart
  182. *
  183. * @return float
  184. */
  185. public function getCartCredits(?Transaction $cart): float
  186. {
  187. $credits = 0.0;
  188. if ($cart && $cart->getDemandes()) {
  189. /** @var Demande $demande */
  190. foreach ($cart->getDemandes() as $demande) {
  191. $credits += $demande->getCredits();
  192. }
  193. }
  194. return $credits;
  195. }
  196. /**
  197. * @return Prestataire|null
  198. */
  199. public function getPrestataire(): ?Prestataire
  200. {
  201. /** @var User $user */
  202. $user = $this->getUser();
  203. if ($user && $user->isPrestataire()) {
  204. return $user->getPrestataire();
  205. }
  206. return null;
  207. }
  208. public function throw404(): Response
  209. {
  210. // throw new NotFoundHttpException();
  211. return $this->render('bundles/TwigBundle/Exception/error404.html.twig');
  212. }
  213. public function getDemandeForm(?Demande $demande = null, bool $preDemande = false, ?int $cid = null, ?bool $header = false)
  214. {
  215. $categories = $this->getCategorieRepository()->getCategorieTree();
  216. if (!$demande) {
  217. $demande = new Demande();
  218. }
  219. if ($cid) {
  220. $category = $this->getEm()->getRepository(Categorie::class)->find($cid);
  221. $demande->setCategorie($category);
  222. }
  223. $preForm = $this->createForm(DemandeType::class, $demande, array(
  224. 'action' => $this->generateUrl('public_demande_form'),
  225. 'em' => $this->entityManager,
  226. 'categories' => $categories,
  227. 'groups' => array_flip([
  228. 'diagnostics' => 72,
  229. 'expertises' => 73,
  230. 'etudes' => 74,
  231. 'solutions' => 75,
  232. 'energies' => 76
  233. ]),
  234. 'pre_demande' => $preDemande,
  235. 'header' => $header,
  236. ));
  237. return $preForm;
  238. }
  239. public function getOption(string $key): mixed
  240. {
  241. /** @var Options $options */
  242. $options = new Options($this->getEm());
  243. return $options->get($key);
  244. }
  245. public function setOption(string $key, mixed $value): void
  246. {
  247. /** @var Options $options */
  248. $options = new Options($this->getEm());
  249. $options->setOption($key, $value);
  250. }
  251. public function getOffset(?int $page, ?int $limit): ?int
  252. {
  253. if ($limit !== null) {
  254. $page = $page ?: 1;
  255. return ($page - 1) * $limit;
  256. }
  257. return null;
  258. }
  259. /**
  260. * @return Paginator
  261. */
  262. public function getPaginator()
  263. {
  264. return $this->get('knp_paginator');
  265. }
  266. /**
  267. * @param mixed $token
  268. */
  269. public function validateUserToken($token): User|false
  270. {
  271. if (!$token || !$token->getData()) {
  272. return false;
  273. }
  274. /** @var User|null $user */
  275. $user = $this->getEM()->getRepository(User::class)->find($token->getData());
  276. if (!$user) {
  277. return false;
  278. }
  279. return $user;
  280. }
  281. /**
  282. * Implémentation PHP de l'algorithme de Luhn basée sur la version XSLT officielle (https://xml.insee.fr/schema/siret.html#controles)
  283. * Les indices commencent à 1, les chiffres d'indice impair sont multipliés par 2
  284. */
  285. protected function isValidSiretChecksum(string $numero, int $somme = 0, int $indice = 1): bool
  286. {
  287. // Cas de base : plus de chiffres à traiter
  288. if (strlen($numero) === 0) {
  289. return ($somme % 10) === 0;
  290. }
  291. $premierChiffre = (int)substr($numero, 0, 1);
  292. $resteNumero = substr($numero, 1);
  293. if ($indice % 2 === 0) {
  294. // Cas des chiffres d'indice pair : ajouter directement
  295. return $this->isValidSiretChecksum($resteNumero, $somme + $premierChiffre, $indice + 1);
  296. } else {
  297. // Cas des chiffres d'indice impair : multiplier par 2 et additionner les chiffres
  298. $double = $premierChiffre * 2;
  299. $doubleStr = str_pad((string)$double, 2, '0', STR_PAD_LEFT);
  300. $sommeChiffres = (int)substr($doubleStr, 0, 1) + (int)substr($doubleStr, 1, 1);
  301. return $this->isValidSiretChecksum($resteNumero, $somme + $sommeChiffres, $indice + 1);
  302. }
  303. }
  304. }