Completed
Push — master ( 00181c...4efd6a )
by Sebastian
02:45
created

DatabaseSeeder::disableForeignKeyChecks()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
3
namespace Spatie\Seeders;
4
5
use DB;
6
use File;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Database\Seeder;
9
use League\Flysystem\Adapter\Local;
10
use League\Flysystem\Filesystem;
11
use Schema;
12
use Spatie\MediaLibrary\HasMedia\Interfaces\HasMedia;
13
use function Spatie\array_rand_value;
14
15
class DatabaseSeeder extends Seeder
16
{
17
    public function run()
18
    {
19
        if (app()->environment('local')) {
20
            throw new \Exception('Sorry, no full seeds on production!');
21
        }
22
23
        DB::connection()->disableQueryLog();
24
25
        Model::unguard();
26
27
        $this->truncateMediaTable();
28
        $this->truncateActivityTable();
29
30
        $this->clearMediaDirectory();
31
    }
32
33
    protected function truncate(string ...$tables)
34
    {
35
        $this->disableForeignKeyChecks();
36
37
        foreach ($tables as $table) {
38
            DB::table($table)->truncate();
39
        }
40
41
        $this->enableForeignKeyChecks();
42
    }
43
44
    protected function disableForeignKeyChecks()
45
    {
46
        if (config('database.default') === 'mysql') {
47
            DB::statement('SET FOREIGN_KEY_CHECKS=0');
48
        }
49
    }
50
51
    protected function enableForeignKeyChecks()
52
    {
53
        if (config('database.default') === 'mysql') {
54
            DB::statement('SET FOREIGN_KEY_CHECKS=1');
55
        }
56
    }
57
58
    protected function truncateMediaTable()
59
    {
60
        if (Schema::hasTable('media')) {
61
            $this->truncate('media');
62
        }
63
    }
64
65
    protected function truncateActivityTable()
66
    {
67
        if (Schema::hasTable('activity_log')) {
68
            $this->truncate('activity_log');
69
        }
70
    }
71
72
    protected function clearMediaDirectory()
73
    {
74
        File::cleanDirectory(public_path().'/media');
75
    }
76
77
    protected function addImages(
78
        HasMedia $model,
79
        $min = 1,
80
        $max = 3,
81
        $collectionName = 'images'
82
    ) {
83
        $this->addFiles(__DIR__.'/../images', $model, $min, $max, $collectionName);
84
    }
85
86
    protected function addDownloads(
87
        HasMedia $model,
88
        $min = 1,
89
        $max = 3,
90
        $collectionName = 'downloads'
91
    ) {
92
        $this->addFiles(__DIR__.'/../downloads', $model, $min, $max, $collectionName);
93
    }
94
95
    protected function addFiles(
96
        string $sourceDirectory,
97
        HasMedia $model,
98
        int $min = 1,
99
        int $max = 3,
100
        string $collectionName
101
    ) {
102
        $files = (new Filesystem(new Local($sourceDirectory)))->listContents();
103
104
        foreach (range($min, mt_rand($min, $max)) as $index) {
105
            $file = array_rand_value($files)['path'];
106
107
            $model
108
                ->addMedia("{$sourceDirectory}/{$file}")
109
                ->preservingOriginal()
110
                ->toCollection($collectionName);
111
        }
112
    }
113
}
114