|
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\Sylius\Provider; |
|
14
|
|
|
|
|
15
|
|
|
use Sylius\Component\Core\Repository\CustomerRepositoryInterface; |
|
16
|
|
|
use Sylius\Component\Customer\Model\CustomerInterface; |
|
17
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
|
18
|
|
|
|
|
19
|
|
|
final class ShopUserAwareCustomerProvider implements CustomerProviderInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var CustomerRepositoryInterface */ |
|
22
|
|
|
private $customerRepository; |
|
23
|
|
|
|
|
24
|
|
|
/** @var FactoryInterface */ |
|
25
|
|
|
private $customerFactory; |
|
26
|
|
|
|
|
27
|
|
|
/** @var LoggedInShopUserProviderInterface */ |
|
28
|
|
|
private $loggedInShopUserProvider; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct( |
|
31
|
|
|
CustomerRepositoryInterface $customerRepository, |
|
32
|
|
|
FactoryInterface $customerFactory, |
|
33
|
|
|
LoggedInShopUserProviderInterface $loggedInShopUserProvider |
|
34
|
|
|
) { |
|
35
|
|
|
$this->customerRepository = $customerRepository; |
|
36
|
|
|
$this->customerFactory = $customerFactory; |
|
37
|
|
|
$this->loggedInShopUserProvider = $loggedInShopUserProvider; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function provide(?string $cartId = null): CustomerInterface |
|
41
|
|
|
{ |
|
42
|
|
|
if ($this->loggedInShopUserProvider->isUserLoggedIn()) { |
|
43
|
|
|
$loggedInUser = $this->loggedInShopUserProvider->provide(); |
|
44
|
|
|
|
|
45
|
|
|
return $loggedInUser->getCustomer(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** @var CustomerInterface $customer */ |
|
49
|
|
|
$customer = $this->customerFactory->createNew(); |
|
50
|
|
|
$customer->setEmail(sprintf('%[email protected]', $cartId)); |
|
51
|
|
|
$customer->setFirstName('Guest'); |
|
52
|
|
|
$customer->setLastName('Customer'); |
|
53
|
|
|
|
|
54
|
|
|
$this->customerRepository->add($customer); |
|
55
|
|
|
|
|
56
|
|
|
return $customer; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|