|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- <?php
-
- namespace Lc\CaracoleBundle\Builder\User;
-
- use Doctrine\ORM\EntityManagerInterface;
- use Lc\CaracoleBundle\Factory\User\VisitorFactory;
- use Lc\CaracoleBundle\Model\User\VisitorInterface;
- use Lc\SovBundle\Component\CookieComponent;
- use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
- use Symfony\Component\HttpFoundation\Cookie;
-
- class VisitorBuilder
- {
- protected CookieComponent $cookieComponent;
- protected ParameterBagInterface $parameterBag;
- protected EntityManagerInterface $entityManager;
-
- public function __construct(
- EntityManagerInterface $entityManager,
- CookieComponent $cookieComponent,
- ParameterBagInterface $parameterBag
- )
- {
- $this->entityManager = $entityManager;
- $this->cookieComponent = $cookieComponent;
- $this->parameterBag = $parameterBag;
- }
-
- // addVisitor
- public function create(string $cookieValue, string $ip)
- {
- $visitorFactory = new VisitorFactory();
- $visitor = $visitorFactory->create($cookieValue, $ip);
-
- $this->entityManager->create($visitor);
- $this->entityManager->flush();
- }
-
- public function update(VisitorInterface $visitor)
- {
- if($visitor->getLastAccess() < new \DateTime('-1 hour')) {
- $totalVisit = $visitor->getTotalVisit() + 1;
- $visitor->setTotalVisit($totalVisit);
- }
-
- $visitor->setLastAccess(new \DateTime());
-
- // Erreur : The EntityManager is closed. #713
- if($this->entityManager->isOpen()) {
- $this->entityManager->update($visitor);
- $this->entityManager->flush();
- }
- }
-
- // setCookieVisitor
- public function setCookie($response, $cookieValue): Cookie
- {
- $cookie = Cookie::create(
- $this->parameterBag->get('app.cookie_name_visitor'),
- $this->cookieComponent->cryptCookie($cookieValue),
- new \DateTime('+2 months'),
- '/',
- $this->cookieComponent->getCookieDomain()
- );
-
- $response->headers->setCookie($cookie);
-
- return $cookie;
- }
-
- // updateVisitorCookie
- public function updateCookie($response): void
- {
- $response->headers->setCookie(
- Cookie::create(
- $this->parameterBag->get('app.cookie_name_visitor'),
- $this->cookieComponent->cryptCookie($this->getVisitorCurrent()->getCookie()),
- new \DateTime('+2 months'),
- '/',
- $this->cookieComponent->getCookieDomain()
- )
- );
- }
-
- }
|