Completed
Push — master ( 6d05bc...b47a4a )
by Paweł
23:31 queued 13:20
created

thisCustomerHasAnAddressInAddressBook()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
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 Doctrine\Common\Persistence\ObjectManager;
16
use Sylius\Behat\Service\SharedStorageInterface;
17
use Sylius\Component\Core\Model\AddressInterface;
18
use Sylius\Component\Core\Model\CustomerInterface;
19
use Sylius\Component\Core\Model\ShopUserInterface;
20
use Sylius\Component\Core\Repository\CustomerRepositoryInterface;
21
use Sylius\Component\Resource\Factory\FactoryInterface;
22
23
/**
24
 * @author Anna Walasek <[email protected]>
25
 */
26
final class CustomerContext implements Context
27
{
28
    /**
29
     * @var SharedStorageInterface
30
     */
31
    private $sharedStorage;
32
33
    /**
34
     * @var CustomerRepositoryInterface
35
     */
36
    private $customerRepository;
37
38
    /**
39
     * @var ObjectManager
40
     */
41
    private $customerManager;
42
43
    /**
44
     * @var FactoryInterface
45
     */
46
    private $customerFactory;
47
48
    /**
49
     * @var FactoryInterface
50
     */
51
    private $userFactory;
52
53
    /**
54
     * @param SharedStorageInterface $sharedStorage
55
     * @param CustomerRepositoryInterface $customerRepository
56
     * @param ObjectManager $customerManager
57
     * @param FactoryInterface $customerFactory
58
     * @param FactoryInterface $userFactory
59
     */
60
    public function __construct(
61
        SharedStorageInterface $sharedStorage,
62
        CustomerRepositoryInterface $customerRepository,
63
        ObjectManager $customerManager,
64
        FactoryInterface $customerFactory,
65
        FactoryInterface $userFactory
66
    ) {
67
        $this->sharedStorage = $sharedStorage;
68
        $this->customerRepository = $customerRepository;
69
        $this->customerManager = $customerManager;
70
        $this->customerFactory = $customerFactory;
71
        $this->userFactory = $userFactory;
72
    }
73
74
    /**
75
     * @Given the store has customer :name with email :email
76
     */
77
    public function theStoreHasCustomerWithNameAndEmail($name, $email)
78
    {
79
        $partsOfName = explode(' ', $name);
80
        $this->createCustomer($email, $partsOfName[0], $partsOfName[1]);
81
    }
82
83
    /**
84
     * @Given the store has customer :email
85
     */
86
    public function theStoreHasCustomer($email)
87
    {
88
        $this->createCustomer($email);
89
    }
90
91
    /**
92
     * @Given the store has customer :email with first name :firstName
93
     */
94
    public function theStoreHasCustomerWithFirstName($email, $firstName)
95
    {
96
        $this->createCustomer($email, $firstName);
97
    }
98
99
    /**
100
     * @Given the store has customer :email with last name :lastName
101
     */
102
    public function theStoreHasCustomerWithLastName($email, $lastName)
103
    {
104
        $this->createCustomer($email, null, $lastName);
105
    }
106
107
    /**
108
     * @Given the store has customer :email with name :fullName since :since
109
     */
110
    public function theStoreHasCustomerWithNameAndRegistrationDate($email, $fullName, $since)
111
    {
112
        $names = explode(' ', $fullName);
113
        $this->createCustomer($email, $names[0], $names[1], new \DateTime($since));
114
    }
115
116
    /**
117
     * @Given there is disabled customer account :email with password :password
118
     */
119
    public function thereIsDisabledCustomerAccountWithPassword($email, $password)
120
    {
121
        $this->createCustomerWithUserAccount($email, $password, false);
122
    }
123
124
    /**
125
     * @Given there is enabled customer account :email with password :password
126
     * @Given there is a customer account :email identified by :password
127
     */
128
    public function theStoreHasEnabledCustomerAccountWithPassword($email, $password)
129
    {
130
        $this->createCustomerWithUserAccount($email, $password, true);
131
    }
132
133
    /**
134
     * @Given there is a customer :name identified by an email :email and a password :password
135
     */
136
    public function theStoreHasCustomerAccountWithEmailAndPassword($name, $email, $password)
137
    {
138
        $names = explode(' ', $name);
139
        $firstName = $names[0];
140
        $lastName = count($names) > 1 ? $names[1] : null;
141
142
        $this->createCustomerWithUserAccount($email, $password, true, $firstName, $lastName);
143
    }
144
145
    /**
146
     * @Given there is an administrator :name identified by an email :email and a password :password
147
     */
148
    public function thereIsAdministratorIdentifiedByEmailAndPassword($name, $email, $password)
149
    {
150
        $names = explode(' ', $name);
151
        $firstName = $names[0];
152
        $lastName = count($names) > 1 ? $names[1] : null;
153
154
        $this->createCustomerWithUserAccount($email, $password, true, $firstName, $lastName, 'ROLE_ADMINISTRATION_ACCESS');
155
    }
156
157
    /**
158
     * @Given /^(his) shipping (address is "(?:[^"]+)", "(?:[^"]+)", "(?:[^"]+)", "(?:[^"]+)" for "(?:[^"]+)")$/
159
     * @Given /^(his) shipping (address is "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)")$/
160
     */
161
    public function heHasShippingAddress(CustomerInterface $customer, AddressInterface $address)
162
    {
163
        $customer->setShippingAddress($address);
164
165
        $this->customerManager->flush();
166
    }
167
168
    /**
169
     * @Given /^(his) billing (address is "(?:[^"]+)", "(?:[^"]+)", "(?:[^"]+)", "(?:[^"]+)" for "(?:[^"]+)")$/
170
     * @Given /^(his) billing (address is "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)")$/
171
     */
172
    public function heHasBillingAddress(CustomerInterface $customer, AddressInterface $address)
173
    {
174
        $customer->setBillingAddress($address);
175
176
        $this->customerManager->flush();
177
    }
178
179
    /**
180
     * @Given /^(the customer) subscribed to the newsletter$/
181
     */
182
    public function theCustomerSubscribedToTheNewsletter(CustomerInterface $customer)
183
    {
184
        $customer->setSubscribedToNewsletter(true);
185
186
        $this->customerManager->flush();
187
    }
188
189
    /**
190
     * @Given /^(I) have an (address "[^"]+", "[^"]+", "[^"]+", "[^"]+", "[^"]+"(?:|, "[^"]+")) in my address book$/
191
     */
192
    public function iHaveAnAddressInAddressBook(ShopUserInterface $user, AddressInterface $address)
193
    {
194
        /** @var CustomerInterface $customer */
195
        $customer = $user->getCustomer();
196
197
        $this->thisCustomerHasAnAddressInAddressBook($customer, $address);
198
    }
199
200
    /**
201
     * @Given /^(this customer) has an (address "[^"]+", "[^"]+", "[^"]+", "[^"]+", "[^"]+"(?:|, "[^"]+")) in their address book$/
202
     */
203
    public function thisCustomerHasAnAddressInAddressBook(CustomerInterface $customer, AddressInterface $address)
204
    {
205
        $customer->addAddress($address);
206
207
        $this->customerManager->flush();
208
    }
209
210
    /**
211
     * @param string $email
212
     * @param string|null $firstName
213
     * @param string|null $lastName
214
     * @param \DateTime|null $createdAt
215
     */
216
    private function createCustomer($email, $firstName = null, $lastName = null, \DateTime $createdAt = null)
217
    {
218
        /** @var CustomerInterface $customer */
219
        $customer = $this->customerFactory->createNew();
220
221
        $customer->setFirstName($firstName);
222
        $customer->setLastName($lastName);
223
        $customer->setEmail($email);
224
        if (null !== $createdAt) {
225
            $customer->setCreatedAt($createdAt);
226
        }
227
228
        $this->sharedStorage->set('customer', $customer);
229
        $this->customerRepository->add($customer);
230
    }
231
232
    /**
233
     * @param string $email
234
     * @param string $password
235
     * @param bool $enabled
236
     * @param string|null $firstName
237
     * @param string|null $lastName
238
     * @param string|null $role
239
     */
240
    private function createCustomerWithUserAccount(
241
        $email,
242
        $password,
243
        $enabled = true,
244
        $firstName = null,
245
        $lastName = null,
246
        $role = null
247
    ) {
248
        $user = $this->userFactory->createNew();
249
        /** @var CustomerInterface $customer */
250
        $customer = $this->customerFactory->createNew();
251
252
        $customer->setFirstName($firstName);
253
        $customer->setLastName($lastName);
254
        $customer->setEmail($email);
255
256
        $user->setUsername($email);
257
        $user->setPlainPassword($password);
258
        $user->setEnabled($enabled);
259
        $user->addRole($role);
260
261
        $customer->setUser($user);
262
263
        $this->sharedStorage->set('customer', $customer);
264
        $this->customerRepository->add($customer);
265
    }
266
}
267