<?php

namespace Lc\CaracoleBundle\Resolver;

use Lc\CaracoleBundle\Repository\User\VisitorStore;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\RequestStack;

class VisitorResolver
{
    protected array $visitor;
    protected RequestStack $requestStack;
    protected ParameterBagInterface $parameterBag;
    protected VisitorStore $visitorStore;

    public function __construct(
            RequestStack $requestStack,
            ParameterBagInterface $parameterBag,
            VisitorStore $visitorStore
    ) {
        $this->requestStack = $requestStack;
        $this->parameterBag = $parameterBag;
        $this->visitorStore = $visitorStore;
    }

    // getVisitorCurrent
    public function getCurrent()
    {
        $cookie = $this->requestStack->getCurrentRequest()->cookies->get(
                $this->parameterBag->get('app.cookie_name_visitor')
        );
        return $this->getVisitor($cookie);
    }

    private function getVisitor($cookie)
    {
        if (!isset($this->visitor[$cookie])) {
            $this->visitor[$cookie] = $this->visitorStore->getOneByCookie($cookie);
        }

        return $this->visitor[$cookie];
    }

}