2
2
3
3
namespace GetCandy \Console ;
4
4
5
+ use GetCandy \Models \Attribute ;
6
+ use GetCandy \FieldTypes \TranslatedText ;
7
+ use GetCandy \Hub \Models \Staff ;
8
+ use GetCandy \Models \AttributeGroup ;
9
+ use GetCandy \Models \Channel ;
10
+ use GetCandy \Models \Collection ;
11
+ use GetCandy \Models \Country ;
12
+ use GetCandy \Models \Currency ;
13
+ use GetCandy \Models \CustomerGroup ;
14
+ use GetCandy \Models \Language ;
15
+ use GetCandy \Models \ProductType ;
16
+ use GetCandy \Models \TaxClass ;
5
17
use Illuminate \Console \Command ;
18
+ use Illuminate \Support \Facades \DB ;
6
19
use Illuminate \Support \Facades \File ;
7
20
8
21
class InstallGetCandy extends Command
@@ -28,22 +41,218 @@ class InstallGetCandy extends Command
28
41
*/
29
42
public function handle (): void
30
43
{
31
- $ this ->info ('Installing GetCandy... ' );
44
+ DB ::transaction (function () {
45
+ $ this ->info ('Installing GetCandy... ' );
32
46
33
- $ this ->info ('Publishing configuration... ' );
47
+ $ this ->info ('Publishing configuration... ' );
34
48
35
- if (! $ this ->configExists ('getcandy ' )) {
36
- $ this ->publishConfiguration ();
37
- } else {
38
- if ($ this ->shouldOverwriteConfig ()) {
39
- $ this ->info ('Overwriting configuration file... ' );
40
- $ this ->publishConfiguration ($ force = true );
49
+ if (! $ this ->configExists ('getcandy ' )) {
50
+ $ this ->publishConfiguration ();
41
51
} else {
42
- $ this ->info ('Existing configuration was not overwritten ' );
52
+ if ($ this ->shouldOverwriteConfig ()) {
53
+ $ this ->info ('Overwriting configuration file... ' );
54
+ $ this ->publishConfiguration ($ force = true );
55
+ } else {
56
+ $ this ->info ('Existing configuration was not overwritten ' );
57
+ }
43
58
}
44
- }
45
59
46
- $ this ->info ('GetCandy is now installed. ' );
60
+ $ this ->info ('Publishing hub assets ' );
61
+
62
+ $ this ->publishResources ();
63
+
64
+ if (! Country::count ()) {
65
+ $ this ->info ('Importing countries ' );
66
+ $ this ->call ('getcandy:import:address-data ' );
67
+ }
68
+
69
+ if (! Channel::whereDefault (true )->exists ()) {
70
+ $ this ->info ('Setting up default channel ' );
71
+
72
+ Channel::create ([
73
+ 'name ' => 'Webstore ' ,
74
+ 'handle ' => 'webstore ' ,
75
+ 'default ' => true ,
76
+ 'url ' => 'localhost ' ,
77
+ ]);
78
+ }
79
+
80
+ if (! Staff::whereAdmin (true )->exists ()) {
81
+ $ this ->info ('Create an admin user ' );
82
+
83
+ $ firstname = $ this ->ask ('Whats your first name? ' );
84
+ $ lastname = $ this ->ask ('Whats your last name? ' );
85
+ $ email = $ this ->ask ('Whats your email address? ' );
86
+ $ password = $ this ->secret ('Enter a password ' );
87
+
88
+ Staff::create ([
89
+ 'firstname ' => $ firstname ,
90
+ 'lastname ' => $ lastname ,
91
+ 'email ' => $ email ,
92
+ 'password ' => bcrypt ($ password ),
93
+ 'admin ' => true ,
94
+ ]);
95
+ }
96
+
97
+ if (! Language::count ()) {
98
+ $ this ->info ('Adding default language ' );
99
+
100
+ Language::create ([
101
+ 'code ' => 'en ' ,
102
+ 'name ' => 'English ' ,
103
+ 'default ' => true ,
104
+ ]);
105
+ }
106
+
107
+ if (! Currency::whereDefault (true )->exists ()) {
108
+ $ this ->info ('Adding a default currency (USD) ' );
109
+
110
+ Currency::create ([
111
+ 'code ' => 'USD ' ,
112
+ 'name ' => 'US Dollar ' ,
113
+ 'exchange_rate ' => 1 ,
114
+ 'format ' => '${value} ' ,
115
+ 'decimal_point ' => '. ' ,
116
+ 'thousand_point ' => ', ' ,
117
+ 'decimal_places ' => 2 ,
118
+ 'default ' => true ,
119
+ ]);
120
+ }
121
+
122
+ if (! CustomerGroup::whereDefault (true )->exists ()) {
123
+ $ this ->info ('Adding a default customer group. ' );
124
+
125
+ CustomerGroup::create ([
126
+ 'name ' => 'Retail ' ,
127
+ 'handle ' => 'retail ' ,
128
+ 'default ' => true ,
129
+ ]);
130
+ }
131
+
132
+ if (! TaxClass::count ()) {
133
+ $ this ->info ('Adding a default tax class. ' );
134
+
135
+ TaxClass::create ([
136
+ 'name ' => 'Default Tax Class ' ,
137
+ ]);
138
+ }
139
+
140
+ if (! Attribute::count ()) {
141
+ $ this ->info ('Setting up initial attributes ' );
142
+
143
+ $ group = AttributeGroup::create ([
144
+ 'attributable_type ' => ProductType::class,
145
+ 'name ' => collect ([
146
+ 'en ' => 'Details ' ,
147
+ ]),
148
+ 'handle ' => 'details ' ,
149
+ 'position ' => 1 ,
150
+ ]);
151
+
152
+ $ collectionGroup = AttributeGroup::create ([
153
+ 'attributable_type ' => Collection::class,
154
+ 'name ' => collect ([
155
+ 'en ' => 'Details ' ,
156
+ ]),
157
+ 'handle ' => 'collection_details ' ,
158
+ 'position ' => 1 ,
159
+ ]);
160
+
161
+ Attribute::create ([
162
+ 'attribute_type ' => ProductType::class,
163
+ 'attribute_group_id ' => $ group ->id ,
164
+ 'position ' => 1 ,
165
+ 'name ' => [
166
+ 'en ' => 'Name ' ,
167
+ ],
168
+ 'handle ' => 'name ' ,
169
+ 'section ' => 'main ' ,
170
+ 'type ' => TranslatedText::class,
171
+ 'required ' => true ,
172
+ 'default_value ' => null ,
173
+ 'configuration ' => [
174
+ 'type ' => 'text ' ,
175
+ ],
176
+ 'system ' => true ,
177
+ ]);
178
+
179
+ Attribute::create ([
180
+ 'attribute_type ' => Collection::class,
181
+ 'attribute_group_id ' => $ collectionGroup ->id ,
182
+ 'position ' => 1 ,
183
+ 'name ' => [
184
+ 'en ' => 'Name ' ,
185
+ ],
186
+ 'handle ' => 'name ' ,
187
+ 'section ' => 'main ' ,
188
+ 'type ' => TranslatedText::class,
189
+ 'required ' => true ,
190
+ 'default_value ' => null ,
191
+ 'configuration ' => [
192
+ 'type ' => 'text ' ,
193
+ ],
194
+ 'system ' => true ,
195
+ ]);
196
+
197
+ Attribute::create ([
198
+ 'attribute_type ' => ProductType::class,
199
+ 'attribute_group_id ' => $ group ->id ,
200
+ 'position ' => 2 ,
201
+ 'name ' => [
202
+ 'en ' => 'Description ' ,
203
+ ],
204
+ 'handle ' => 'description ' ,
205
+ 'section ' => 'main ' ,
206
+ 'type ' => TranslatedText::class,
207
+ 'required ' => true ,
208
+ 'default_value ' => null ,
209
+ 'configuration ' => [
210
+ 'type ' => 'richtext ' ,
211
+ ],
212
+ 'system ' => true ,
213
+ ]);
214
+
215
+ Attribute::create ([
216
+ 'attribute_type ' => Collection::class,
217
+ 'attribute_group_id ' => $ collectionGroup ->id ,
218
+ 'position ' => 2 ,
219
+ 'name ' => [
220
+ 'en ' => 'Description ' ,
221
+ ],
222
+ 'handle ' => 'description ' ,
223
+ 'section ' => 'main ' ,
224
+ 'type ' => TranslatedText::class,
225
+ 'required ' => true ,
226
+ 'default_value ' => null ,
227
+ 'configuration ' => [
228
+ 'type ' => 'richtext ' ,
229
+ ],
230
+ 'system ' => true ,
231
+ ]);
232
+ }
233
+
234
+ if (! ProductType::count ()) {
235
+ $ this ->info ('Adding a product type. ' );
236
+
237
+ $ type = ProductType::create ([
238
+ 'name ' => 'Stock ' ,
239
+ ]);
240
+
241
+ $ type ->mappedAttributes ()->attach (
242
+ Attribute::whereAttributeType (ProductType::class)->get ()->pluck ('id ' )
243
+ );
244
+ }
245
+
246
+ $ this ->info ('GetCandy is now installed. ' );
247
+
248
+ if ($ this ->confirm ('Would you like to show some love by starring the repo? ' )) {
249
+ $ exec = PHP_OS_FAMILY === 'Windows ' ? 'start ' : 'open ' ;
250
+
251
+ exec ("{$ exec } https://github.com/getcandy/getcandy " );
252
+
253
+ $ this ->line ("Thanks, you're awesome! " );
254
+ }
255
+ });
47
256
}
48
257
49
258
/**
@@ -54,6 +263,9 @@ public function handle(): void
54
263
*/
55
264
private function configExists ($ fileName ): bool
56
265
{
266
+ if (! File::isDirectory (config_path ($ fileName ))) {
267
+ return false ;
268
+ }
57
269
return ! empty (File::allFiles (config_path ($ fileName )));
58
270
}
59
271
@@ -89,4 +301,11 @@ private function publishConfiguration($forcePublish = false): void
89
301
90
302
$ this ->call ('vendor:publish ' , $ params );
91
303
}
304
+
305
+ private function publishResources ()
306
+ {
307
+ $ this ->call ('vendor:publish ' , [
308
+ '--tag ' => 'getcandy:hub:public ' ,
309
+ ]);
310
+ }
92
311
}
0 commit comments