Completed
Push — master ( eeff33...1702a2 )
by Kamil
41:57 queued 20:40
created

CustomerContext::getOrCreateCustomerByEmail()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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