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.

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