Completed
Push — master ( e62357...459429 )
by Loïc
17s queued 11s
created

CustomerContext::thereIsCustomerWithNameAndEmail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
nc 1
cc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of AppName.
5
 *
6
 * (c) Monofony
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 App\Behat\Context\Setup;
15
16
use Behat\Behat\Context\Context;
17
use Doctrine\Common\Persistence\ObjectManager;
18
use App\Behat\Service\SharedStorageInterface;
19
use App\Entity\Customer;
20
use Sylius\Component\Resource\Factory\FactoryInterface;
21
use Sylius\Component\Resource\Repository\RepositoryInterface;
22
use Sylius\Component\User\Model\User;
23
24
final class CustomerContext implements Context
25
{
26
    /**
27
     * @var SharedStorageInterface
28
     */
29
    private $sharedStorage;
30
31
    /**
32
     * @var RepositoryInterface
33
     */
34
    private $customerRepository;
35
36
    /**
37
     * @var ObjectManager
38
     */
39
    private $customerManager;
40
41
    /**
42
     * @var FactoryInterface
43
     */
44
    private $customerFactory;
45
46
    /**
47
     * @var FactoryInterface
48
     */
49
    private $appUserFactory;
50
51
    /**
52
     * @param SharedStorageInterface $sharedStorage
53
     * @param RepositoryInterface    $customerRepository
54
     * @param ObjectManager          $customerManager
55
     * @param FactoryInterface       $customerFactory
56
     * @param FactoryInterface       $appUserFactory
57
     */
58
    public function __construct(
59
        SharedStorageInterface $sharedStorage,
60
        RepositoryInterface $customerRepository,
61
        ObjectManager $customerManager,
62
        FactoryInterface $customerFactory,
63
        FactoryInterface $appUserFactory
64
    ) {
65
        $this->sharedStorage = $sharedStorage;
66
        $this->customerRepository = $customerRepository;
67
        $this->customerManager = $customerManager;
68
        $this->customerFactory = $customerFactory;
69
        $this->appUserFactory = $appUserFactory;
70
    }
71
72
    /**
73
     * @Given there is a customer :name with email :email
74
     */
75
    public function thereIsCustomerWithNameAndEmail($name, $email): void
76
    {
77
        $partsOfName = explode(' ', $name);
78
        $customer = $this->createCustomer($email, $partsOfName[0], $partsOfName[1]);
79
        $this->customerRepository->add($customer);
80
    }
81
82
    /**
83
     * @Given there is (also )a customer :email
84
     */
85
    public function thereIsCustomer($email): void
86
    {
87
        $customer = $this->createCustomer($email);
88
89
        $this->customerRepository->add($customer);
90
    }
91
92
    /**
93
     * @Given there are :numberOfCustomers customers
94
     */
95
    public function thereAreCustomers(int $numberOfCustomers): void
96
    {
97
        for ($i=0; $i<$numberOfCustomers; ++$i) {
98
            $customer = $this->createCustomer(sprintf('john%[email protected]', uniqid()));
99
            $customer->setFirstname('John');
100
            $customer->setLastname('Doe' . $i);
101
102
            $this->customerRepository->add($customer);
103
        }
104
    }
105
106
    /**
107
     * @Given there is customer :email with first name :firstName
108
     */
109
    public function thereIsCustomerWithFirstName($email, $firstName): void
110
    {
111
        $customer = $this->createCustomer($email, $firstName);
112
113
        $this->customerRepository->add($customer);
114
    }
115
116
    /**
117
     * @param string                  $email
118
     * @param string|null             $firstName
119
     * @param string|null             $lastName
120
     * @param \DateTimeInterface|null $createdAt
121
     * @param string|null             $phoneNumber
122
     *
123
     * @return Customer
124
     */
125
    private function createCustomer(
126
        $email,
127
        $firstName = null,
128
        $lastName = null,
129
        \DateTimeInterface $createdAt = null,
130
        $phoneNumber = null
131
    ) {
132
        /** @var Customer $customer */
133
        $customer = $this->customerFactory->createNew();
134
135
        $customer->setFirstName($firstName);
136
        $customer->setLastName($lastName);
137
        $customer->setEmail($email);
138
        $customer->setPhoneNumber($phoneNumber);
139
        if (null !== $createdAt) {
140
            $customer->setCreatedAt($createdAt);
141
        }
142
143
        $this->sharedStorage->set('customer', $customer);
144
145
        return $customer;
146
    }
147
148
    /**
149
     * @param string      $email
150
     * @param string      $password
151
     * @param bool        $enabled
152
     * @param string|null $firstName
153
     * @param string|null $lastName
154
     * @param string|null $role
155
     *
156
     * @return Customer
157
     */
158
    private function createCustomerWithUserAccount(
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
159
        $email,
160
        $password,
161
        $enabled = true,
162
        $firstName = null,
163
        $lastName = null,
164
        $role = null
165
    ) {
166
        /** @var User $user */
167
        $user = $this->appUserFactory->createNew();
168
        /** @var Customer $customer */
169
        $customer = $this->customerFactory->createNew();
170
171
        $customer->setFirstName($firstName);
172
        $customer->setLastName($lastName);
173
        $customer->setEmail($email);
174
175
        $user->setUsername($email);
176
        $user->setPlainPassword($password);
177
        $user->setEnabled($enabled);
178
        if (null !== $role) {
179
            $user->addRole($role);
180
        }
181
182
        $customer->setUser($user);
183
184
        $this->sharedStorage->set('customer', $customer);
185
186
        return $customer;
187
    }
188
}
189