Completed
Push — master ( 3ff7e6...cf5edc )
by Kamil
96:30 queued 63:14
created

CustomerContext::heHasBillingAddress()   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) default (address is "(?:[^"]+)", "(?:[^"]+)", "(?:[^"]+)", "(?:[^"]+)" for "(?:[^"]+)")$/
159
     * @Given /^(his) default (address is "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)")$/
160
     */
161
    public function heHasDefaultAddress(CustomerInterface $customer, AddressInterface $address)
162
    {
163
        $customer->setDefaultAddress($address);
164
165
        $this->customerManager->flush();
166
    }
167
168
    /**
169
     * @Given /^(the customer) subscribed to the newsletter$/
170
     */
171
    public function theCustomerSubscribedToTheNewsletter(CustomerInterface $customer)
172
    {
173
        $customer->setSubscribedToNewsletter(true);
174
175
        $this->customerManager->flush();
176
    }
177
178
    /**
179
     * @Given /^(I) have an (address "[^"]+", "[^"]+", "[^"]+", "[^"]+", "[^"]+"(?:|, "[^"]+")) in my address book$/
180
     */
181
    public function iHaveAnAddressInAddressBook(ShopUserInterface $user, AddressInterface $address)
182
    {
183
        /** @var CustomerInterface $customer */
184
        $customer = $user->getCustomer();
185
186
        $this->thisCustomerHasAnAddressInAddressBook($customer, $address);
187
    }
188
189
    /**
190
     * @Given /^(this customer) has an (address "[^"]+", "[^"]+", "[^"]+", "[^"]+", "[^"]+"(?:|, "[^"]+")) in their address book$/
191
     */
192
    public function thisCustomerHasAnAddressInAddressBook(CustomerInterface $customer, AddressInterface $address)
193
    {
194
        $customer->addAddress($address);
195
196
        $this->customerManager->flush();
197
    }
198
199
    /**
200
     * @param string $email
201
     * @param string|null $firstName
202
     * @param string|null $lastName
203
     * @param \DateTime|null $createdAt
204
     */
205
    private function createCustomer($email, $firstName = null, $lastName = null, \DateTime $createdAt = null)
206
    {
207
        /** @var CustomerInterface $customer */
208
        $customer = $this->customerFactory->createNew();
209
210
        $customer->setFirstName($firstName);
211
        $customer->setLastName($lastName);
212
        $customer->setEmail($email);
213
        if (null !== $createdAt) {
214
            $customer->setCreatedAt($createdAt);
215
        }
216
217
        $this->sharedStorage->set('customer', $customer);
218
        $this->customerRepository->add($customer);
219
    }
220
221
    /**
222
     * @param string $email
223
     * @param string $password
224
     * @param bool $enabled
225
     * @param string|null $firstName
226
     * @param string|null $lastName
227
     * @param string|null $role
228
     */
229
    private function createCustomerWithUserAccount(
230
        $email,
231
        $password,
232
        $enabled = true,
233
        $firstName = null,
234
        $lastName = null,
235
        $role = null
236
    ) {
237
        $user = $this->userFactory->createNew();
238
        /** @var CustomerInterface $customer */
239
        $customer = $this->customerFactory->createNew();
240
241
        $customer->setFirstName($firstName);
242
        $customer->setLastName($lastName);
243
        $customer->setEmail($email);
244
245
        $user->setUsername($email);
246
        $user->setPlainPassword($password);
247
        $user->setEnabled($enabled);
248
        $user->addRole($role);
249
250
        $customer->setUser($user);
251
252
        $this->sharedStorage->set('customer', $customer);
253
        $this->customerRepository->add($customer);
254
    }
255
}
256