DatabaseManager::seed()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace App\Http\Helpers\Installer;
4
5
use Exception;
6
use Illuminate\Database\SQLiteConnection;
7
use Illuminate\Support\Facades\Artisan;
8
use Illuminate\Support\Facades\Config;
9
use Illuminate\Support\Facades\DB;
10
11
class DatabaseManager
12
{
13
14
    /**
15
     * Migrate and seed the database.
16
     *
17
     * @return array
18
     */
19
    public function migrateAndSeed()
20
    {
21
        $this->sqlite();
22
        return $this->migrate();
23
    }
24
25
    /**
26
     * Run the migration and call the seeder.
27
     *
28
     * @return array
29
     */
30
    private function migrate()
31
    {
32
        try{
33
            Artisan::call('migrate', ["--force"=> true ]);
34
        }
35
        catch(Exception $e){
36
            return $this->response($e->getMessage());
37
        }
38
39
        return $this->seed();
40
    }
41
42
    /**
43
     * Seed the database.
44
     *
45
     * @return array
46
     */
47
    private function seed()
48
    {
49
        try{
50
            Artisan::call('db:seed');
51
        }
52
        catch(Exception $e){
53
            return $this->response($e->getMessage());
54
        }
55
56
        return $this->response(trans('installer.final.finished'), 'success');
57
    }
58
59
    /**
60
     * Return a formatted error messages.
61
     *
62
     * @param $message
63
     * @param string $status
64
     * @return array
65
     */
66
    private function response($message, $status = 'danger')
67
    {
68
        return array(
69
            'status' => $status,
70
            'message' => $message
71
        );
72
    }
73
74
        /**
75
     * check database type. If SQLite, then create the database file.
76
     */
77
    private function sqlite()
78
    {
79
        if(DB::connection() instanceof SQLiteConnection) {
80
            $database = DB::connection()->getDatabaseName();
81
            if(!file_exists($database)) {
82
                touch($database);
83
                DB::reconnect(Config::get('database.default'));
84
            }
85
        }
86
    }
87
}
88