Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

128 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, $newsletter, $subscribeNewsletter)
  86. {
  87. $currentMerchant = $this->merchantUtils->getMerchantCurrent();
  88. $newsletters = $currentMerchant->getNewsletters();
  89. foreach ($newsletters as $newsletterMerchant) {
  90. if ($newsletterMerchant->getId() == $newsletter->getId()) {
  91. if ($subscribeNewsletter) {
  92. $user->addNewsletter($newsletter);
  93. } else {
  94. $user->removeNewsletter($newsletter);
  95. }
  96. }
  97. }
  98. $this->em->persist($user);
  99. $this->em->flush();
  100. }
  101. public function isSubscribedToNewsletter($user, $newsletter)
  102. {
  103. return $user->getNewsletters()->contains($newsletter);
  104. }
  105. }