CustomerBuilder   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 209
Duplicated Lines 0 %

Test Coverage

Coverage 82.65%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 85
c 5
b 0
f 0
dl 0
loc 209
ccs 81
cts 98
cp 0.8265
rs 10
wmc 21

19 Methods

Rating   Name   Duplication   Size   Complexity  
A __clone() 0 3 1
A __construct() 0 12 1
A aCustomer() 0 21 1
A withAddresses() 0 5 1
A build() 0 20 2
A withStoreId() 0 5 1
A withFirstname() 0 5 1
A withWebsiteId() 0 5 1
A withSuffix() 0 5 1
A withEmail() 0 5 1
A withTaxvat() 0 5 1
A withConfirmation() 0 5 1
A withMiddlename() 0 5 1
A saveNewCustomer() 0 3 1
A withGroupId() 0 5 1
A withLastname() 0 5 1
A withPrefix() 0 5 1
A withCustomAttributes() 0 7 2
A withDob() 0 5 1
1
<?php
2
declare(strict_types=1);
3
4
namespace TddWizard\Fixtures\Customer;
5
6
use Magento\Customer\Api\CustomerRepositoryInterface;
7
use Magento\Customer\Api\Data\CustomerInterface;
8
use Magento\Framework\Encryption\EncryptorInterface as Encryptor;
9
use Magento\Framework\Exception\LocalizedException;
10
use Magento\TestFramework\Helper\Bootstrap;
0 ignored issues
show
Bug introduced by
The type Magento\TestFramework\Helper\Bootstrap was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
12
/**
13
 * Builder to be used by fixtures
14
 */
