|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file has been created by developers from BitBag. |
|
5
|
|
|
* Feel free to contact us once you face any issues or want to start |
|
6
|
|
|
* another great project. |
|
7
|
|
|
* You can find more information about us on https://bitbag.io and write us |
|
8
|
|
|
* an email on [email protected]. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
declare(strict_types=1); |
|
12
|
|
|
|
|
13
|
|
|
namespace BitBag\SyliusVueStorefrontPlugin\CommandHandler\Cart; |
|
14
|
|
|
|
|
15
|
|
|
use BitBag\SyliusVueStorefrontPlugin\Command\Cart\CreateCart; |
|
16
|
|
|
use BitBag\SyliusVueStorefrontPlugin\Sylius\Provider\ChannelProviderInterface; |
|
17
|
|
|
use BitBag\SyliusVueStorefrontPlugin\Sylius\Provider\CustomerProviderInterface; |
|
18
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
|
19
|
|
|
use Sylius\Component\Core\Repository\OrderRepositoryInterface; |
|
20
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
|
21
|
|
|
use Symfony\Component\Messenger\Handler\MessageHandlerInterface; |
|
22
|
|
|
|
|
23
|
|
|
final class CreateCartHandler implements MessageHandlerInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** @var FactoryInterface */ |
|
26
|
|
|
private $cartFactory; |
|
27
|
|
|
|
|
28
|
|
|
/** @var OrderRepositoryInterface */ |
|
29
|
|
|
private $cartRepository; |
|
30
|
|
|
|
|
31
|
|
|
/** @var ChannelProviderInterface */ |
|
32
|
|
|
private $channelProvider; |
|
33
|
|
|
|
|
34
|
|
|
/** @var CustomerProviderInterface */ |
|
35
|
|
|
private $customerProvider; |
|
36
|
|
|
|
|
37
|
|
|
public function __construct( |
|
38
|
|
|
FactoryInterface $cartFactory, |
|
39
|
|
|
OrderRepositoryInterface $cartRepository, |
|
40
|
|
|
ChannelProviderInterface $channelProvider, |
|
41
|
|
|
CustomerProviderInterface $customerProvider |
|
42
|
|
|
) { |
|
43
|
|
|
$this->cartFactory = $cartFactory; |
|
44
|
|
|
$this->cartRepository = $cartRepository; |
|
45
|
|
|
$this->channelProvider = $channelProvider; |
|
46
|
|
|
$this->customerProvider = $customerProvider; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function __invoke(CreateCart $createCart): void |
|
50
|
|
|
{ |
|
51
|
|
|
$channel = $this->channelProvider->provide(); |
|
52
|
|
|
$customer = $this->customerProvider->provide($createCart->cartId()); |
|
53
|
|
|
|
|
54
|
|
|
if (strpos($customer->getEmail(), '@guest.example') === false) { |
|
55
|
|
|
$cart = $this->cartRepository->findLatestCartByChannelAndCustomer($channel, $customer); |
|
56
|
|
|
$cart->setTokenValue($createCart->cartId()); |
|
57
|
|
|
|
|
58
|
|
|
return; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** @var OrderInterface $cart */ |
|
62
|
|
|
$cart = $this->cartFactory->createNew(); |
|
63
|
|
|
$cart->setCustomer($customer); |
|
64
|
|
|
$cart->setChannel($channel); |
|
65
|
|
|
$cart->setCurrencyCode($channel->getBaseCurrency()->getCode()); |
|
66
|
|
|
$cart->setLocaleCode($channel->getDefaultLocale()->getCode()); |
|
67
|
|
|
$cart->setTokenValue($createCart->cartId()); |
|
68
|
|
|
|
|
69
|
|
|
$this->cartRepository->add($cart); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|