Completed
Push — master ( 3a5c6d...7f7360 )
by Emmanuel
02:10
created

DBTrait::connectToDB()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 4
nop 4
crap 3
1
<?php
2
/**
3
 * neuralyzer : Data Anonymization Library and CLI Tool
4
 *
5
 * PHP Version 7.0
6
 *
7
 * @author Emmanuel Dyan
8
 * @author Rémi Sauvat
9
 * @copyright 2017 Emmanuel Dyan
10
 *
11
 * @package edyan/neuralyzer
12
 *
13
 * @license GNU General Public License v2.0
14
 *
15
 * @link https://github.com/edyan/neuralyzer
16
 */
17
18
namespace Inet\Neuralyzer\Console\Commands;
19
20
/**
21
 * Trait with basic function to connect to DB
22
 */
23
trait DBTrait
24
{
25
    /**
26
     * PDO Object Initialized
27
     * @var \PDO
28
     */
29
    private $pdo;
30
31
    /**
32
     * Initialize a PDO Object
33
     */
34 4
    private function connectToDB(string $host, $dbName, string $user, $password)
35
    {
36
        // Throw an exception immediately if we dont have the required DB parameter
37 4
        if (empty($dbName)) {
38 1
            throw new \InvalidArgumentException('Database name is required (--db)');
39
        }
40
41
        try {
42 3
            $this->pdo = new \PDO("mysql:dbname=$dbName;host=" . $host, $user, $password);
43 2
            $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
44 1
        } catch (\Exception $e) {
45 1
            throw new \RuntimeException("Can't connect to the database. Check your credentials");
46
        }
47 2
    }
48
}
49