Completed
Push — master ( 3f89d7...f790e9 )
by Mike
30:28 queued 29:00
created

DatabaseSeeder::run()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 60
rs 8.8727
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author;
4
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Book;
5
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Comment;
6
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\History;
7
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Image;
8
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Observers\AuthorObserver;
9
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Post;
10
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Printer;
11
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Profile;
12
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Publisher;
13
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Store;
14
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Tag;
15
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedPost;
16
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedUser;
17
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\User;
18
use Illuminate\Database\Seeder;
19
20
class DatabaseSeeder extends Seeder
21
{
22
    public function run()
23
    {
24
        factory(History::class)->create();
25
        $user = factory(User::class)->create();
26
        $image = factory(Image::class)->create([
27
            "imagable_id" => $user->id,
28
            "imagable_type" => User::class,
29
        ]);
30
        factory(Image::class)->create([
31
            "imagable_id" => $user->id,
32
            "imagable_type" => UncachedUser::class,
33
            "path" => $image->path,
34
        ]);
35
        factory(Tag::class, 5)->create();
36
        $post = factory(Post::class)->create();
37
        $uncachedPost = (new UncachedPost)->first();
38
        $post->tags()->attach(1);
39
        $uncachedPost->tags()->attach(1);
40
        factory(Comment::class, 5)
41
            ->create([
42
                "commentable_id" => $post->id,
43
                "commentable_type" => Post::class,
44
            ])
45
            ->each(function ($comment) {
46
                (new Comment)->create([
47
                    "commentable_id" => $comment->commentable_id,
48
                    "commentable_type" => UncachedPost::class,
49
                    "description" => $comment->description,
50
                    "subject" => $comment->subject . ' - uncached post',
51
                ]);
52
            });
53
        $publishers = factory(Publisher::class, 10)->create();
54
        (new Author)->observe(AuthorObserver::class);
55
        factory(Author::class, 10)->create()
56
            ->each(function ($author) use ($publishers) {
57
                $profile = factory(Profile::class)
58
                    ->make();
59
                $profile->author_id = $author->id;
60
                $profile->save();
61
                factory(Book::class, random_int(5, 25))
62
                    ->create([
63
                        "author_id" => $author->id,
64
                        "publisher_id" => $publishers[rand(0, 9)]->id,
65
                    ])
66
                    ->each(function ($book) use ($author, $publishers) {
67
                        factory(Printer::class)->create([
68
                            "book_id" => $book->id,
69
                        ]);
70
                    });
71
                factory(Profile::class)->make([
72
                    'author_id' => $author->id,
73
                ]);
74
            });
75
76
        $bookIds = (new Book)->all()->pluck('id');
77
        factory(Store::class, 10)->create()
78
            ->each(function ($store) use ($bookIds) {
79
                $store->books()->sync(rand($bookIds->min(), $bookIds->max()));
80
            });
81
    }
82
}
83