ProductTableSeeder::run()   F
last analyzed

Complexity

Conditions 33
Paths 1512

Size

Total Lines 256
Code Lines 177

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 33
eloc 177
c 5
b 0
f 0
nc 1512
nop 0
dl 0
loc 256
rs 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
14
class ProductTableSeeder extends Seeder
15
{
16
    public function run()
17
    {
18
        Illuminate\Support\Facades\File::deleteDirectory(storage_path().'/app/files/product');
19
        Illuminate\Support\Facades\File::deleteDirectory(public_path().'/files/product');
20
        $directory = resource_path('assets/images/product');
21
22
        $productFiles = File::allFiles($directory);
23
24
25
        $storageImagePath = "/files/product/";
26
        $publicImagePath = public_path() .config('hideyo.public_path'). "/product/";
27
28
29
        $productCategory = ProductCategory::where('title', '=', 'Pants')->first();
30
        $taxRate = TaxRate::where('title', '=', '21%')->first();
31
        $product = new Product;
32
33
        \DB::table($product->getTable())->delete();
34
35
        for ($x = 0; $x <= 10; $x++) {
36
  
37
            $product = new Product;
38
            $shop = Shop::where('title', '=', 'hideyo')->first();
39
            $product->id = 1 + $x;
40
            $product->active = 1;
41
            $product->title = 'Cotton pants '.$x;
42
            $product->short_description = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
43
            $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.';
44
            $product->meta_title = 'Cotton pants';
45
            $product->meta_description = 'Cotton pants';   
46
            $product->price = '99.50' * $x;
47
            $product->amount = 10;
48
            $product->reference_code = '12343443';        
49
            $product->shop_id = $shop->id;
50
            $product->product_category_id = $productCategory->id;
51
            $product->tax_rate_id = $taxRate->id;
52
            $product->save();
53
54
            $productImage = new productImage;
55
            $productImage->product_id = $product->id;
56
            $productImage->extension = 'jpg';
57
            $productImage->size = '0';
58
            Storage::put($storageImagePath.$product->id.'/'.$productFiles[0]->getFileName(), $productFiles[0]->getContents());
59
            $productImage->file = $productFiles[0]->getFileName();
60
            $productImage->path = $storageImagePath.$product->id.'/'.$productFiles[0]->getFileName();
61
            $productImage->save();
62
63
64
            if ($shop->thumbnail_square_sizes) {
65
                $sizes = explode(',', $shop->thumbnail_square_sizes);
66
                if (is_array($sizes)) {
67
                    foreach ($sizes as $valueSize) {
68
69
                        $image = Image::make(storage_path().'/app/'.$storageImagePath.$product->id.'/'.$productFiles[0]->getFileName());
70
                        $explode = explode('x', $valueSize);
71
72
                        if ($image->width() >= $explode[0] and $image->height() >= $explode[1]) {
73
                            $image->resize($explode[0], $explode[1]);
0 ignored issues
show
Bug introduced by
$explode[0] of type string is incompatible with the type integer expected by parameter $width of Intervention\Image\Image::resize(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
                            $image->resize(/** @scrutinizer ignore-type */ $explode[0], $explode[1]);
Loading history...
Bug introduced by
$explode[1] of type string is incompatible with the type integer expected by parameter $height of Intervention\Image\Image::resize(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
                            $image->resize($explode[0], /** @scrutinizer ignore-type */ $explode[1]);
Loading history...
74
                        }
75
76
                        if (!File::exists($publicImagePath.$valueSize."/".$product->id."/")) {
77
                            File::makeDirectory($publicImagePath.$valueSize."/".$product->id."/", 0777, true);
78
                        }
79
80
                        $image->interlace();
81
82
                        $image->save($publicImagePath.$valueSize."/".$product->id."/".$productFiles[0]->getFileName());
83
                    }
84
                }
85
            }
86
        } 
87
88
        $productImage2 = new productImage;
89
        $productImage2->product_id = $product->id;
90
        $productImage2->extension = 'jpg';
91
        $productImage2->size = '0';
92
        Storage::put($storageImagePath.$product->id.'/'.$productFiles[1]->getFileName(), $productFiles[1]->getContents());
93
        $productImage2->file = $productFiles[1]->getFileName();
94
        $productImage2->path = $storageImagePath.$product->id.'/'.$productFiles[1]->getFileName();
95
        $productImage2->save();
96
97
        if ($shop->thumbnail_square_sizes) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $shop does not seem to be defined for all execution paths leading up to this point.
Loading history...
98
            $sizes = explode(',', $shop->thumbnail_square_sizes);
99
            if (is_array($sizes)) {
0 ignored issues
show
introduced by
The condition is_array($sizes) is always true.
Loading history...
100
                foreach ($sizes as $valueSize) {
101
102
                    $image = Image::make(storage_path().'/app/'.$storageImagePath.$product->id.'/'.$productFiles[1]->getFileName());
103
                    $explode = explode('x', $valueSize);
104
105
                    if ($image->width() >= $explode[0] and $image->height() >= $explode[1]) {
106
                        $image->resize($explode[0], $explode[1]);
107
                    }
108
109
                    if (!File::exists($publicImagePath.$valueSize."/".$product->id."/")) {
110
                        File::makeDirectory($publicImagePath.$valueSize."/".$product->id."/", 0777, true);
111
                    }
112
113
                    $image->interlace();
114
115
                    $image->save($publicImagePath.$valueSize."/".$product->id."/".$productFiles[1]->getFileName());
116
                }
117
            }
118
        }
119
120
        $product2 = new Product;
121
        $product2->id = 12;
122
        $product2->active = 1;
123
        $product2->title = 'Jeans';
124
        $product2->short_description = 'Slimfit jeans';
125
        $product2->description = 'Slimfit jeans';
126
        $product2->meta_title = 'Jeans';
127
        $product2->meta_description = 'Slimfit jeans';   
128
        $product2->price = '124.99'; 
129
        $product2->amount = 5;
130
        $product2->reference_code = '12343445';       
131
        $product2->shop_id = $shop->id;
132
        $product2->product_category_id = $productCategory->id;
133
        $product2->tax_rate_id = $taxRate->id;
134
135
        if (! $product2->save()) {
136
            \Log::info('Unable to create product '.$product2->title, (array)$product2->errors());
137
        } else {
138
            \Log::info('Created product "'.$product2->title.'" <'.$product2->title.'>');
139
        }
140
141
        $productImage = new productImage;
142
        $productImage->product_id = $product2->id;
143
        $productImage->extension = 'jpg';
144
        $productImage->size = '0';
145
        Storage::put($storageImagePath.$product2->id.'/'.$productFiles[2]->getFileName(), $productFiles[2]->getContents());
146
        $productImage->file = $productFiles[2]->getFileName();
147
        $productImage->path = $storageImagePath.$product2->id.'/'.$productFiles[2]->getFileName();
148
        $productImage->save();
149
150
151
        if ($shop->thumbnail_square_sizes) {
152
            $sizes = explode(',', $shop->thumbnail_square_sizes);
153
            if (is_array($sizes)) {
0 ignored issues
show
introduced by
The condition is_array($sizes) is always true.
Loading history...
154
                foreach ($sizes as $valueSize) {
155
156
                    $image = Image::make(storage_path().'/app/'.$storageImagePath.$product2->id.'/'.$productFiles[2]->getFileName());
157
                    $explode = explode('x', $valueSize);
158
159
                    if ($image->width() >= $explode[0] and $image->height() >= $explode[1]) {
160
                        $image->resize($explode[0], $explode[1]);
161
                    }
162
163
                    if (!File::exists($publicImagePath.$valueSize."/".$product2->id."/")) {
164
                        File::makeDirectory($publicImagePath.$valueSize."/".$product2->id."/", 0777, true);
165
                    }
166
167
                    $image->interlace();
168
169
                    $image->save($publicImagePath.$valueSize."/".$product2->id."/".$productFiles[2]->getFileName());
170
                }
171
            }
172
        }
173
174
175
        $productCategory = ProductCategory::where('title', '=', 'T-shirts')->first();
176
        $product3 = new Product;
177
        $product3->id = 13;
178
        $product3->active = 1;
179
        $product3->title = 'Cotton t-shirt';
180
        $product3->short_description = 'Cotton t-shirt';
181
        $product3->description = 'Cotton t-shirt';
182
        $product3->meta_title = 'T-shirt';
183
        $product3->meta_description = 'Cotton t-shirt';   
184
        $product3->price = '99'; 
185
        $product3->amount = 5;
186
        $product3->reference_code = '1222343445';       
187
        $product3->shop_id = $shop->id;
188
        $product3->product_category_id = $productCategory->id;
189
        $product3->tax_rate_id = $taxRate->id;
190
        $product3->save();
191
192
        $productImage = new productImage;
193
        $productImage->product_id = $product3->id;
194
        $productImage->extension = 'jpg';
195
        $productImage->size = '0';
196
        Storage::put($storageImagePath.$product3->id.'/'.$productFiles[2]->getFileName(), $productFiles[2]->getContents());
197
        $productImage->file = $productFiles[2]->getFileName();
198
        $productImage->path = $storageImagePath.$product3->id.'/'.$productFiles[2]->getFileName();
199
        $productImage->save();
200
201
        if ($shop->thumbnail_square_sizes) {
202
            $sizes = explode(',', $shop->thumbnail_square_sizes);
203
            if (is_array($sizes)) {
0 ignored issues
show
introduced by
The condition is_array($sizes) is always true.
Loading history...
204
                foreach ($sizes as $valueSize) {
205
206
                    $image = Image::make(storage_path().'/app/'.$storageImagePath.$product3->id.'/'.$productFiles[2]->getFileName());
207
                    $explode = explode('x', $valueSize);
208
209
                    if ($image->width() >= $explode[0] and $image->height() >= $explode[1]) {
210
                        $image->resize($explode[0], $explode[1]);
211
                    }
212
213
                    if (!File::exists($publicImagePath.$valueSize."/".$product3->id."/")) {
214
                        File::makeDirectory($publicImagePath.$valueSize."/".$product3->id."/", 0777, true);
215
                    }
216
217
                    $image->interlace();
218
219
                    $image->save($publicImagePath.$valueSize."/".$product3->id."/".$productFiles[2]->getFileName());
220
                }
221
            }
222
        }
223
224
225
226
        $productCategory = ProductCategory::where('title', '=', 'T-shirts')->first();
227
        $product4 = new Product;
228
        $product4->id = 14;
229
        $product4->active = 1;
230
        $product4->title = 'Sport t-shirt';
231
        $product4->short_description = 'Sport t-shirt';
232
        $product4->description = 'Sport t-shirt';
233
        $product4->meta_title = 'T-shirt';
234
        $product4->meta_description = 'Sport t-shirt';   
235
        $product4->price = '89'; 
236
        $product4->amount = 5;
237
        $product4->reference_code = '222343445';       
238
        $product4->shop_id = $shop->id;
239
        $product4->product_category_id = $productCategory->id;
240
        $product4->tax_rate_id = $taxRate->id;
241
        $product4->save();
242
243
        $productImage = new productImage;
244
        $productImage->product_id = $product4->id;
245
        $productImage->extension = 'jpg';
246
        $productImage->size = '0';
247
        Storage::put($storageImagePath.$product4->id.'/'.$productFiles[2]->getFileName(), $productFiles[2]->getContents());
248
        $productImage->file = $productFiles[2]->getFileName();
249
        $productImage->path = $storageImagePath.$product4->id.'/'.$productFiles[2]->getFileName();
250
        $productImage->save();
251
252
253
        if ($shop->thumbnail_square_sizes) {
254
            $sizes = explode(',', $shop->thumbnail_square_sizes);
255
            if (is_array($sizes)) {
0 ignored issues
show
introduced by
The condition is_array($sizes) is always true.
Loading history...
256
                foreach ($sizes as $valueSize) {
257
258
                    $image = Image::make(storage_path().'/app/'.$storageImagePath.$product4->id.'/'.$productFiles[2]->getFileName());
259
                    $explode = explode('x', $valueSize);
260
261
                    if ($image->width() >= $explode[0] and $image->height() >= $explode[1]) {
262
                        $image->resize($explode[0], $explode[1]);
263
                    }
264
265
                    if (!File::exists($publicImagePath.$valueSize."/".$product4->id."/")) {
266
                        File::makeDirectory($publicImagePath.$valueSize."/".$product4->id."/", 0777, true);
267
                    }
268
269
                    $image->interlace();
270
271
                    $image->save($publicImagePath.$valueSize."/".$product4->id."/".$productFiles[2]->getFileName());
272
                }
273
            }
274
        }
275
    }
276
}
277