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.

147 lines
4.6KB

  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. protected $visitor;
  21. public function __construct(ParameterBagInterface $parameterBag, EntityManagerInterface $em, Utils $utils,
  22. RequestStack $requestStack, MerchantUtilsInterface $merchantUtils, CookieChecker $cookieChecker,
  23. Security $security)
  24. {
  25. $this->em = $em;
  26. $this->parameterBag = $parameterBag;
  27. $this->utils = $utils;
  28. $this->requestStack = $requestStack;
  29. $this->visitorRepository = $this->em->getRepository($this->em->getClassMetadata(VisitorInterface::class)->getName());
  30. $this->merchantUtils = $merchantUtils;
  31. $this->cookieChecker = $cookieChecker;
  32. $this->security = $security;
  33. }
  34. public function getCookieNameVisitor()
  35. {
  36. return $this->parameterBag->get('app.cookie_name_visitor');
  37. }
  38. public function cryptCookie($data)
  39. {
  40. return base64_encode($data);
  41. }
  42. public function decryptCookie($data)
  43. {
  44. return base64_decode($data);
  45. }
  46. public function setCookieVisitor($response, $cookie)
  47. {
  48. $response->headers->setCookie(Cookie::create($this->getCookieNameVisitor(), $this->cryptCookie($cookie), new \DateTime('+2 months'), '/', $this->utils->getCookieDomain()));
  49. }
  50. public function updateVisitorCookie($response)
  51. {
  52. $response->headers->setCookie(Cookie::create($this->getCookieNameVisitor(), $this->cryptCookie($this->getVisitorCurrent()->getCookie()), new \DateTime('+2 months'), '/', $this->utils->getCookieDomain()));
  53. }
  54. public function getVisitor($cookie)
  55. {
  56. if (!isset($this->visitor[$cookie])) {
  57. $this->visitor[$cookie] = $this->visitorRepository->findOneBy(['cookie' => $cookie]);
  58. }
  59. return $this->visitor[$cookie];
  60. }
  61. public function getVisitorCurrent()
  62. {
  63. $cookie = $this->requestStack->getCurrentRequest()->cookies->get($this->getCookieNameVisitor());
  64. return $this->getVisitor($cookie);
  65. }
  66. public function addVisitor($cookie, $ip)
  67. {
  68. $classVisitor = $this->em->getClassMetadata(VisitorInterface::class)->getName();
  69. $visitor = new $classVisitor;
  70. $visitor->setCookie($cookie);
  71. $visitor->setIp($ip);
  72. $visitor->setTotalVisit(1);
  73. $visitor->setLastAccess(new \DateTime());
  74. $this->em->persist($visitor);
  75. $this->em->flush();
  76. }
  77. public function updateVisitor($visitor)
  78. {
  79. $totalVisit = $visitor->getTotalVisit() + 1;
  80. $visitor->setTotalVisit($totalVisit);
  81. $visitor->setLastAccess(new \DateTime());
  82. $this->em->persist($visitor);
  83. $this->em->flush();
  84. }
  85. public function setNewsletter($user, $subscribeNewsletter)
  86. {
  87. $currentMerchant = $this->merchantUtils->getMerchantCurrent();
  88. $newsletters = $currentMerchant->getNewsletters();
  89. if($newsletters && count($newsletters) > 0) {
  90. if ($subscribeNewsletter) {
  91. $user->addNewsletter($newsletters[0]);
  92. }
  93. else {
  94. $user->removeNewsletter($newsletters[0]);
  95. }
  96. }
  97. $this->em->persist($user);
  98. $this->em->flush();
  99. }
  100. /*public function setNewsletter($user, $newsletter, $subscribeNewsletter)
  101. {
  102. $currentMerchant = $this->merchantUtils->getMerchantCurrent() ;
  103. $newsletters = $currentMerchant->getNewsletters() ;
  104. foreach($newsletters as $newsletterMerchant) {
  105. if($newsletterMerchant == $newsletter) {
  106. if($subscribeNewsletter) {
  107. $user->addNewsletter($newsletter) ;
  108. }
  109. else {
  110. $user->removeNewsletter($newsletter) ;
  111. }
  112. }
  113. }
  114. $this->em->persist($user) ;
  115. $this->em->flush() ;
  116. }*/
  117. public function isSubscribedToNewsletter($user, $newsletter)
  118. {
  119. return $user->getNewsletters()->contains($newsletter);
  120. }
  121. }