Sonata\Component\Basket\BasketEntityFactory::load PHP Méthode

load() public méthode

public load ( Sonata\Component\Customer\CustomerInterface $customer )
$customer Sonata\Component\Customer\CustomerInterface
    public function load(CustomerInterface $customer)
    {
        $sessionBasket = parent::load($customer);
        if ($customer->getId()) {
            $basket = $this->basketManager->loadBasketPerCustomer($customer);
            if (!$basket) {
                return $sessionBasket;
            }
            $this->basketBuilder->build($basket);
            if ($sessionBasket && !$sessionBasket->isEmpty()) {
                // Retrieve elements put in session before user logged in and replace db elements with it
                $basket->setBasketElements($sessionBasket->getBasketElements());
            }
            // Clear session to avoid retaking elements from session afterwards
            $this->clearSession($customer);
            // We need to ensure that both customer & customer id are set
            $basket->setCustomer($customer);
            return $basket;
        }
        return $sessionBasket;
    }

Usage Example

 public function testLoadWithBasket()
 {
     $basket = $this->getMock('Sonata\\Component\\Basket\\BasketInterface');
     $basket->expects($this->once())->method('setCustomer');
     $basketManager = $this->getMock('Sonata\\Component\\Basket\\BasketManagerInterface');
     $basketManager->expects($this->once())->method('loadBasketPerCustomer')->will($this->returnValue($basket));
     $basketBuilder = $this->getMock('Sonata\\Component\\Basket\\BasketBuilderInterface');
     $basketBuilder->expects($this->once())->method('build');
     $customer = $this->getMock('Sonata\\Component\\Customer\\CustomerInterface');
     $customer->expects($this->once())->method('getId')->will($this->returnValue(1));
     $session = $this->getMock('Symfony\\Component\\HttpFoundation\\Session\\Session');
     $currencyDetector = $this->getMock('Sonata\\Component\\Currency\\CurrencyDetectorInterface');
     $currency = new Currency();
     $currency->setLabel("EUR");
     $currencyDetector->expects($this->any())->method('getCurrency')->will($this->returnValue($currency));
     $factory = new BasketEntityFactory($basketManager, $basketBuilder, $currencyDetector, $session);
     $basket = $factory->load($customer);
     $this->isInstanceOf('Sonata\\Component\\Basket\\BasketInterface', $basket);
 }
BasketEntityFactory