Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

pirms 3 gadiem
pirms 3 gadiem
pirms 3 gadiem
pirms 4 gadiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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(isset($newsletters[0]) && $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. }