ShopUserAwareCustomerProvider   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 1
dl 0
loc 40
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A provide() 0 18 2
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