Issues (12)

src/Helpers/DatabaseManager.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Turahe\LaravelInstaller\Helpers;
4
5
use Exception;
6
use Illuminate\Support\Facades\DB;
7
use Illuminate\Support\Facades\Config;
8
use Illuminate\Support\Facades\Artisan;
9
use Illuminate\Database\SQLiteConnection;
10
use Symfony\Component\Console\Output\BufferedOutput;
11
12
/**
13
 * Class DatabaseManager.
14
 */
15
class DatabaseManager
16
{
17
    /**
18
     * Migrate and seed the database.
19
     *
20
     * @return array
21
     */
22
    public function migrateAndSeed()
23
    {
24
        $outputLog = new BufferedOutput;
25
26
        $this->sqlite($outputLog);
27
28
        return $this->migrate($outputLog);
29
    }
30
31
    /**
32
     * Run the migration and call the seeder.
33
     *
34
     * @param BufferedOutput $outputLog
35
     * @return array
36
     */
37
    private function migrate(BufferedOutput $outputLog)
38
    {
39
        try {
40
            Artisan::call('migrate', ['--force'=> true], $outputLog);
41
        } catch (Exception $e) {
42
            return $this->response($e->getMessage(), 'error', $outputLog);
43
        }
44
45
        return $this->seed($outputLog);
46
    }
47
48
    /**
49
     * Seed the database.
50
     *
51
     * @param BufferedOutput $outputLog
52
     * @return array
53
     */
54
    private function seed(BufferedOutput $outputLog)
55
    {
56
        try {
57
            Artisan::call('db:seed', ['--force' => true], $outputLog);
58
        } catch (Exception $e) {
59
            return $this->response($e->getMessage(), 'error', $outputLog);
60
        }
61
62
        return $this->response(trans('installer_messages.final.finished'), 'success', $outputLog);
0 ignored issues
show
It seems like trans('installer_messages.final.finished') can also be of type array and array; however, parameter $message of Turahe\LaravelInstaller\...baseManager::response() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

62
        return $this->response(/** @scrutinizer ignore-type */ trans('installer_messages.final.finished'), 'success', $outputLog);
Loading history...
63
    }
64
65
    /**
66
     * Return a formatted error messages.
67
     *
68
     * @param string $message
69
     * @param string $status
70
     * @param BufferedOutput $outputLog
71
     * @return array
72
     */
73
    private function response($message, $status, BufferedOutput $outputLog)
74
    {
75
        return [
76
            'status' => $status,
77
            'message' => $message,
78
            'dbOutputLog' => $outputLog->fetch(),
79
        ];
80
    }
81
82
    /**
83
     * Check database type. If SQLite, then create the database file.
84
     *
85
     * @param BufferedOutput $outputLog
86
     */
87
    private function sqlite(BufferedOutput $outputLog)
88
    {
89
        if (DB::connection() instanceof SQLiteConnection) {
90
            $database = DB::connection()->getDatabaseName();
91
            if (! file_exists($database)) {
92
                touch($database);
93
                DB::reconnect(Config::get('database.default'));
94
            }
95
            $outputLog->write('Using SqlLite database: '.$database, 1);
96
        }
97
    }
98
}
99