Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

112 Zeilen
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. 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. }