App::setupDatabaseSchema()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
/**
4
 * @author: Raimi Ademola <[email protected]>
5
 * @copyright: 2016 Andela
6
 */
7
namespace Demo;
8
9
use Exception;
10
use Dotenv\Dotenv;
11
use Illuminate\Database\Schema\Blueprint;
12
use Illuminate\Database\Capsule\Manager as Capsule;
13
14
class App
15
{
16
    protected $app;
17
    protected $schema;
18
    protected $capsule;
19
20
    /**
21
     * This is a constructor; a default method  that will be called automatically during slim app instantiation.
22
     */
23
    public function __construct($path = null)
24
    {
25
        $settings = require  __DIR__.'/settings.php';
26
        $app = new \Slim\App($settings);
27
        // Set up dependencies
28
        require  __DIR__.'/dependencies.php';
29
        // Register routes
30
        require  __DIR__.'/routes.php';
31
        $this->app = $app;
32
        $this->capsule = new Capsule();
33
        $this->schema = new DatabaseSchema();
34
        $this->loadEnv($path); 
35
        $this->setUpDatabaseManager();
36
        $this->setupDatabaseSchema();
37
    }
38
39
    /**
40
     * Setup Eloquent ORM.
41
     */
42
    private function setUpDatabaseManager()
43
    {
44
        //Register the database connection with Eloquent
45
        $config = [
46
            'driver'    => getenv('driver'),
47
            'host'      => getenv('host'),
48
            'database'  => getenv('database'),
49
            'username'  => getenv('username'),
50
            'password'  => getenv('password'),
51
            'charset'   => 'utf8',
52
            'collation' => 'utf8_unicode_ci',
53
        ];
54
55
        $this->capsule->addConnection($config);
56
        $this->capsule->setAsGlobal();
57
        $this->capsule->bootEloquent();
58
    }
59
60
    /**
61
     * Create necessary database tables needed in the application.
62
     */
63
    public function setupDatabaseSchema()
64
    {
65
        $this->schema->createUsersTable();
66
        $this->schema->createEmojisTable();
67
        $this->schema->createKeywordsTable();
68
    }
69
70
    /**
71
     * Load Dotenv to grant getenv() access to environment variables in .env file.
72
     */
73
    public function loadEnv($path = null)
74
    {
75
        $path = $path == null ? __DIR__ . '/../' : $path;
76
        $dotenv = new Dotenv($path);
77
        $dotenv->load();
78
    }
79
80
    /**
81
     * Get an instance of the application.
82
     *
83
     * @return \Slim\App
84
     */
85
    public function get()
86
    {
87
        return $this->app;
88
    }
89
}
90