Completed
Push — remove-content-bundle ( 201341...8d07b3 )
by Kamil
52:29 queued 32:39
created

CustomerContext   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 218
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 0
Metric Value
wmc 18
c 0
b 0
f 0
lcom 2
cbo 6
dl 0
loc 218
rs 10

15 Methods

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