DatabaseSeeder::truncateContentBlocksTable()   A
last analyzed

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 DB;
6
use File;
7
use Schema;
8
use App\Models\ContentBlock;
9
use Illuminate\Database\Seeder;
10
use League\Flysystem\Filesystem;
11
use League\Flysystem\Adapter\Local;
12
use Illuminate\Database\Eloquent\Model;
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,
93
        int $max,
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
                $file = $files->random()['path'];
103
104
                $model
105
                    ->addMedia("{$sourceDirectory}/{$file}")
106
                    ->preservingOriginal()
107
                    ->toCollection($collectionName);
108
            });
109
    }
110
111
    public function addContentBlocks(Model $model, $minimum = 1, $maximum = 3): Model
112
    {
113
        $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...
114
115
        if ($maximum === 0) {
116
            return $model;
117
        }
118
119
        foreach (range($minimum, $maximum) as $i) {
120
            $contentBlock = ContentBlock::create([
121
                '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...
122
                '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...
123
                'text' => faker()->translate(faker()->paragraph()),
124
                'draft' => false,
125
                'online' => true,
126
            ]);
127
128
            $this->addImages($contentBlock, 1, 1, 'image');
129
            $contentBlock->collection_name = 'default';
130
            $contentBlock->subject()->associate($model);
131
            $contentBlock->save();
132
        }
133
134
        return $model;
135
    }
136
}
137