Passed
Push — master ( 8e5f83...c46986 )
by Matthijs
02:10
created

ProductTableSeeder::run()   F

Complexity

Conditions 33
Paths 1512

Size

Total Lines 258
Code Lines 177

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 33
eloc 177
nc 1512
nop 0
dl 0
loc 258
rs 0
c 2
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
use Illuminate\Database\Seeder;
4
use Illuminate\Database\Eloquent\Model;
5
use Hideyo\Ecommerce\Framework\Services\Shop\Entity\Shop as Shop;
6
use Hideyo\Ecommerce\Framework\Services\Product\Entity\Product as Product;
7
use Hideyo\Ecommerce\Framework\Services\ProductCategory\Entity\ProductCategory as ProductCategory;
8
use Hideyo\Ecommerce\Framework\Services\Product\Entity\ProductImage as ProductImage;
9
use Hideyo\Ecommerce\Framework\Services\TaxRate\Entity\TaxRate as TaxRate;
10
use Illuminate\Support\Facades\Storage;
11
use Intervention\Image\Facades\Image as Image;
12
use Illuminate\Support\Facades\File;
13
use DB;
14
use Log;
15
16
class ProductTableSeeder extends Seeder
17
{
18
    public function run()
19
    {
20
        Illuminate\Support\Facades\File::deleteDirectory(storage_path().'/app/files/product');
21
        Illuminate\Support\Facades\File::deleteDirectory(public_path().'/files/product');
22
        $directory = resource_path('assets/images/product');
23
24
        $productFiles = File::allFiles($directory);
25
26
27
        $storageImagePath = "/files/product/";
28
        $publicImagePath = public_path() .config('hideyo.public_path'). "/product/";
29
30
31
        $productCategory = ProductCategory::where('title', '=', 'Pants')->first();
32
        $taxRate = TaxRate::where('title', '=', '21%')->first();
33
        $product = new Product;
34
35
        DB::table($product->getTable())->delete();
36
37
        for ($x = 0; $x <= 10; $x++) {
38
  
39
            $product = new Product;
40
            $shop = Shop::where('title', '=', 'hideyo')->first();
41
            $product->id = 1 + $x;
42
            $product->active = 1;
43
            $product->title = 'Cotton pants '.$x;
44
            $product->short_description = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
45
            $product->description = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec nec nulla dignissim, tempus neque quis, pharetra orci. Pellentesque scelerisque odio vitae dolor pretium, in luctus eros convallis. Etiam nec leo porta, dapibus lectus a, convallis ligula. Morbi in dui aliquet, mattis justo at, egestas nisi. Suspendisse lobortis felis enim, venenatis venenatis elit pretium id. Duis a magna eros. Proin auctor orci eu posuere tincidunt. Interdum et malesuada fames ac ante ipsum primis in faucibus. Nullam ac tempus urna. Quisque mattis mauris quis elit elementum, porta tincidunt erat scelerisque. Donec ornare lacus quis purus consequat cursus. Maecenas fringilla interdum purus id semper. Donec non eros sem. Maecenas sit amet augue ut lacus commodo venenatis.';
46
            $product->meta_title = 'Cotton pants';
47
            $product->meta_description = 'Cotton pants';   
48
            $product->price = '99.50' * $x;
49
            $product->amount = 10;
50
            $product->reference_code = '12343443';        
51
            $product->shop_id = $shop->id;
52
            $product->product_category_id = $productCategory->id;
53
            $product->tax_rate_id = $taxRate->id;
54
            $product->save();
55
56
            $productImage = new productImage;
57
            $productImage->product_id = $product->id;
58
            $productImage->extension = 'jpg';
59
            $productImage->size = '0';
60
            Storage::put($storageImagePath.$product->id.'/'.$productFiles[0]->getFileName(), $productFiles[0]->getContents());
61
            $productImage->file = $productFiles[0]->getFileName();
62
            $productImage->path = $storageImagePath.$product->id.'/'.$productFiles[0]->getFileName();
63
            $productImage->save();
64
65
66
            if ($shop->thumbnail_square_sizes) {
67
                $sizes = explode(',', $shop->thumbnail_square_sizes);
68
                if (is_array($sizes)) {
69
                    foreach ($sizes as $valueSize) {
70
71
                        $image = Image::make(storage_path().'/app/'.$storageImagePath.$product->id.'/'.$productFiles[0]->getFileName());
72
                        $explode = explode('x', $valueSize);
73
74
                        if ($image->width() >= $explode[0] and $image->height() >= $explode[1]) {
75
                            $image->resize($explode[0], $explode[1]);
76
                        }
77
78
                        if (!File::exists($publicImagePath.$valueSize."/".$product->id."/")) {
79
                            File::makeDirectory($publicImagePath.$valueSize."/".$product->id."/", 0777, true);
80
                        }
81
82
                        $image->interlace();
83
84
                        $image->save($publicImagePath.$valueSize."/".$product->id."/".$productFiles[0]->getFileName());
85
                    }
86
                }
87
            }
88
        } 
89
90
        $productImage2 = new productImage;
91
        $productImage2->product_id = $product->id;
92
        $productImage2->extension = 'jpg';
93
        $productImage2->size = '0';
94
        Storage::put($storageImagePath.$product->id.'/'.$productFiles[1]->getFileName(), $productFiles[1]->getContents());
95
        $productImage2->file = $productFiles[1]->getFileName();
96
        $productImage2->path = $storageImagePath.$product->id.'/'.$productFiles[1]->getFileName();
97
        $productImage2->save();
98
99
        if ($shop->thumbnail_square_sizes) {
100
            $sizes = explode(',', $shop->thumbnail_square_sizes);
101
            if (is_array($sizes) {
102
                foreach ($sizes as $valueSize) {
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_FOREACH on line 102 at column 16
Loading history...
103
104
                    $image = Image::make(storage_path().'/app/'.$storageImagePath.$product->id.'/'.$productFiles[1]->getFileName());
105
                    $explode = explode('x', $valueSize);
106
107
                    if ($image->width() >= $explode[0] and $image->height() >= $explode[1]) {
108
                        $image->resize($explode[0], $explode[1]);
109
                    }
110
111
                    if (!File::exists($publicImagePath.$valueSize."/".$product->id."/")) {
112
                        File::makeDirectory($publicImagePath.$valueSize."/".$product->id."/", 0777, true);
113
                    }
114
115
                    $image->interlace();
116
117
                    $image->save($publicImagePath.$valueSize."/".$product->id."/".$productFiles[1]->getFileName());
118
                }
119
            }
120
        }
121
122
        $product2 = new Product;
123
        $product2->id = 12;
124
        $product2->active = 1;
125
        $product2->title = 'Jeans';
126
        $product2->short_description = 'Slimfit jeans';
127
        $product2->description = 'Slimfit jeans';
128
        $product2->meta_title = 'Jeans';
129
        $product2->meta_description = 'Slimfit jeans';   
130
        $product2->price = '124.99'; 
131
        $product2->amount = 5;
132
        $product2->reference_code = '12343445';       
133
        $product2->shop_id = $shop->id;
134
        $product2->product_category_id = $productCategory->id;
135
        $product2->tax_rate_id = $taxRate->id;
136
137
        if (! $product2->save()) {
138
            Log::info('Unable to create product '.$product2->title, (array)$product2->errors());
139
        } else {
140
            Log::info('Created product "'.$product2->title.'" <'.$product2->title.'>');
141
        }
142
143
144
145
        $productImage = new productImage;
146
        $productImage->product_id = $product2->id;
147
        $productImage->extension = 'jpg';
148
        $productImage->size = '0';
149
        Storage::put($storageImagePath.$product2->id.'/'.$productFiles[2]->getFileName(), $productFiles[2]->getContents());
150
        $productImage->file = $productFiles[2]->getFileName();
151
        $productImage->path = $storageImagePath.$product2->id.'/'.$productFiles[2]->getFileName();
152
        $productImage->save();
153
154
155
        if ($shop->thumbnail_square_sizes) {
156
            $sizes = explode(',', $shop->thumbnail_square_sizes);
157
            if (is_array($sizes) {
158
                foreach ($sizes as $valueSize) {
159
160
                    $image = Image::make(storage_path().'/app/'.$storageImagePath.$product2->id.'/'.$productFiles[2]->getFileName());
161
                    $explode = explode('x', $valueSize);
162
163
                    if ($image->width() >= $explode[0] and $image->height() >= $explode[1]) {
164
                        $image->resize($explode[0], $explode[1]);
165
                    }
166
167
                    if (!File::exists($publicImagePath.$valueSize."/".$product2->id."/")) {
168
                        File::makeDirectory($publicImagePath.$valueSize."/".$product2->id."/", 0777, true);
169
                    }
170
171
                    $image->interlace();
172
173
                    $image->save($publicImagePath.$valueSize."/".$product2->id."/".$productFiles[2]->getFileName());
174
                }
175
            }
176
        }
177
178
179
        $productCategory = ProductCategory::where('title', '=', 'T-shirts')->first();
180
        $product3 = new Product;
181
        $product3->id = 13;
182
        $product3->active = 1;
183
        $product3->title = 'Cotton t-shirt';
184
        $product3->short_description = 'Cotton t-shirt';
185
        $product3->description = 'Cotton t-shirt';
186
        $product3->meta_title = 'T-shirt';
187
        $product3->meta_description = 'Cotton t-shirt';   
188
        $product3->price = '99'; 
189
        $product3->amount = 5;
190
        $product3->reference_code = '1222343445';       
191
        $product3->shop_id = $shop->id;
192
        $product3->product_category_id = $productCategory->id;
193
        $product3->tax_rate_id = $taxRate->id;
194
        $product3->save();
195
196
        $productImage = new productImage;
197
        $productImage->product_id = $product3->id;
198
        $productImage->extension = 'jpg';
199
        $productImage->size = '0';
200
        Storage::put($storageImagePath.$product3->id.'/'.$productFiles[2]->getFileName(), $productFiles[2]->getContents());
201
        $productImage->file = $productFiles[2]->getFileName();
202
        $productImage->path = $storageImagePath.$product3->id.'/'.$productFiles[2]->getFileName();
203
        $productImage->save();
204
205
        if ($shop->thumbnail_square_sizes) {
206
            $sizes = explode(',', $shop->thumbnail_square_sizes);
207
            if (is_array($sizes) {
208
                foreach ($sizes as $valueSize) {
209
210
                    $image = Image::make(storage_path().'/app/'.$storageImagePath.$product3->id.'/'.$productFiles[2]->getFileName());
211
                    $explode = explode('x', $valueSize);
212
213
                    if ($image->width() >= $explode[0] and $image->height() >= $explode[1]) {
214
                        $image->resize($explode[0], $explode[1]);
215
                    }
216
217
                    if (!File::exists($publicImagePath.$valueSize."/".$product3->id."/")) {
218
                        File::makeDirectory($publicImagePath.$valueSize."/".$product3->id."/", 0777, true);
219
                    }
220
221
                    $image->interlace();
222
223
                    $image->save($publicImagePath.$valueSize."/".$product3->id."/".$productFiles[2]->getFileName());
224
                }
225
            }
226
        }
227
228
229
230
        $productCategory = ProductCategory::where('title', '=', 'T-shirts')->first();
231
        $product4 = new Product;
232
        $product4->id = 14;
233
        $product4->active = 1;
234
        $product4->title = 'Sport t-shirt';
235
        $product4->short_description = 'Sport t-shirt';
236
        $product4->description = 'Sport t-shirt';
237
        $product4->meta_title = 'T-shirt';
238
        $product4->meta_description = 'Sport t-shirt';   
239
        $product4->price = '89'; 
240
        $product4->amount = 5;
241
        $product4->reference_code = '222343445';       
242
        $product4->shop_id = $shop->id;
243
        $product4->product_category_id = $productCategory->id;
244
        $product4->tax_rate_id = $taxRate->id;
245
        $product4->save();
246
247
        $productImage = new productImage;
248
        $productImage->product_id = $product4->id;
249
        $productImage->extension = 'jpg';
250
        $productImage->size = '0';
251
        Storage::put($storageImagePath.$product4->id.'/'.$productFiles[2]->getFileName(), $productFiles[2]->getContents());
252
        $productImage->file = $productFiles[2]->getFileName();
253
        $productImage->path = $storageImagePath.$product4->id.'/'.$productFiles[2]->getFileName();
254
        $productImage->save();
255
256
257
        if ($shop->thumbnail_square_sizes) {
258
            $sizes = explode(',', $shop->thumbnail_square_sizes);
259
            if (is_array($sizes) {
260
                foreach ($sizes as $valueSize) {
261
262
                    $image = Image::make(storage_path().'/app/'.$storageImagePath.$product4->id.'/'.$productFiles[2]->getFileName());
263
                    $explode = explode('x', $valueSize);
264
265
                    if ($image->width() >= $explode[0] and $image->height() >= $explode[1]) {
266
                        $image->resize($explode[0], $explode[1]);
267
                    }
268
269
                    if (!File::exists($publicImagePath.$valueSize."/".$product4->id."/")) {
270
                        File::makeDirectory($publicImagePath.$valueSize."/".$product4->id."/", 0777, true);
271
                    }
272
273
                    $image->interlace();
274
275
                    $image->save($publicImagePath.$valueSize."/".$product4->id."/".$productFiles[2]->getFileName());
276
                }
277
            }
278
        }
279
    }
280
}
281