您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

VisitorBuilder.php 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. }