/
Seeder.php
105 lines (86 loc) · 2.5 KB
/
Seeder.php
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
<?php
namespace Modules;
use Exception;
use Faker\Factory as Faker;
use Illuminate\Database\Seeder as BaseSeeder;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Modules\Media\Commands\StoreNewImage;
use Modules\Media\ImageDimensionHelpers;
/**
* Class Seeder
* @package Modules
*/
abstract class Seeder extends BaseSeeder
{
use DispatchesJobs;
use ImageDimensionHelpers;
/**
* The Faker instance.
*
* @var Faker
*/
protected $faker;
protected $nl;
protected $fr;
protected $en;
protected $de;
/**
* Build a new Seed.
*/
public function __construct()
{
// Bind Faker instance if available
if (class_exists('Faker\Factory')) {
$this->nl = Faker::create('nl_BE');
$this->fr = Faker::create('fr_BE');
$this->en = Faker::create('en_US');
$this->de = Faker::create('de_DE');
$this->faker = $this->en;
}
\DB::disableQueryLog();
}
/**
*
* @throws Exception
* $images
* $sizes
*/
protected function preImageCaching()
{
//run images cachings.
foreach ($this->image_names as $image) {
$path = $this->prefix.$image;
$media = app('Modules\Media\Configurator');
foreach ($media->getImageSizes($this->model) as $size) {
list($width, $height) = $this->dimensions($size);
$constraint = $this->constraint($width, $height);
$this->images->cache(function ($image) use ($path, $width, $height, $constraint) {
$image->make($path)->resize($width, $height, $constraint);
});
}
}
}
protected function validateSeederModel()
{
if (! isset($this->image_names)) {
throw new Exception('need to set image_names when calling this function');
}
if (! isset($this->model)) {
throw new Exception('need to set the model when calling this function');
}
if (! isset($this->prefix)) {
throw new Exception('need to set prefix when calling this function');
}
}
/**
* @param $owner
* @param $account
* @throws Exception
*/
protected function newImage($owner, $account)
{
$this->validateSeederModel();
$image = rand(0, count($this->image_names) - 1);
$this->dispatch(new StoreNewImage($account, $owner, $this->prefix.$this->image_names[$image]));
}
}