Passed
Pull Request — master (#55)
by Rafael
07:07
created

DatabaseProvider::register()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2.0145

Importance

Changes 0
Metric Value
cc 2
eloc 15
nc 1
nop 1
dl 0
loc 23
ccs 11
cts 13
cp 0.8462
crap 2.0145
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gewaer\Providers;
6
7
use function Gewaer\Core\envValue;
8
use Phalcon\Db\Adapter\Pdo\Mysql;
9
use Phalcon\Di\ServiceProviderInterface;
10
use Phalcon\DiInterface;
11
use PDOException;
12
use Gewaer\Exception\ServerErrorHttpException;
13
14
class DatabaseProvider implements ServiceProviderInterface
15
{
16
    /**
17
     * @param DiInterface $container
18
     */
19 88
    public function register(DiInterface $container)
20
    {
21 88
        $container->setShared(
22 88
            'db',
23
            function () {
24
                $options = [
25 85
                    'host' => envValue('DATA_API_MYSQL_HOST', 'localhost'),
26 85
                    'username' => envValue('DATA_API_MYSQL_USER', 'nanobox'),
27 85
                    'password' => envValue('DATA_API_MYSQL_PASS', ''),
28 85
                    'dbname' => envValue('DATA_API_MYSQL_NAME', 'gonano'),
29 85
                    'charset' => 'utf8',
30
                ];
31
32
                try {
33 85
                    $connection = new Mysql($options);
34
35
                    // Set everything to UTF8
36 85
                    $connection->execute('SET NAMES utf8mb4', []);
37
                } catch (PDOException $e) {
38
                    throw new ServerErrorHttpException($e->getMessage());
39
                }
40
41 85
                return $connection;
42 88
            }
43
        );
44 88
    }
45
}
46