DatabaseConnection   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 4
c 3
b 1
f 0
lcom 1
cbo 1
dl 0
loc 53
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A setUpDatabase() 0 19 1
A loadEnv() 0 7 2
1
<?php
2
/**
3
 * @author   Temitope Olotin <[email protected]>
4
 * @license  <https://opensource.org/license/MIT> MIT
5
 */
6
namespace Laztopaz\EmojiRestfulAPI;
7
8
use Dotenv\Dotenv;
9
10
class DatabaseConnection
11
{
12
    private $capsule;
13
14
    /**
15
     * This constructor accept Capsule; a connection
16
     * string for connecting to the database.
17
     *
18
     * @param $capsule
19
     */
20
    public function __construct($capsule)
21
    {
22
        $this->capsule = $capsule;
23
        self::loadEnv();
24
        $this->setUpDatabase();
25
    }
26
27
    /**
28
     * This method setup the PDO database connection and also
29
     * start the database connection.
30
     */
31
    private function setUpDatabase()
32
    {
33
        $this->capsule->addConnection(
34
            [
35
                'driver'    => getenv('driver'),
36
                'host'      => getenv('host'),
37
                'database'  => getenv('database'),
38
                'username'  => getenv('username'),
39
                'password'  => getenv('password'),
40
                'charset'   => 'utf8',
41
                'collation' => 'utf8_unicode_ci',
42
                'port'      => getenv('port'),
43
                'prefix'    => '',
44
                'strict'    => true,
45
            ]);
46
47
        $this->capsule->setAsGlobal();
48
        $this->capsule->bootEloquent();
49
    }
50
51
     /**
52
      * Load Dotenv to grant getenv() access to
53
      * environment variables in .env file.
54
      */
55
     public static function loadEnv()
56
     {
57
         if (! getenv('APP_ENV')) {
58
             $dotenv = new Dotenv(__DIR__.'/../../');
59
             $dotenv->load();
60
         }
61
     }
62
}
63