Completed
Push — master ( bc34a9...2d2192 )
by Freek
24:03
created

DatabaseSeeder::truncateContentBlocksTable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Seeders;
4
5
use App\Models\ContentBlock;
6
use DB;
7
use File;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Database\Seeder;
10
use League\Flysystem\Adapter\Local;
11
use League\Flysystem\Filesystem;
12
use Schema;
13
use Spatie\MediaLibrary\HasMedia\Interfaces\HasMedia;
14
15
class DatabaseSeeder extends Seeder
16
{
17
    public static $withMedia = true;
18
19
    public function run()
20
    {
21
        DB::connection()->disableQueryLog();
22
23
        Model::unguard();
24
25
        $this->truncateMediaTable();
26
        $this->truncateContentBlocksTable();
27
        $this->truncateActivityTable();
28
29
        $this->clearMediaDirectory();
30
    }
31
32
    protected function truncate(string ...$tables)
33
    {
34
        collect($tables)->each(function (string $table) {
35
            if (str_contains($table, 'App\Models')) {
36
                $table = (new $table)->getTable();
37
            }
38
39
            Schema::disableForeignKeyConstraints();
40
            DB::table($table)->truncate();
41
            Schema::enableForeignKeyConstraints();
42
        });
43
    }
44
45
    protected function truncateMediaTable()
46
    {
47
        if (Schema::hasTable('media')) {
48
            $this->truncate('media');
49
        }
50
    }
51
52
    protected function truncateContentBlocksTable()
53
    {
54
        if (Schema::hasTable('content_blocks')) {
55
            $this->truncate('content_blocks');
56
        }
57
    }
58
59
    protected function truncateActivityTable()
60
    {
61
        if (Schema::hasTable('activity_log')) {
62
            $this->truncate('activity_log');
63
        }
64
    }
65
66
    protected function clearMediaDirectory()
67
    {
68
        File::cleanDirectory(public_path() . '/media');
69
    }
70
71
    protected function addImages(
72
        HasMedia $model,
73
        $min = 1,
74
        $max = 3,
75
        $collectionName = 'images'
76
    ) {
77
        $this->addFiles(__DIR__.'/../images', $model, $min, $max, $collectionName);
78
    }
79
80
    protected function addDownloads(
81
        HasMedia $model,
82
        $min = 1,
83
        $max = 3,
84
        $collectionName = 'downloads'
85
    ) {
86
        $this->addFiles(__DIR__.'/../downloads', $model, $min, $max, $collectionName);
87
    }
88
89
    protected function addFiles(
90
        string $sourceDirectory,
91
        HasMedia $model,
92
        int $min = 1,
93
        int $max = 3,
94
        string $collectionName
95
    ) {
96
        $files = collect(
97
            (new Filesystem(new Local($sourceDirectory)))->listContents()
98
        );
99
100
        collect(range($min, mt_rand($min, $max)))
101
            ->each(function () use ($files, $model, $sourceDirectory, $collectionName) {
102
103
                $file = $files->random()['path'];
104
105
                $model
106
                    ->addMedia("{$sourceDirectory}/{$file}")
107
                    ->preservingOriginal()
108
                    ->toCollection($collectionName);
109
            });
110
    }
111
112
    public function addContentBlocks(Model $model, $minimum = 1, $maximum = 3): Model
113
    {
114
        $maximum = faker()->numberBetween($minimum, $maximum);
0 ignored issues
show
Bug introduced by
The method numberBetween does only exist in Faker\Generator, but not in Spatie\Seeders\Faker.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
115
116
        if ($maximum === 0) {
117
            return $model;
118
        }
119
120
        foreach (range($minimum, $maximum) as $i) {
121
            $contentBlock = ContentBlock::create([
122
                'type' => faker()->randomElement(['imageLeft', 'imageRight']),
0 ignored issues
show
Bug introduced by
The method randomElement does only exist in Faker\Generator, but not in Spatie\Seeders\Faker.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
123
                'name' => faker()->translate(faker()->sentence()),
0 ignored issues
show
Bug introduced by
The method translate does only exist in Spatie\Seeders\Faker, but not in Faker\Generator.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
124
                'text' => faker()->translate(faker()->paragraph()),
125
                'draft' => false,
126
                'online' => true,
127
            ]);
128
129
            $this->addImages($contentBlock, 1,1, 'image');
130
            $contentBlock->collection_name = 'default';
131
            $contentBlock->subject()->associate($model);
132
            $contentBlock->save();
133
        }
134
135
        return $model;
136
    }
137
}
138