Completed
Push — master ( e14e42...11077c )
by Derek Stephen
01:18
created

DbPackage::addToContainer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 0
cts 19
cp 0
rs 9.568
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php declare(strict_types=1);
2
3
namespace Bone\Db;
4
5
use Barnacle\Container;
6
use Barnacle\RegistrationInterface;
7
use Barnacle\Exception\NotFoundException;
8
use PDO;
9
10
class DbPackage implements RegistrationInterface
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: getEntityPath, hasEntityPath
Loading history...
11
{
12
    /**
13
     * @param Container $c
14
     */
15
    public function addToContainer(Container $c)
16
    {
17
        if ($c->has('db')) {
18
            $c[PDO::class] = $c->factory(function (Container $c): PDO {
19
                $credentials = $c->get('db');
20
                $host = $credentials['host'];
21
                $db = $credentials['database'];
22
                $user = $credentials['user'];
23
                $pass = $credentials['pass'];
24
25
                $dbConnection = new PDO('mysql:host=' . $host . ';dbname=' . $db, $user, $pass, [
26
                    PDO::ATTR_EMULATE_PREPARES => false,
27
                    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
28
                ]);
29
30
                return $dbConnection;
31
            });
32
        } else {
33
            throw new NotFoundException('DbPackage is registered but there is no db config. See the 
34
            delboy1978uk/bone-db README.', 418);
35
        }
36
    }
37
}
38