Completed
Push — master ( 4f4b30...79d213 )
by Kamil
49:20 queued 33s
created

CustomerContext::getOrCreateCustomerByEmail()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 13
rs 9.4285
cc 2
eloc 7
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
namespace Sylius\Behat\Context\Setup;
13
14
use Behat\Behat\Context\Context;
15
use Sylius\Component\Core\Model\CustomerInterface;
16
use Sylius\Component\Core\Test\Services\SharedStorageInterface;
17
use Sylius\Component\Resource\Factory\FactoryInterface;
18
use Sylius\Component\Resource\Repository\RepositoryInterface;
19
20
/**
21
 * @author Łukasz Chruściel <[email protected]>
22
 */
23
final class CustomerContext implements Context
24
{
25
    /**
26
     * @var RepositoryInterface
27
     */
28
    private $customerRepository;
29
30
    /**
31
     * @var FactoryInterface
32
     */
33
    private $customerFactory;
34
35
    /**
36
     * @param RepositoryInterface $customerRepository
37
     * @param FactoryInterface $customerFactory
38
     */
39
    public function __construct(RepositoryInterface $customerRepository, FactoryInterface $customerFactory)
40
    {
41
        $this->customerRepository = $customerRepository;
42
        $this->customerFactory = $customerFactory;
43
    }
44
45
    /**
46
     * @Transform :customer
47
     */
48
    public function getOrCreateCustomerByEmail($email)
49
    {
50
        /** @var CustomerInterface $customer */
51
        $customer = $this->customerRepository->findOneBy(['email' => $email]);
52
        if (null === $customer) {
53
            $customer = $this->customerFactory->createNew();
54
            $customer->setEmail($email);
55
56
            $this->customerRepository->add($customer);
57
        }
58
59
        return $customer;
60
    }
61
}
62