Completed
Push — master ( 198a3e...84e362 )
by Rougin
10:23
created

Credo::connection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 4
cts 4
cp 1
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 10
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Rougin\Credo;
4
5
use CI_DB_query_builder as Builder;
6
use Doctrine\ORM\EntityManager;
7
use Doctrine\ORM\Tools\Setup;
8
use Rougin\Credo\Helpers\MethodHelper;
9
10
/**
11
 * Credo
12
 *
13
 * @package Credo
14
 * @author  Rougin Royce Gutib <[email protected]>
15
 *
16
 * @method \Doctrine\ORM\EntityRepository             getRepository(string $entityName)
17
 * @method \Doctrine\ORM\Mapping\ClassMetadataFactory getMetadataFactory()
18
 * @method \Doctrine\ORM\Query\QueryBuilder           createQueryBuilder()
19
 * @method object                                     refresh($entity)
20
 * @method void                                       flush(object|array $entity = null)
21
 * @method void                                       remove(object $entity)
22
 */
23
class Credo
24
{
25
    /**
26
     * @var \Doctrine\ORM\EntityManager
27
     */
28
    protected $manager;
29
30
    /**
31
     * Initializes the EntityManager instance.
32
     *
33
     * @param \CI_DB_query_builder $builder
34 9
     */
35
    public function __construct(Builder $builder)
36 9
    {
37 9
        $connection = $this->connection($builder);
38 9
39
        if ($connection['driver'] == 'pdo' && strpos($connection['dsn'], ':') !== false) {
40
            $keys = explode(':', $connection['dsn']);
41 9
42
            $connection['driver'] .= '_' . $keys[0];
43 9
        }
44
45 9
        if ($connection['driver'] == 'pdo_sqlite') {
46 9
            $connection['path'] = str_replace('sqlite:', '', $connection['dsn']);
47
        }
48
49
        $directories = array(APPPATH . 'models', APPPATH . 'repositories');
50
51
        // Set $ci->db->db_debug to TRUE to disable caching while you develop
52
        $config = Setup::createAnnotationMetadataConfiguration($directories, $builder->db_debug, APPPATH . 'models/proxies');
53
54 9
        $this->manager = EntityManager::create($connection, $config);
55
    }
56 9
57 9
    /**
58
     * Sets up the database configuration from CodeIgniter.
59 9
     *
60 9
     * @param  \CI_DB_query_builder $builder
61
     * @return array
62 9
     */
63 9
    protected function connection(Builder $builder)
64 9
    {
65
        $connection = [
66 9
            'dsn'      => $builder->dsn,
67
            'driver'   => $builder->dbdriver,
68
            'user'     => $builder->username,
69
            'password' => $builder->password,
70
            'host'     => $builder->hostname,
71
            'dbname'   => $builder->database,
72
            'charset'  => $builder->char_set,
73
        ];
74
75 9
        return $connection;
76
    }
77
78 9
    /**
79 9
     * Calls methods from EntityManager in underscore case.
80 9
     *
81 9
     * @param  string $method
82 9
     * @param  mixed  $parameters
83 9
     * @return mixed
84 9
     */
85 9
    public function __call($method, $parameters)
86
    {
87 9
        return MethodHelper::call($this->manager, $method, $parameters);
88
    }
89
}
90