Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

86 lines
2.5KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Builder\User;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lc\CaracoleBundle\Factory\User\VisitorFactory;
  5. use Lc\CaracoleBundle\Model\User\VisitorInterface;
  6. use Lc\SovBundle\Component\CookieComponent;
  7. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  8. use Symfony\Component\HttpFoundation\Cookie;
  9. class VisitorBuilder
  10. {
  11. protected CookieComponent $cookieComponent;
  12. protected ParameterBagInterface $parameterBag;
  13. protected EntityManagerInterface $entityManager;
  14. public function __construct(
  15. EntityManagerInterface $entityManager,
  16. CookieComponent $cookieComponent,
  17. ParameterBagInterface $parameterBag
  18. )
  19. {
  20. $this->entityManager = $entityManager;
  21. $this->cookieComponent = $cookieComponent;
  22. $this->parameterBag = $parameterBag;
  23. }
  24. // addVisitor
  25. public function create(string $cookieValue, string $ip)
  26. {
  27. $visitorFactory = new VisitorFactory();
  28. $visitor = $visitorFactory->create($cookieValue, $ip);
  29. $this->entityManager->create($visitor);
  30. $this->entityManager->flush();
  31. }
  32. public function update(VisitorInterface $visitor)
  33. {
  34. if($visitor->getLastAccess() < new \DateTime('-1 hour')) {
  35. $totalVisit = $visitor->getTotalVisit() + 1;
  36. $visitor->setTotalVisit($totalVisit);
  37. }
  38. $visitor->setLastAccess(new \DateTime());
  39. // Erreur : The EntityManager is closed. #713
  40. if($this->entityManager->isOpen()) {
  41. $this->entityManager->update($visitor);
  42. $this->entityManager->flush();
  43. }
  44. }
  45. // setCookieVisitor
  46. public function setCookie($response, $cookieValue): Cookie
  47. {
  48. $cookie = Cookie::create(
  49. $this->parameterBag->get('app.cookie_name_visitor'),
  50. $this->cookieComponent->cryptCookie($cookieValue),
  51. new \DateTime('+2 months'),
  52. '/',
  53. $this->cookieComponent->getCookieDomain()
  54. );
  55. $response->headers->setCookie($cookie);
  56. return $cookie;
  57. }
  58. // updateVisitorCookie
  59. public function updateCookie($response): void
  60. {
  61. $response->headers->setCookie(
  62. Cookie::create(
  63. $this->parameterBag->get('app.cookie_name_visitor'),
  64. $this->cookieComponent->cryptCookie($this->getVisitorCurrent()->getCookie()),
  65. new \DateTime('+2 months'),
  66. '/',
  67. $this->cookieComponent->getCookieDomain()
  68. )
  69. );
  70. }
  71. }