Completed
Push — master ( c6aea9...538fb8 )
by Flo
05:04 queued 02:10
created

ORMFactory::createService()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 39
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 24
nc 6
nop 1
1
<?php
2
/**
3
 * Class ORMFactory | ORMFactory.php
4
 * @package Faulancer\Service\Factory
5
 * @author  Florian Knapp <[email protected]>
6
 */
7
namespace Faulancer\Service\Factory;
8
9
use ORM\DbConfig;
10
use ORM\EntityManager;
11
use Faulancer\Service\Config;
12
use Faulancer\Service\ORM;
13
use Faulancer\ServiceLocator\FactoryInterface;
14
use Faulancer\ServiceLocator\ServiceLocator;
15
use Faulancer\ServiceLocator\ServiceLocatorInterface;
16
17
/**
18
 * Class ORMFactory
19
 */
20
class ORMFactory implements FactoryInterface
21
{
22
23
    /**
24
     * @param ServiceLocatorInterface $serviceLocator
25
     * @return ORM
26
     * @codeCoverageIgnore
27
     */
28
    public function createService(ServiceLocatorInterface $serviceLocator)
29
    {
30
        /** @var Config $config */
31
        $config = ServiceLocator::instance()->get(Config::class);
32
33
        $type = $config->get('db:type');
34
        $name = $config->get('db:name');
35
        $user = $config->get('db:username');
36
        $pass = $config->get('db:password');
37
        $host = $config->get('db:host') ?: 'localhost';
38
        $port = '';
39
40
        $attributes = [];
41
42
        if ($type === 'mysql') {
43
44
            $attributes = [
45
                \PDO::MYSQL_ATTR_INIT_COMMAND => "SET sql_mode ='ANSI_QUOTES', NAMES utf8",
46
                \PDO::ATTR_EMULATE_PREPARES   => false,
47
            ];
48
49
            $port = $config->get('db:port') ?: 3306;
50
51
        }
52
53
        $dbConf = new DbConfig(
54
            $type,
55
            $name,
56
            $user,
57
            $pass,
58
            $host,
59
            $port,
60
            $attributes
61
        );
62
63
        $entityManager = new EntityManager([EntityManager::OPT_CONNECTION => $dbConf]);
64
65
        return new ORM($entityManager);
66
    }
67
68
}