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.

110 lines
3.9KB

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