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

DatabaseProvider   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 84.62%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 28
ccs 11
cts 13
cp 0.8462
rs 10
c 0
b 0
f 0
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A register() 0 23 2
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 Exception;
12
use PDOException;
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 Exception($e->getMessage());
39
                }
40
41 85
                return $connection;
42 88
            }
43
        );
44 88
    }
45
}
46