SetupCommand::seed()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Chriscreates\Blog\Console\Commands;
4
5
use Chriscreates\Blog\Comment;
6
use Chriscreates\Blog\Post;
7
use Chriscreates\Blog\Tag;
8
use Illuminate\Console\Command;
9
use Illuminate\Support\Str;
10
11
class SetupCommand extends Command
12
{
13
    /**
14
     * The name and signature of the console command.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'blog:setup
19
                            {--data : Setup the database with demo data}';
20
21
    /**
22
     * The console command description.
23
     *
24
     * @var string
25
     */
26
    protected $description = 'Scaffold basic blog views and routes';
27
28
    /**
29
     * Execute the console command.
30
     *
31
     * @return void
32
     */
33
    public function handle()
34
    {
35
        if ( ! file_exists(config_path('blog.php'))) {
36
            $this->error("You haven't published the config file.");
37
38
            if ($this->confirm('Publish the config file?')) {
39
                $this->call('blog:install');
40
            } else {
41
                return;
42
            }
43
        }
44
45
        if ( ! file_exists(base_path('routes/blog.php'))) {
46
            $this->publishRoutes();
47
        }
48
49
        if ( ! file_exists(app_path('Providers/BlogServiceProvider.php'))) {
50
            $this->publishProvider();
51
        }
52
53
        // TODO: Checks
54
        $this->callSilent('vendor:publish', ['--tag' => 'blog-assets']);
55
        $this->callSilent('vendor:publish', ['--tag' => 'blog-views']);
56
57
        if ($this->option('data')) {
58
            $this->seed();
59
        } elseif ($this->confirm(
60
            'Would you like to setup the database with demo data?'
61
        )) {
62
            $this->seed();
63
        }
64
65
        $this->info('Setup complete.');
66
    }
67
68
    /**
69
     * Run the demo data seeder.
70
     *
71
     * @return void
72
     */
73
    private function seed()
74
    {
75
        $this->callSilent('vendor:publish', ['--tag' => 'blog-factories']);
76
77
        // $files = collect(
78
        //     array_diff(scandir(__DIR__.'/../../../database/factories/'), array('.', '..'))
79
        // )->each(function ($file) {
80
        //     copy(__DIR__."/../../../database/factories/{$file}", database_path('factories')."/{$file}");
81
        // });
82
83
        // Create data
84
        factory(Post::class, 20)
85
         ->create()
86
         ->each(function ($post) {
87
             $post->tags()->sync(
88
                 factory(Tag::class, 4)->create()->pluck('id')->toArray()
89
             );
90
91
             $post->comments()->save(factory(Comment::class)->make());
92
             $post->comments()->save(factory(Comment::class)->make());
93
             $post->comments()->save(factory(Comment::class)->make());
94
         });
95
    }
96
97
    /**
98
     * Register the provider.
99
     *
100
     * @return void
101
     */
102
    private function publishProvider()
103
    {
104
        copy(
105
            __DIR__.'/../../Providers/BlogServiceProvider.php',
106
            app_path('Providers/BlogServiceProvider.php')
107
        );
108
109
        $namespace = Str::replaceLast('\\', '', $this->getAppNamespace());
0 ignored issues
show
Documentation Bug introduced by
The method getAppNamespace does not exist on object<Chriscreates\Blog...\Commands\SetupCommand>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
110
        $appConfig = file_get_contents(config_path('app.php'));
111
112
        if (Str::contains($appConfig, $namespace.'\\Providers\\BlogServiceProvider::class')) {
113
            return;
114
        }
115
116
        $lineEndingCount = [
117
            "\r\n" => substr_count($appConfig, "\r\n"),
118
            "\r" => substr_count($appConfig, "\r"),
119
            "\n" => substr_count($appConfig, "\n"),
120
        ];
121
122
        $eol = array_keys($lineEndingCount, max($lineEndingCount))[0];
123
124
        file_put_contents(config_path('app.php'), str_replace(
125
            "{$namespace}\\Providers\EventServiceProvider::class,".$eol,
126
            "{$namespace}\\Providers\EventServiceProvider::class,".$eol."        {$namespace}\Providers\BlogServiceProvider::class,".$eol,
127
            $appConfig
128
        ));
129
    }
130
131
    /**
132
     * Register the routes.
133
     *
134
     * @return void
135
     */
136
    private function publishRoutes()
137
    {
138
        copy(
139
            __DIR__.'/../../../routes/web.php',
140
            base_path('routes/blog.php')
141
        );
142
    }
143
}
144