VisitorResolver.php 1.2KB

3 jaren geleden
3 jaren geleden
3 jaren geleden
1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace Lc\CaracoleBundle\Resolver;
  3. use Lc\CaracoleBundle\Repository\User\VisitorStore;
  4. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  5. use Symfony\Component\HttpFoundation\RequestStack;
  6. class VisitorResolver
  7. {
  8. protected array $visitor;
  9. protected RequestStack $requestStack;
  10. protected ParameterBagInterface $parameterBag;
  11. protected VisitorStore $visitorStore;
  12. public function __construct(
  13. RequestStack $requestStack,
  14. ParameterBagInterface $parameterBag,
  15. VisitorStore $visitorStore
  16. ) {
  17. $this->requestStack = $requestStack;
  18. $this->parameterBag = $parameterBag;
  19. $this->visitorStore = $visitorStore;
  20. }
  21. // getVisitorCurrent
  22. public function getCurrent()
  23. {
  24. $cookie = $this->requestStack->getCurrentRequest()->cookies->get(
  25. $this->parameterBag->get('app.cookie_name_visitor')
  26. );
  27. return $this->getVisitor($cookie);
  28. }
  29. private function getVisitor($cookie)
  30. {
  31. if (!isset($this->visitor[$cookie])) {
  32. $this->visitor[$cookie] = $this->visitorStore->getOneByCookie($cookie);
  33. }
  34. return $this->visitor[$cookie];
  35. }
  36. }