Passed
Push — trunk ( 26fc38...0b2f4f )
by Christian
13:44 queued 13s
created

CustomerBuilder::connection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Test\Integration\Builder\Customer;
4
5
use Doctrine\DBAL\Connection;
6
use Shopware\Core\Framework\Log\Package;
7
use Shopware\Core\Framework\Test\IdsCollection;
8
use Shopware\Core\Framework\Test\TestCaseBase\KernelTestBehaviour;
9
use Shopware\Core\Framework\Uuid\Uuid;
10
use Shopware\Core\Test\TestBuilderTrait;
11
use Shopware\Core\Test\TestDefaults;
12
13
/**
14
 * @final
15
 * How to use:
16
 * $x = (new CustomerBuilder(new IdsCollection(), 'p1'))
17
 *          ->firstName('Max')
18
 *          ->lastName('Muster')
19
 *          ->group('standard')
20
 *          ->build();
21
 */
22
#[Package('customer-order')]
23
class CustomerBuilder
24
{
25
    use KernelTestBehaviour;
26
    use TestBuilderTrait;
27
28
    public string $id;
29
30
    protected string $firstName;
31
32
    protected string $lastName;
33
34
    protected string $email;
35
36
    protected string $customerGroupId;
37
38
    protected string $defaultBillingAddressId;
39
40
    /**
41
     * @var array<string, mixed>
42
     */
43
    protected array $defaultBillingAddress = [];
44
45
    protected string $defaultShippingAddressId;
46
47
    protected string $defaultPaymentMethodId;
48
49
    /**
50
     * @var array<string, mixed>
51
     */
52
    protected array $addresses = [];
53
54
    /**
55
     * @var array<string, mixed>
56
     */
57
    protected array $group = [];
58
59
    /**
60
     * @var array<string, mixed>
61
     */
62
    protected array $defaultPaymentMethod = [];
63
64
    /**
65
     * @var array<string, mixed>
66
     */
67
    protected array $salutation = [];
68
69
    public function __construct(
70
        IdsCollection $ids,
71
        protected string $customerNumber,
72
        protected string $salesChannelId = TestDefaults::SALES_CHANNEL,
73
        string $customerGroup = 'customer-group',
74
        string $billingAddress = 'default-address',
75
        string $shippingAddress = 'default-address'
76
    ) {
77
        $this->ids = $ids;
78
        $this->id = $ids->create($customerNumber);
79
        $this->firstName = 'Max';
80
        $this->lastName = 'Mustermann';
81
        $this->email = '[email protected]';
82
        $this->salutation = self::salutation($ids);
83
84
        $this->customerGroup($customerGroup);
85
        $this->defaultBillingAddress($billingAddress);
86
        $this->defaultShippingAddress($shippingAddress);
87
88
        $this->defaultPaymentMethodId = self::connection()->fetchOne(
89
            'SELECT LOWER(HEX(payment_method_id))
90
                   FROM sales_channel_payment_method
91
                   JOIN payment_method ON sales_channel_payment_method.payment_method_id = payment_method.id
92
                   WHERE sales_channel_id = :id AND payment_method.active = true LIMIT 1',
93
            ['id' => Uuid::fromHexToBytes($salesChannelId)]
94
        );
95
    }
96
97
    public function customerNumber(string $customerNumber): self
98
    {
99
        $this->customerNumber = $customerNumber;
100
101
        return $this;
102
    }
103
104
    public function firstName(string $firstName): self
105
    {
106
        $this->firstName = $firstName;
107
108
        return $this;
109
    }
110
111
    public function lastName(string $lastName): self
112
    {
113
        $this->lastName = $lastName;
114
115
        return $this;
116
    }
117
118
    public function customerGroup(string $key): self
119
    {
120
        $this->customerGroupId = $this->ids->get($key);
121
        $this->group = [
122
            'id' => $this->ids->get($key),
123
            'name' => $key,
124
        ];
125
126
        return $this;
127
    }
128
129
    /**
130
     * @param array<string, mixed> $customParams
131
     */
132
    public function defaultBillingAddress(string $key, array $customParams = []): self
133
    {
134
        $this->addAddress($key, $customParams);
135
136
        $defaultBillingAddress = $this->addresses;
137
        $defaultBillingAddress[$key]['id'] = $this->ids->get($key);
138
        $this->defaultBillingAddress = $defaultBillingAddress[$key];
139
        $this->defaultBillingAddressId = $this->ids->get($key);
140
141
        return $this;
142
    }
143
144
    /**
145
     * @param array<string, mixed> $customParams
146
     */
147
    public function defaultShippingAddress(string $key, array $customParams = []): self
148
    {
149
        $this->addAddress($key, $customParams);
150
        $this->defaultShippingAddressId = $this->ids->get($key);
151
152
        return $this;
153
    }
154
155
    public function defaultPaymentMethod(string $key): self
156
    {
157
        $this->defaultPaymentMethod = [
158
            'id' => $this->ids->get($key),
159
            'name' => $key,
160
        ];
161
162
        return $this;
163
    }
164
165
    /**
166
     * @param array<string, mixed> $customParams
167
     */
168
    public function addAddress(string $key, array $customParams = []): self
169
    {
170
        $address = \array_replace([
171
            'firstName' => $this->firstName,
172
            'lastName' => $this->lastName,
173
            'city' => 'Bielefeld',
174
            'salutation' => self::salutation($this->ids),
175
            'street' => 'Buchenweg 5',
176
            'zipcode' => '33062',
177
            'countryId' => $this->getCountry(),
178
        ], $customParams);
179
180
        $this->addresses[$key] = $address;
181
182
        return $this;
183
    }
184
185
    /**
186
     * @return array<string, mixed>
187
     */
188
    private static function salutation(IdsCollection $ids): array
189
    {
190
        return [
191
            'id' => $ids->get('salutation'),
192
            'salutationKey' => 'salutation',
193
            'displayName' => 'test',
194
            'letterName' => 'test',
195
        ];
196
    }
197
198
    private static function connection(): Connection
199
    {
200
        return self::getContainer()->get(Connection::class);
201
    }
202
203
    private function getCountry(): string
204
    {
205
        return self::connection()->fetchOne(
206
            'SELECT LOWER(HEX(country_id)) FROM sales_channel_country WHERE sales_channel_id = :id LIMIT 1',
207
            ['id' => Uuid::fromHexToBytes($this->salesChannelId)]
208
        );
209
    }
210
}
211