Completed
Push — master ( 43ae25...bc0bf7 )
by Paweł
27:13 queued 11:59
created

SessionAndChannelBasedCartContext::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
rs 9.4285
cc 1
eloc 9
nc 1
nop 4
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\CoreBundle\Context;
13
14
use Sylius\Component\Cart\Context\CartContextInterface;
15
use Sylius\Component\Cart\Context\CartNotFoundException;
16
use Sylius\Component\Channel\Context\ChannelContextInterface;
17
use Sylius\Component\Channel\Context\ChannelNotFoundException;
18
use Sylius\Component\Core\Repository\CartRepositoryInterface;
19
use Symfony\Component\HttpFoundation\Session\SessionInterface;
20
21
/**
22
 * @author Anna Walasek <[email protected]>
23
 */
24
final class SessionAndChannelBasedCartContext implements CartContextInterface
25
{
26
    /**
27
     * @var SessionInterface
28
     */
29
    private $session;
30
31
    /**
32
     * @var string
33
     */
34
    private $sessionKeyName;
35
36
    /**
37
     * @var CartRepositoryInterface
38
     */
39
    private $cartRepository;
40
41
    /**
42
     * @var ChannelContextInterface
43
     */
44
    private $channelContext;
45
46
    /**
47
     * @param SessionInterface $session
48
     * @param string $sessionKeyName
49
     * @param ChannelContextInterface $channelContext
50
     * @param CartRepositoryInterface $cartRepository
51
     */
52
    public function __construct(
53
        SessionInterface $session, 
54
        $sessionKeyName, 
55
        ChannelContextInterface $channelContext,
56
        CartRepositoryInterface $cartRepository
57
    ) {
58
        $this->session = $session;
59
        $this->sessionKeyName = $sessionKeyName;
60
        $this->channelContext = $channelContext;
61
        $this->cartRepository = $cartRepository;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function getCart()
68
    {
69
        try {
70
            $channel = $this->channelContext->getChannel();
71
        } catch (ChannelNotFoundException $exception) {
72
            throw new CartNotFoundException($exception);
73
        }
74
75
        if (!$this->session->has(sprintf('%s.%s', $this->sessionKeyName, $channel->getCode()))) {
76
            throw new CartNotFoundException('Sylius was not able to find the cart in session');
77
        }
78
        
79
        $cart = $this->cartRepository->findCartByIdAndChannel(
80
            $this->session->get(sprintf('%s.%s', $this->sessionKeyName, $channel->getCode())),
81
            $channel
82
        );
83
84
        if (null === $cart) {
85
            $this->session->remove(sprintf('%s.%s', $this->sessionKeyName, $channel->getCode()));
86
87
            throw new CartNotFoundException('Sylius was not able to find the cart in session');
88
        }
89
90
        return $cart;
91
    }
92
}
93