Completed
Push — unused-definitions ( d9908f )
by Kamil
18:33
created

theStoreHasCustomerWithNameAndRegistrationDate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 3
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 name :fullName since :since
100
     */
101
    public function theStoreHasCustomerWithNameAndRegistrationDate($email, $fullName, $since)
102
    {
103
        $names = explode(' ', $fullName);
104
        $this->createCustomer($email, $names[0], $names[1], new \DateTime($since));
105
    }
106
107
    /**
108
     * @Given there is disabled customer account :email with password :password
109
     */
110
    public function thereIsDisabledCustomerAccountWithPassword($email, $password)
111
    {
112
        $this->createCustomerWithUserAccount($email, $password, false);
113
    }
114
115
    /**
116
     * @Given there is a customer account :email
117
     * @Given there is a customer account :email identified by :password
118
     * @Given there is enabled customer account :email with password :password
119
     */
120
    public function theStoreHasEnabledCustomerAccountWithPassword($email, $password = 'sylius')
121
    {
122
        $this->createCustomerWithUserAccount($email, $password, true);
123
    }
124
125
    /**
126
     * @Given there is a customer :name identified by an email :email and a password :password
127
     */
128
    public function theStoreHasCustomerAccountWithEmailAndPassword($name, $email, $password)
129
    {
130
        $names = explode(' ', $name);
131
        $firstName = $names[0];
132
        $lastName = count($names) > 1 ? $names[1] : null;
133
134
        $this->createCustomerWithUserAccount($email, $password, true, $firstName, $lastName);
135
    }
136
137
    /**
138
     * @Given /^(the customer) subscribed to the newsletter$/
139
     */
140
    public function theCustomerSubscribedToTheNewsletter(CustomerInterface $customer)
141
    {
142
        $customer->setSubscribedToNewsletter(true);
143
144
        $this->customerManager->flush();
145
    }
146
147
    /**
148
     * @Given /^(this customer) verified their email$/
149
     */
150
    public function theCustomerVerifiedTheirEmail(CustomerInterface $customer)
151
    {
152
        $customer->getUser()->setVerifiedAt(new \DateTime());
153
154
        $this->customerManager->flush();
155
    }
156
157
    /**
158
     * @Given /^(the customer) belongs to (group "([^"]+)")$/
159
     */
160
    public function theCustomerBelongsToGroup(CustomerInterface $customer, CustomerGroupInterface $customerGroup)
161
    {
162
        $customer->setGroup($customerGroup);
163
164
        $this->customerManager->flush();
165
    }
166
167
    /**
168
     * @param string $email
169
     * @param string|null $firstName
170
     * @param string|null $lastName
171
     * @param \DateTime|null $createdAt
172
     */
173
    private function createCustomer($email, $firstName = null, $lastName = null, \DateTime $createdAt = null)
174
    {
175
        /** @var CustomerInterface $customer */
176
        $customer = $this->customerFactory->createNew();
177
178
        $customer->setFirstName($firstName);
179
        $customer->setLastName($lastName);
180
        $customer->setEmail($email);
181
        if (null !== $createdAt) {
182
            $customer->setCreatedAt($createdAt);
183
        }
184
185
        $this->sharedStorage->set('customer', $customer);
186
        $this->customerRepository->add($customer);
187
    }
188
189
    /**
190
     * @param string $email
191
     * @param string $password
192
     * @param bool $enabled
193
     * @param string|null $firstName
194
     * @param string|null $lastName
195
     * @param string|null $role
196
     */
197
    private function createCustomerWithUserAccount(
198
        $email,
199
        $password,
200
        $enabled = true,
201
        $firstName = null,
202
        $lastName = null,
203
        $role = null
204
    ) {
205
        $user = $this->userFactory->createNew();
206
        /** @var CustomerInterface $customer */
207
        $customer = $this->customerFactory->createNew();
208
209
        $customer->setFirstName($firstName);
210
        $customer->setLastName($lastName);
211
        $customer->setEmail($email);
212
213
        $user->setUsername($email);
214
        $user->setPlainPassword($password);
215
        $user->setEnabled($enabled);
216
        $user->addRole($role);
217
218
        $customer->setUser($user);
219
220
        $this->sharedStorage->set('customer', $customer);
221
        $this->customerRepository->add($customer);
222
    }
223
}
224