|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- <?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 $cookie, string $ip)
- {
- $visitorFactory = new VisitorFactory();
- $visitor = $visitorFactory->create($cookie, $ip);
-
- $this->entityManager->persist($visitor);
- $this->entityManager->flush();
- }
-
- public function update(VisitorInterface $visitor)
- {
- $totalVisit = $visitor->getTotalVisit() + 1;
-
- $visitor->setTotalVisit($totalVisit);
- $visitor->setLastAccess(new \DateTime());
-
- $this->entityManager->persist($visitor);
- $this->entityManager->flush();
- }
-
- // setCookieVisitor
- public function setCookie($response, $cookie)
- {
- $response->headers->setCookie(
- Cookie::create(
- $this->parameterBag->get('app.cookie_name_visitor'),
- $this->cookieComponent->cryptCookie($cookie),
- new \DateTime('+2 months'),
- '/',
- $this->cookieComponent->getCookieDomain()
- )
- );
- }
-
- // updateVisitorCookie
- public function updateCookie($response)
- {
- $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()
- )
- );
- }
-
- }
|