You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 line
2.4KB

  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. $this->entityManager = $entityManager;
  20. $this->cookieComponent = $cookieComponent;
  21. $this->parameterBag = $parameterBag;
  22. }
  23. // addVisitor
  24. public function create(string $cookie, string $ip)
  25. {
  26. $visitorFactory = new VisitorFactory();
  27. $visitor = $visitorFactory->create($cookie, $ip);
  28. $this->entityManager->create($visitor);
  29. $this->entityManager->flush();
  30. }
  31. public function update(VisitorInterface $visitor)
  32. {
  33. $totalVisit = $visitor->getTotalVisit() + 1;
  34. $visitor->setTotalVisit($totalVisit);
  35. $visitor->setLastAccess(new \DateTime());
  36. $this->entityManager->update($visitor);
  37. $this->entityManager->flush();
  38. }
  39. // setCookieVisitor
  40. public function setCookie($response, $cookie): void
  41. {
  42. dump('NICICIIC');
  43. $response->headers->setCookie(
  44. Cookie::create(
  45. $this->parameterBag->get('app.cookie_name_visitor'),
  46. $this->cookieComponent->cryptCookie($cookie),
  47. new \DateTime('+2 months'),
  48. '/',
  49. $this->cookieComponent->getCookieDomain()
  50. )
  51. );
  52. }
  53. // updateVisitorCookie
  54. public function updateCookie($response): void
  55. {
  56. $response->headers->setCookie(
  57. Cookie::create(
  58. $this->parameterBag->get('app.cookie_name_visitor'),
  59. $this->cookieComponent->cryptCookie($this->getVisitorCurrent()->getCookie()),
  60. new \DateTime('+2 months'),
  61. '/',
  62. $this->cookieComponent->getCookieDomain()
  63. )
  64. );
  65. }
  66. }