15
class CustomerBuilder
16
{
17
    /**
18
     * @var CustomerInterface
19
     */
20
    private $customer;
21
22
    /**
23
     * @var string
24
     */
25
    private $password;
26
27
    /**
28
     * @var CustomerRepositoryInterface
29
     */
30
    private $customerRepository;
31
32
    /**
33
     * @var AddressBuilder[]
34
     */
35
    private $addressBuilders;
36
37
    /**
38
     * @var Encryptor
39
     */
40
    private $encryptor;
41
42 26
    public function __construct(
43
        CustomerRepositoryInterface $customerRepository,
44
        CustomerInterface $customer,
45
        Encryptor $encryptor,
46
        string $password,
47
        AddressBuilder ...$addressBuilders
48
    ) {
49 26
        $this->customerRepository = $customerRepository;
50 26
        $this->customer = $customer;
51 26
        $this->encryptor = $encryptor;
52 26
        $this->password = $password;
53 26
        $this->addressBuilders = $addressBuilders;
54 26
    }
55
56 26
    public function __clone()
57
    {
58 26
        $this->customer = clone $this->customer;
59 26
    }
60
61 26
    public static function aCustomer(): CustomerBuilder
62
    {
63 26
        $objectManager = Bootstrap::getObjectManager();
64
        /** @var CustomerInterface $customer */
65 26
        $customer = $objectManager->create(CustomerInterface::class);
66 26
        $customer->setWebsiteId(1)
67 26
            ->setGroupId(1)
68 26
            ->setStoreId(1)
69 26
            ->setPrefix('Mr.')
70 26
            ->setFirstname('John')
71 26
            ->setMiddlename('A')
72 26
            ->setLastname('Smith')
73 26
            ->setSuffix('Esq.')
74 26
            ->setTaxvat('12')
75 26
            ->setGender(0);
76 26
        $password = 'Test#123';
77 26
        return new self(
78 26
            $objectManager->create(CustomerRepositoryInterface::class),
79
            $customer,
80 26
            $objectManager->create(Encryptor::class),
81
            $password
82
        );
83
    }
84
85 20
    public function withAddresses(AddressBuilder ...$addressBuilders): CustomerBuilder
86
    {
87 20
        $builder = clone $this;
88 20
        $builder->addressBuilders = $addressBuilders;
89 20
        return $builder;
90
    }
91
92 3
    public function withEmail(string $email): CustomerBuilder
93
    {
94 3
        $builder = clone $this;
95 3
        $builder->customer->setEmail($email);
96 3
        return $builder;
97
    }
98
99 1
    public function withGroupId(int $groupId): CustomerBuilder
100
    {
101 1
        $builder = clone $this;
102 1
        $builder->customer->setGroupId($groupId);
103 1
        return $builder;
104
    }
105
106 1
    public function withStoreId(int $storeId): CustomerBuilder
107
    {
108 1
        $builder = clone $this;
109 1
        $builder->customer->setStoreId($storeId);
110 1
        return $builder;
111
    }
112
113
    public function withWebsiteId(int $websiteId): CustomerBuilder
114
    {
115
        $builder = clone $this;
116
        $builder->customer->setWebsiteId($websiteId);
117
        return $builder;
118
    }
119
120 1
    public function withPrefix(string $prefix): CustomerBuilder
121
    {
122 1
        $builder = clone $this;
123 1
        $builder->customer->setPrefix($prefix);
124 1
        return $builder;
125
    }
126
127 1
    public function withFirstname(string $firstname): CustomerBuilder
128
    {
129 1
        $builder = clone $this;
130 1
        $builder->customer->setFirstname($firstname);
131 1
        return $builder;
132
    }
133
134 1
    public function withMiddlename(string $middlename): CustomerBuilder
135
    {
136 1
        $builder = clone $this;
137 1
        $builder->customer->setMiddlename($middlename);
138 1
        return $builder;
139
    }
140
141 1
    public function withLastname(string $lastname): CustomerBuilder
142
    {
143 1
        $builder = clone $this;
144 1
        $builder->customer->setLastname($lastname);
145 1
        return $builder;
146
    }
147
148 1
    public function withSuffix(string $suffix): CustomerBuilder
149
    {
150 1
        $builder = clone $this;
151 1
        $builder->customer->setSuffix($suffix);
152 1
        return $builder;
153
    }
154
155 1
    public function withTaxvat(string $taxvat): CustomerBuilder
156
    {
157 1
        $builder = clone $this;
158 1
        $builder->customer->setTaxvat($taxvat);
159 1
        return $builder;
160
    }
161
162
    public function withDob(string $dob): CustomerBuilder
163
    {
164
        $builder = clone $this;
165
        $builder->customer->setDob($dob);
166
        return $builder;
167
    }
168
169
    /**
170
     * @param mixed[] $values
171
     * @return CustomerBuilder
172
     */
173
    public function withCustomAttributes(array $values): CustomerBuilder
174
    {
175
        $builder = clone $this;
176
        foreach ($values as $code => $value) {
177
            $builder->customer->setCustomAttribute($code, $value);
178
        }
179
        return $builder;
180
    }
181
182
    public function withConfirmation(string $confirmation): CustomerBuilder
183
    {
184
        $builder = clone $this;
185
        $builder->customer->setConfirmation($confirmation);
186
        return $builder;
187
    }
188
189
    /**
190
     * @return CustomerInterface
191
     * @throws LocalizedException
192
     */
193 26
    public function build(): CustomerInterface
194
    {
195 26
        $builder = clone $this;
196 26
        if (!$builder->customer->getEmail()) {
197 23
            $builder->customer->setEmail(sha1(uniqid('', true)) . '@example.com');
198
        }
199 26
        $addresses = array_map(
200
            function (AddressBuilder $addressBuilder) {
201 20
                return $addressBuilder->buildWithoutSave();
202 26
            },
203 26
            $builder->addressBuilders
204
        );
205 26
        $builder->customer->setAddresses($addresses);
206 26
        $customer = $builder->saveNewCustomer();
207
        /*
208
         * Magento automatically sets random confirmation key for new account with password.
209
         * We need to save again with our own confirmation (null for confirmed customer)
210
         */
211 26
        $customer->setConfirmation((string)$builder->customer->getConfirmation());
212 26
        return $builder->customerRepository->save($customer);
213
    }
214
215
    /**
216
     * @SuppressWarnings(PHPMD.UnusedPrivateMethod) False positive: the method is used in build() on the cloned builder
217
     *
218
     * @return CustomerInterface
219
     * @throws LocalizedException
220
     */
221 26
    private function saveNewCustomer(): CustomerInterface
222
    {
223 26
        return $this->customerRepository->save($this->customer, $this->encryptor->getHash($this->password, true));
224
    }
225
}
226