Completed
Push — master ( 08edae...bc34a9 )
by Sebastian
12s
created

DatabaseSeeder::addDownloads()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 4
dl 0
loc 8
rs 9.4285
c 0
b 0
f 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
14
class DatabaseSeeder extends Seeder
15
{
16
    public static $withMedia = true;
17
18
    public function run()
19
    {
20
        if (!app()->environment('local', 'testing')) {
21
            throw new \Exception('Sorry, no full seeds on production!');
22
        }
23
24
        DB::connection()->disableQueryLog();
25
26
        Model::unguard();
27
28
        $this->truncateMediaTable();
29
        $this->truncateActivityTable();
30
31
        $this->clearMediaDirectory();
32
    }
33
34
    protected function truncate(string ...$tables)
35
    {
36
        collect($tables)->each(function (string $table) {
37
            if (str_contains($table, 'App\Models')) {
38
                $table = (new $table)->getTable();
39
            }
40
41
            Schema::disableForeignKeyConstraints();
42
            DB::table($table)->truncate();
43
            Schema::enableForeignKeyConstraints();
44
        });
45
    }
46
47
    protected function truncateMediaTable()
48
    {
49
        if (Schema::hasTable('media')) {
50
            $this->truncate('media');
51
        }
52
    }
53
54
    protected function truncateActivityTable()
55
    {
56
        if (Schema::hasTable('activity_log')) {
57
            $this->truncate('activity_log');
58
        }
59
    }
60
61
    protected function clearMediaDirectory()
62
    {
63
        File::cleanDirectory(public_path().'/media');
64
    }
65
66
    protected function addImages(
67
        HasMedia $model,
68
        $min = 1,
69
        $max = 3,
70
        $collectionName = 'images'
71
    ) {
72
        $this->addFiles(__DIR__.'/../images', $model, $min, $max, $collectionName);
73
    }
74
75
    protected function addDownloads(
76
        HasMedia $model,
77
        $min = 1,
78
        $max = 3,
79
        $collectionName = 'downloads'
80
    ) {
81
        $this->addFiles(__DIR__.'/../downloads', $model, $min, $max, $collectionName);
82
    }
83
84
    protected function addFiles(
85
        string $sourceDirectory,
86
        HasMedia $model,
87
        int $min = 1,
88
        int $max = 3,
89
        string $collectionName
90
    ) {
91
        $files = collect(
92
            (new Filesystem(new Local($sourceDirectory)))->listContents()
93
        );
94
95
        collect(range($min, mt_rand($min, $max)))
96
            ->each(function () use ($files, $model, $sourceDirectory, $collectionName) {
97
98
                $file = $files->random()['path'];
99
100
                $model
101
                    ->addMedia("{$sourceDirectory}/{$file}")
102
                    ->preservingOriginal()
103
                    ->toCollection($collectionName);
104
            });
105
    }
106
}
107