You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.2KB

  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. public function getCurrent()
  22. {
  23. $cookie = $this->requestStack->getCurrentRequest()->cookies->get(
  24. $this->parameterBag->get('app.cookie_name_visitor')
  25. );
  26. return $this->getVisitor($cookie);
  27. }
  28. private function getVisitor($cookie)
  29. {
  30. if (!isset($this->visitor[$cookie])) {
  31. $this->visitor[$cookie] = $this->visitorStore->getOneByCookie($cookie);
  32. }
  33. return $this->visitor[$cookie];
  34. }
  35. }