|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- <?php
-
- namespace Lc\ShopBundle\Services ;
-
- use Doctrine\ORM\EntityManagerInterface;
- use Symfony\Component\HttpFoundation\Cookie ;
- use Lc\ShopBundle\Context\VisitorInterface;
- use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
- use Symfony\Component\HttpFoundation\RequestStack;
-
- class UserUtils
- {
- protected $parameterBag ;
- protected $em ;
- protected $utils ;
- protected $requestStack ;
- protected $visitorRepository ;
-
- public function __construct(ParameterBagInterface $parameterBag, EntityManagerInterface $em, Utils $utils, RequestStack $requestStack)
- {
- $this->em = $em ;
- $this->parameterBag = $parameterBag ;
- $this->utils = $utils ;
- $this->requestStack = $requestStack ;
- $this->visitorRepository = $this->em->getRepository($this->em->getClassMetadata(VisitorInterface::class)->getName()) ;
- }
-
- 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), 0, '/', $this->utils->getCookieDomain()));
- }
-
- public function getVisitor($cookie)
- {
- return $this->visitorRepository->findOneBy(['cookie' => $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() ;
- }
-
- }
|