Completed
Push — master ( 2ade39...0f03be )
by Ryuichi
02:56
created

ConnectionManager::getConnection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace WebStream\Database;
3
4
use WebStream\Container\Container;
0 ignored issues
show
Bug introduced by
The type WebStream\Container\Container was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
5
use WebStream\Exception\Extend\ClassNotFoundException;
0 ignored issues
show
Bug introduced by
The type WebStream\Exception\Extend\ClassNotFoundException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use WebStream\Exception\Extend\DatabaseException;
0 ignored issues
show
Bug introduced by
The type WebStream\Exception\Extend\DatabaseException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
8
/**
9
 * ConnectionManager
10
 * @author Ryuichi TANAKA.
11
 * @since 2014/06/13
12
 * @version 0.4
13
 */
14
class ConnectionManager
15
{
16
    /**
17
     * @var array<string> クラスパス-DSNハッシュマップ
18
     */
19
    private $classpathMap;
20
21
    /**
22
     * @var AnnotationContainer データベース接続項目コンテナ
0 ignored issues
show
Bug introduced by
The type WebStream\Database\AnnotationContainer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
     */
24
    private $connectionContainer;
25
26
    /**
27
     * constructor
28
     * @param Container 依存コンテナ
29
     */
0 ignored issues
show
Documentation Bug introduced by
The doc comment 依存コンテナ at position 0 could not be parsed: Unknown type name '依存コンテナ' at position 0 in 依存コンテナ.
Loading history...
30
    public function __construct(Container $container)
31
    {
32
        $this->initialize($container);
33
    }
34
35
    /**
36
     * destructor
37
     */
38
    public function __destruct()
39
    {
40
        $this->connectionContainer = null;
41
    }
42
43
    /**
44
     * DBコネクションを返却する
45
     * @param string Modelクラスファイルパス
46
     * @return DatabaseDriver データベースドライバインスタンス
47
     */
0 ignored issues
show
Documentation Bug introduced by
The doc comment Modelクラスファイルパス at position 0 could not be parsed: Unknown type name 'Modelクラスファイルパス' at position 0 in Modelクラスファイルパス.
Loading history...
48
    public function getConnection($filepath)
49
    {
50
        $dsnHash = $this->classpathMap[$filepath];
51
52
        return $dsnHash !== null ? $this->connectionContainer->{$dsnHash} : null;
53
    }
54
55
    /**
56
     * 初期処理
57
     * @param Container 依存コンテナ
58
     */
0 ignored issues
show
Documentation Bug introduced by
The doc comment 依存コンテナ at position 0 could not be parsed: Unknown type name '依存コンテナ' at position 0 in 依存コンテナ.
Loading history...
59
    private function initialize(Container $container)
60
    {
61
        $this->classpathMap = [];
62
        $this->connectionContainer = new Container();
63
        $logger = $container->logger;
64
        $innerException = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $innerException is dead and can be removed.
Loading history...
65
66
        foreach ($container->connectionContainerList as $container) {
67
            $config = null;
68
            $ext = pathinfo($container->configPath, PATHINFO_EXTENSION);
69
            if ($ext === 'ini') {
70
                $config = parse_ini_file($container->configPath);
71
            } elseif ($ext === 'yml' || $ext === 'yaml') {
72
                $config = \Spyc::YAMLLoad($container->configPath);
73
            } else {
74
                throw new DatabaseException("Yaml or ini file only available database configuration file.");
75
            }
76
77
            $driverClassPath = $container->driverClassPath;
78
79
            if (!class_exists($driverClassPath)) {
80
                throw new ClassNotFoundException("$driverClassPath is not defined.");
81
            }
82
83
            $dsnHash = "";
84
            $databaseConfigContainer = new Container(false);
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $databaseConfigContainer exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
85
            foreach ($config as $key => $value) {
86
                $dsnHash .= $key . $value;
87
                $databaseConfigContainer->set($key, $value);
88
            }
89
            $dsnHash = md5($dsnHash);
90
91
            $this->classpathMap[$container->filepath] = $dsnHash;
92
93
            $this->connectionContainer->{$dsnHash} = function () use ($driverClassPath, $databaseConfigContainer, $logger) {
94
                $driver = new $driverClassPath($databaseConfigContainer);
95
                $driver->inject('logger', $logger);
96
97
                return $driver;
98
            };
99
        }
100
    }
101
}
102