|
|
@@ -1,11 +1,11 @@ |
|
|
|
<?php |
|
|
|
|
|
|
|
namespace Lc\ShopBundle\Services ; |
|
|
|
namespace Lc\ShopBundle\Services; |
|
|
|
|
|
|
|
use ConnectHolland\CookieConsentBundle\Cookie\CookieChecker; |
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
|
|
use Lc\ShopBundle\Context\MerchantUtilsInterface; |
|
|
|
use Symfony\Component\HttpFoundation\Cookie ; |
|
|
|
use Symfony\Component\HttpFoundation\Cookie; |
|
|
|
use Lc\ShopBundle\Context\VisitorInterface; |
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; |
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
|
@@ -13,108 +13,134 @@ use Symfony\Component\Security\Core\Security; |
|
|
|
|
|
|
|
class UserUtils |
|
|
|
{ |
|
|
|
protected $parameterBag ; |
|
|
|
protected $em ; |
|
|
|
protected $utils ; |
|
|
|
protected $requestStack ; |
|
|
|
protected $visitorRepository ; |
|
|
|
protected $merchantUtils ; |
|
|
|
protected $cookieChecker ; |
|
|
|
protected $visitor ; |
|
|
|
|
|
|
|
public function __construct(ParameterBagInterface $parameterBag, EntityManagerInterface $em, Utils $utils, |
|
|
|
RequestStack $requestStack, MerchantUtilsInterface $merchantUtils, CookieChecker $cookieChecker, |
|
|
|
Security $security) |
|
|
|
{ |
|
|
|
$this->em = $em ; |
|
|
|
$this->parameterBag = $parameterBag ; |
|
|
|
$this->utils = $utils ; |
|
|
|
$this->requestStack = $requestStack ; |
|
|
|
$this->visitorRepository = $this->em->getRepository($this->em->getClassMetadata(VisitorInterface::class)->getName()) ; |
|
|
|
$this->merchantUtils = $merchantUtils ; |
|
|
|
$this->cookieChecker = $cookieChecker ; |
|
|
|
$this->security = $security ; |
|
|
|
protected $parameterBag; |
|
|
|
protected $em; |
|
|
|
protected $utils; |
|
|
|
protected $requestStack; |
|
|
|
protected $visitorRepository; |
|
|
|
protected $merchantUtils; |
|
|
|
protected $cookieChecker; |
|
|
|
protected $visitor; |
|
|
|
|
|
|
|
public function __construct(ParameterBagInterface $parameterBag, EntityManagerInterface $em, Utils $utils, |
|
|
|
RequestStack $requestStack, MerchantUtilsInterface $merchantUtils, CookieChecker $cookieChecker, |
|
|
|
Security $security) |
|
|
|
{ |
|
|
|
$this->em = $em; |
|
|
|
$this->parameterBag = $parameterBag; |
|
|
|
$this->utils = $utils; |
|
|
|
$this->requestStack = $requestStack; |
|
|
|
$this->visitorRepository = $this->em->getRepository($this->em->getClassMetadata(VisitorInterface::class)->getName()); |
|
|
|
$this->merchantUtils = $merchantUtils; |
|
|
|
$this->cookieChecker = $cookieChecker; |
|
|
|
$this->security = $security; |
|
|
|
} |
|
|
|
|
|
|
|
public function getCookieNameVisitor() |
|
|
|
{ |
|
|
|
return $this->parameterBag->get('app.cookie_name_visitor'); |
|
|
|
} |
|
|
|
|
|
|
|
public function cryptCookie($data) |
|
|
|
{ |
|
|
|
return base64_encode($data); |
|
|
|
} |
|
|
|
|
|
|
|
public function decryptCookie($data) |
|
|
|
{ |
|
|
|
return base64_decode($data); |
|
|
|
} |
|
|
|
|
|
|
|
public function setCookieVisitor($response, $cookie) |
|
|
|
{ |
|
|
|
$response->headers->setCookie(Cookie::create($this->getCookieNameVisitor(), $this->cryptCookie($cookie), new \DateTime('+2 months'), '/', $this->utils->getCookieDomain())); |
|
|
|
} |
|
|
|
|
|
|
|
public function updateVisitorCookie($response) |
|
|
|
{ |
|
|
|
$response->headers->setCookie(Cookie::create($this->getCookieNameVisitor(), $this->cryptCookie($this->getVisitorCurrent()->getCookie()), new \DateTime('+2 months'), '/', $this->utils->getCookieDomain())); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public function getVisitor($cookie) |
|
|
|
{ |
|
|
|
if (!isset($this->visitor[$cookie])) { |
|
|
|
$this->visitor[$cookie] = $this->visitorRepository->findOneBy(['cookie' => $cookie]); |
|
|
|
} |
|
|
|
|
|
|
|
public function getCookieNameVisitor() |
|
|
|
{ |
|
|
|
return $this->parameterBag->get('app.cookie_name_visitor') ; |
|
|
|
} |
|
|
|
|
|
|
|
public function cryptCookie($data) |
|
|
|
{ |
|
|
|
return base64_encode($data); |
|
|
|
return $this->visitor[$cookie]; |
|
|
|
} |
|
|
|
|
|
|
|
public function getVisitorCurrent() |
|
|
|
{ |
|
|
|
$cookie = $this->requestStack->getCurrentRequest()->cookies->get($this->getCookieNameVisitor()); |
|
|
|
return $this->getVisitor($cookie); |
|
|
|
} |
|
|
|
|
|
|
|
public function addVisitor($cookie, $ip) |
|
|
|
{ |
|
|
|
$classVisitor = $this->em->getClassMetadata(VisitorInterface::class)->getName(); |
|
|
|
$visitor = new $classVisitor; |
|
|
|
|
|
|
|
$visitor->setCookie($cookie); |
|
|
|
$visitor->setIp($ip); |
|
|
|
$visitor->setTotalVisit(1); |
|
|
|
$visitor->setLastAccess(new \DateTime()); |
|
|
|
|
|
|
|
$this->em->persist($visitor); |
|
|
|
$this->em->flush(); |
|
|
|
} |
|
|
|
|
|
|
|
public function updateVisitor($visitor) |
|
|
|
{ |
|
|
|
$totalVisit = $visitor->getTotalVisit() + 1; |
|
|
|
$visitor->setTotalVisit($totalVisit); |
|
|
|
$visitor->setLastAccess(new \DateTime()); |
|
|
|
|
|
|
|
$this->em->persist($visitor); |
|
|
|
$this->em->flush(); |
|
|
|
} |
|
|
|
|
|
|
|
public function setNewsletter($user, $subscribeNewsletter) |
|
|
|
{ |
|
|
|
$currentMerchant = $this->merchantUtils->getMerchantCurrent(); |
|
|
|
$newsletters = $currentMerchant->getNewsletters(); |
|
|
|
|
|
|
|
if($newsletters && count($newsletters) > 0) { |
|
|
|
if ($subscribeNewsletter) { |
|
|
|
$user->addNewsletter($newsletters[0]); |
|
|
|
} |
|
|
|
else { |
|
|
|
$user->removeNewsletter($newsletters[0]); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public function decryptCookie($data) |
|
|
|
{ |
|
|
|
return base64_decode($data); |
|
|
|
} |
|
|
|
|
|
|
|
public function setCookieVisitor($response, $cookie) |
|
|
|
{ |
|
|
|
$response->headers->setCookie(Cookie::create($this->getCookieNameVisitor(), $this->cryptCookie($cookie), new \DateTime('+2 months'), '/', $this->utils->getCookieDomain())); |
|
|
|
} |
|
|
|
public function updateVisitorCookie($response) |
|
|
|
{ |
|
|
|
$response->headers->setCookie(Cookie::create($this->getCookieNameVisitor(), $this->cryptCookie($this->getVisitorCurrent()->getCookie()), new \DateTime('+2 months'), '/', $this->utils->getCookieDomain())); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public function getVisitor($cookie) |
|
|
|
{ |
|
|
|
if(!isset($this->visitor[$cookie])){ |
|
|
|
$this->visitor[$cookie] = $this->visitorRepository->findOneBy(['cookie' => $cookie]) ; |
|
|
|
$this->em->persist($user); |
|
|
|
$this->em->flush(); |
|
|
|
} |
|
|
|
|
|
|
|
/*public function setNewsletter($user, $newsletter, $subscribeNewsletter) |
|
|
|
{ |
|
|
|
$currentMerchant = $this->merchantUtils->getMerchantCurrent() ; |
|
|
|
$newsletters = $currentMerchant->getNewsletters() ; |
|
|
|
|
|
|
|
foreach($newsletters as $newsletterMerchant) { |
|
|
|
if($newsletterMerchant == $newsletter) { |
|
|
|
if($subscribeNewsletter) { |
|
|
|
$user->addNewsletter($newsletter) ; |
|
|
|
} |
|
|
|
else { |
|
|
|
$user->removeNewsletter($newsletter) ; |
|
|
|
} |
|
|
|
} |
|
|
|
return $this->visitor[$cookie]; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public function getVisitorCurrent() |
|
|
|
{ |
|
|
|
$cookie = $this->requestStack->getCurrentRequest()->cookies->get($this->getCookieNameVisitor()) ; |
|
|
|
return $this->getVisitor($cookie) ; |
|
|
|
} |
|
|
|
$this->em->persist($user) ; |
|
|
|
$this->em->flush() ; |
|
|
|
}*/ |
|
|
|
|
|
|
|
public function addVisitor($cookie, $ip) |
|
|
|
{ |
|
|
|
$classVisitor = $this->em->getClassMetadata(VisitorInterface::class)->getName() ; |
|
|
|
$visitor = new $classVisitor ; |
|
|
|
|
|
|
|
$visitor->setCookie($cookie) ; |
|
|
|
$visitor->setIp($ip) ; |
|
|
|
$visitor->setTotalVisit(1) ; |
|
|
|
$visitor->setLastAccess(new \DateTime()) ; |
|
|
|
|
|
|
|
$this->em->persist($visitor); |
|
|
|
$this->em->flush() ; |
|
|
|
} |
|
|
|
|
|
|
|
public function updateVisitor($visitor) |
|
|
|
{ |
|
|
|
$totalVisit = $visitor->getTotalVisit() + 1 ; |
|
|
|
$visitor->setTotalVisit($totalVisit) ; |
|
|
|
$visitor->setLastAccess(new \DateTime()) ; |
|
|
|
|
|
|
|
$this->em->persist($visitor); |
|
|
|
$this->em->flush() ; |
|
|
|
} |
|
|
|
|
|
|
|
public function setNewsletter($user, $subscribeNewsletter) |
|
|
|
{ |
|
|
|
$currentMerchant = $this->merchantUtils->getMerchantCurrent() ; |
|
|
|
$newsletters = $currentMerchant->getNewsletters() ; |
|
|
|
|
|
|
|
if(isset($newsletters[0]) && $newsletters[0]) { |
|
|
|
if($subscribeNewsletter) { |
|
|
|
$user->addNewsletter($newsletters[0]) ; |
|
|
|
} |
|
|
|
else { |
|
|
|
$user->removeNewsletter($newsletters[0]) ; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
$this->em->persist($user) ; |
|
|
|
$this->em->flush() ; |
|
|
|
} |
|
|
|
public function isSubscribedToNewsletter($user, $newsletter) |
|
|
|
{ |
|
|
|
return $user->getNewsletters()->contains($newsletter); |
|
|
|
} |
|
|
|
|
|
|
|
} |