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.

83 lines
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. {
  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. $this->entityManager->update($visitor);
  40. $this->entityManager->flush();
  41. }
  42. // setCookieVisitor
  43. public function setCookie($response, $cookieValue): Cookie
  44. {
  45. $cookie = Cookie::create(
  46. $this->parameterBag->get('app.cookie_name_visitor'),
  47. $this->cookieComponent->cryptCookie($cookieValue),
  48. new \DateTime('+2 months'),
  49. '/',
  50. $this->cookieComponent->getCookieDomain()
  51. );
  52. $response->headers->setCookie($cookie);
  53. return $cookie;
  54. }
  55. // updateVisitorCookie
  56. public function updateCookie($response): void
  57. {
  58. $response->headers->setCookie(
  59. Cookie::create(
  60. $this->parameterBag->get('app.cookie_name_visitor'),
  61. $this->cookieComponent->cryptCookie($this->getVisitorCurrent()->getCookie()),
  62. new \DateTime('+2 months'),
  63. '/',
  64. $this->cookieComponent->getCookieDomain()
  65. )
  66. );
  67. }
  68. }