Completed
Branch master (e3a0da)
by
unknown
15:36
created

LoadEnvVariablesTrait::loadDbEnv()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4286
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
3
namespace Opeyemiabiodun\PotatoORM\Connections;
4
5
use Exception;
6
use Dotenv\Dotenv;
7
use RuntimeException;
8
9
trait LoadEnvVariablesTrait
10
{
11
    /**
12
     * $_database The database name.
13
     *
14
     * @var string
15
     */
16
    private $_database;
17
18
    /**
19
     * $_host The host name.
20
     *
21
     * @var string
22
     */
23
    private $_host;
24
25
    /**
26
     * $_password The password to the database server.
27
     *
28
     * @var string
29
     */
30
    private $_password;
31
32
    /**
33
     * $_pdo The PDO instance of the connection.
34
     *
35
     * @var PDO
36
     */
37
    private $_pdo;
38
39
    /**
40
     *  $_port The port number to the database server.
41
     *
42
     * @var string
43
     */
44
    private $_port;
45
46
    /**
47
     * The username to the database server.
48
     *
49
     * @var string
50
     */
51
    private $_username;
52
53
    /**
54
     * Loads variables in the .env file.
55
     *
56
     * @return void
57
     */
58
    private function loadDbEnv()
59
    {
60
        $dotenv = new Dotenv(__DIR__.'/../..');
61
        $dotenv->load();
62
63
        $dotenv->required(['DB_HOST', 'DB_DATABASE', 'DB_USERNAME', 'DB_PASSWORD', 'DB_PORT'])->notEmpty();
64
65
        $this->_host = getenv('DB_HOST');
66
        $this->_database = getenv('DB_DATABASE');
67
        $this->_username = getenv('DB_USERNAME');
68
        $this->_password = getenv('DB_PASSWORD');
69
        $this->_port = getenv('DB_PORT');
70
    }
71
72
    /**
73
     * Loads variables in the .env file and handles exceptions.
74
     *
75
     * @return void
76
     */
77
    private function useDbEnv()
78
    {
79
        try {
80
            $this->loadDbEnv();
81
        } catch (RuntimeException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
82
        } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
83
        }
84
    }
85
}