Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

UserUtils.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace Lc\ShopBundle\Services ;
  3. use ConnectHolland\CookieConsentBundle\Cookie\CookieChecker;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Lc\ShopBundle\Context\MerchantUtilsInterface;
  6. use Symfony\Component\HttpFoundation\Cookie ;
  7. use Lc\ShopBundle\Context\VisitorInterface;
  8. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. use Symfony\Component\Security\Core\Security;
  11. class UserUtils
  12. {
  13. protected $parameterBag ;
  14. protected $em ;
  15. protected $utils ;
  16. protected $requestStack ;
  17. protected $visitorRepository ;
  18. protected $merchantUtils ;
  19. protected $cookieChecker ;
  20. public function __construct(ParameterBagInterface $parameterBag, EntityManagerInterface $em, Utils $utils,
  21. RequestStack $requestStack, MerchantUtilsInterface $merchantUtils, CookieChecker $cookieChecker,
  22. Security $security)
  23. {
  24. $this->em = $em ;
  25. $this->parameterBag = $parameterBag ;
  26. $this->utils = $utils ;
  27. $this->requestStack = $requestStack ;
  28. $this->visitorRepository = $this->em->getRepository($this->em->getClassMetadata(VisitorInterface::class)->getName()) ;
  29. $this->merchantUtils = $merchantUtils ;
  30. $this->cookieChecker = $cookieChecker ;
  31. $this->security = $security ;
  32. }
  33. public function getCookieNameVisitor()
  34. {
  35. return $this->parameterBag->get('app.cookie_name_visitor') ;
  36. }
  37. public function cryptCookie($data)
  38. {
  39. return base64_encode($data);
  40. }
  41. public function decryptCookie($data)
  42. {
  43. return base64_decode($data);
  44. }
  45. public function setCookieVisitor($response, $cookie)
  46. {
  47. $response->headers->setCookie(Cookie::create($this->getCookieNameVisitor(), $this->cryptCookie($cookie), 0, '/', $this->utils->getCookieDomain()));
  48. }
  49. public function getVisitor($cookie)
  50. {
  51. return $this->visitorRepository->findOneBy(['cookie' => $cookie]) ;
  52. }
  53. public function getVisitorCurrent()
  54. {
  55. $cookie = $this->requestStack->getCurrentRequest()->cookies->get($this->getCookieNameVisitor()) ;
  56. return $this->getVisitor($cookie) ;
  57. }
  58. public function addVisitor($cookie, $ip)
  59. {
  60. $classVisitor = $this->em->getClassMetadata(VisitorInterface::class)->getName() ;
  61. $visitor = new $classVisitor ;
  62. $visitor->setCookie($cookie) ;
  63. $visitor->setIp($ip) ;
  64. $visitor->setTotalVisit(1) ;
  65. $visitor->setLastAccess(new \DateTime()) ;
  66. $this->em->persist($visitor);
  67. $this->em->flush() ;
  68. }
  69. public function updateVisitor($visitor)
  70. {
  71. $totalVisit = $visitor->getTotalVisit() + 1 ;
  72. $visitor->setTotalVisit($totalVisit) ;
  73. $visitor->setLastAccess(new \DateTime()) ;
  74. $this->em->persist($visitor);
  75. $this->em->flush() ;
  76. }
  77. public function setNewsletter($user, $subscribeNewsletter)
  78. {
  79. $currentMerchant = $this->merchantUtils->getMerchantCurrent() ;
  80. $newsletters = $currentMerchant->getNewsletters() ;
  81. if(isset($newsletters[0]) && $newsletters[0]) {
  82. if($subscribeNewsletter) {
  83. $user->addNewsletter($newsletters[0]) ;
  84. }
  85. else {
  86. $user->removeNewsletter($newsletters[0]) ;
  87. }
  88. }
  89. $this->em->persist($user) ;
  90. $this->em->flush() ;
  91. }
  92. }