Completed
Push — master ( b4619a...ab6ee6 )
by Kamil
12:06 queued 06:35
created

CustomerProvider::provide()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 2
nc 2
nop 1
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
declare(strict_types=1);
13
14
namespace Sylius\Bundle\ApiBundle\Provider;
15
16
use Sylius\Component\Core\Model\CustomerInterface;
17
use Sylius\Component\Core\Repository\CustomerRepositoryInterface;
18
use Sylius\Component\Resource\Factory\FactoryInterface;
19
use Sylius\Component\User\Canonicalizer\CanonicalizerInterface;
20
21
final class CustomerProvider implements CustomerProviderInterface
22
{
23
    /** @var CanonicalizerInterface */
24
    private $canonicalizer;
25
26
    /** @var FactoryInterface */
27
    private $customerFactory;
28
29
    /** @var CustomerRepositoryInterface */
30
    private $customerRepository;
31
32
    public function __construct(
33
        CanonicalizerInterface $canonicalizer,
34
        FactoryInterface $customerFactory,
35
        CustomerRepositoryInterface $customerRepository
36
    ) {
37
        $this->canonicalizer = $canonicalizer;
38
        $this->customerFactory = $customerFactory;
39
        $this->customerRepository = $customerRepository;
40
    }
41
42
    public function provide(string $email): CustomerInterface
43
    {
44
        $emailCanonical = $this->canonicalizer->canonicalize($email);
45
46
        /** @var CustomerInterface|null $customer */
47
        $customer = $this->customerRepository->findOneBy(['emailCanonical' => $emailCanonical]);
48
49
        if ($customer === null) {
50
            /** @var CustomerInterface $customer */
51
            $customer = $this->customerFactory->createNew();
52
            $customer->setEmail($email);
53
        }
54
55
        return $customer;
56
    }
57
}
58