Completed
Pull Request — master (#146)
by Łukasz
10:35 queued 06:48
created

CustomerProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sylius\ShopApiPlugin\Provider;
6
7
use Sylius\Component\Core\Model\CustomerInterface;
8
use Sylius\Component\Core\Repository\CustomerRepositoryInterface;
9
use Sylius\Component\Resource\Factory\FactoryInterface;
10
11
final class CustomerProvider implements CustomerProviderInterface
12
{
13
    /**
14
     * @var CustomerRepositoryInterface
15
     */
16
    private $customerRepository;
17
18
    /**
19
     * @var FactoryInterface
20
     */
21
    private $customerFactory;
22
23
    public function __construct(CustomerRepositoryInterface $customerRepository, FactoryInterface $customerFactory)
24
    {
25
        $this->customerRepository = $customerRepository;
26
        $this->customerFactory = $customerFactory;
27
    }
28
29
    public function provide(string $email): CustomerInterface
30
    {
31
        $customer =  $this->customerRepository->findOneBy(['email' => $email]);
32
33
        if (null === $customer) {
34
            /** @var CustomerInterface $customer */
35
            $customer = $this->customerFactory->createNew();
36
            $customer->setEmail($email);
37
38
            $this->customerRepository->add($customer);
39
        }
40
41
        return $customer;
42
    }
43
}
44