-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCustomerProvider.php
More file actions
160 lines (135 loc) · 5.24 KB
/
CustomerProvider.php
File metadata and controls
160 lines (135 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
declare(strict_types=1);
/*
* (c) shopware AG <info@shopware.com>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Swag\PlatformDemoData\DataProvider;
use Doctrine\DBAL\Connection;
use Shopware\Core\Content\Category\CategoryCollection;
use Shopware\Core\Framework\Api\Context\SystemSource;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Log\Package;
#[Package('fundamentals@after-sales')]
class CustomerProvider extends DemoDataProvider
{
public const CUSTOMER_ID = '6c97534c2c0747f39e8751e43cb2b013';
private Connection $connection;
/**
* @var EntityRepository<CategoryCollection>
*/
private EntityRepository $categoryRepository;
/**
* @param EntityRepository<CategoryCollection> $categoryRepository
*/
public function __construct(Connection $connection, EntityRepository $categoryRepository)
{
$this->connection = $connection;
$this->categoryRepository = $categoryRepository;
}
public function getAction(): string
{
return 'upsert';
}
public function getEntity(): string
{
return 'customer';
}
public function getPayload(): array
{
$salutationId = $this->getSalutationId();
$paymentMethodId = $this->getPaymentMethodId();
$countryId = $this->getCountryId();
$salesChannelId = $this->getStorefrontSalesChannelId();
return [
[
'id' => self::CUSTOMER_ID,
'defaultPaymentMethodId' => $paymentMethodId,
'salutationId' => $salutationId,
'salesChannelId' => $salesChannelId,
'customerNumber' => 'SWDEMO10000',
'firstName' => 'Max',
'lastName' => 'Mustermann',
'password' => '$2y$10$qYoCQe2r3h/tiGIqwsq7YuuKBGCEmgtM/U4v182xtKDrFv5vSNFJO',
'email' => 'test@example.com',
'active' => true,
'guest' => false,
'newsletter' => false,
'lastLogin' => '2019-06-12 07:13:39.641',
'birthday' => '1996-06-06',
'defaultShippingAddress' => [
'id' => 'd8f0dff7ef3947979a83c42f6509f22c',
'countryId' => $countryId,
'salutationId' => $salutationId,
'firstName' => 'Max',
'lastName' => 'Mustermann',
'street' => 'Musterstraße 1',
'zipcode' => '12345',
'city' => 'Musterstadt',
],
'defaultBillingAddressId' => 'd8f0dff7ef3947979a83c42f6509f22c',
'groupId' => 'cfbd5018d38d41d8adca10d94fc8bdd6',
],
];
}
private function getSalutationId(): string
{
$result = $this->connection->fetchOne('
SELECT LOWER(HEX(COALESCE(
(SELECT `id` FROM `salutation` WHERE `salutation_key` = "mr" LIMIT 1),
(SELECT `id` FROM `salutation` LIMIT 1)
)))
');
if (!$result) {
throw new \RuntimeException('No salutation found, please make sure that basic data is available by running the migrations.');
}
return (string) $result;
}
private function getPaymentMethodId(): string
{
$result = $this->connection->fetchOne('
SELECT LOWER(HEX(`id`))
FROM `payment_method`
WHERE `active` = 1;
');
if (!$result) {
throw new \RuntimeException('No active payment method found, please make sure that basic data is available by running the migrations.');
}
return (string) $result;
}
private function getCountryId(): string
{
$result = $this->connection->fetchOne('
SELECT LOWER(HEX(`id`))
FROM `country`
WHERE `active` = 1;
');
if (!$result) {
throw new \RuntimeException('No active payment method found, please make sure that basic data is available by running the migrations.');
}
return (string) $result;
}
private function getStorefrontSalesChannelId(): string
{
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('parentId', null));
$criteria->addAssociation('navigationSalesChannels');
$rootCategory = $this->categoryRepository->search($criteria, new Context(new SystemSource()))->getEntities()->first();
if (!$rootCategory) {
throw new \RuntimeException('Root category not found');
}
$navigationSalesChannels = $rootCategory->getNavigationSalesChannels();
if ($navigationSalesChannels === null) {
throw new \RuntimeException('Sales channel not found');
}
$navigationSalesChannel = $navigationSalesChannels->first();
if (!$navigationSalesChannel) {
throw new \RuntimeException('Sales channel not found');
}
return $navigationSalesChannel->getId();
}
}