Completed
Push — master ( 29afc4...76b08e )
by Ryuichi
03:42
created

ConnectionManager::initialize()   C

Complexity

Conditions 8
Paths 9

Size

Total Lines 45
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 8.1678

Importance

Changes 0
Metric Value
cc 8
eloc 30
nc 9
nop 1
dl 0
loc 45
ccs 25
cts 29
cp 0.8621
crap 8.1678
rs 5.3846
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\IO\File;
0 ignored issues
show
Bug introduced by
The type WebStream\IO\File 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\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...
7
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...
8
9
/**
10
 * ConnectionManager
11
 * @author Ryuichi TANAKA.
12
 * @since 2014/06/13
13
 * @version 0.4
14
 */
15
class ConnectionManager
16
{
17
    /**
18
     * @var array<string> クラスパス-DSNハッシュマップ
19
     */
20
    private $classpathMap;
21
22
    /**
23
     * @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...
24
     */
25
    private $connectionContainer;
26
27
    /**
28
     * constructor
29
     * @param Container 依存コンテナ
30
     */
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...
31 39
    public function __construct(Container $container)
32
    {
33 39
        $this->initialize($container);
34
    }
35
36
    /**
37
     * destructor
38
     */
39
    public function __destruct()
40
    {
41
        $this->connectionContainer = null;
42
    }
43
44
    /**
45
     * DBコネクションを返却する
46
     * @param string Modelクラスファイルパス
47
     * @return DatabaseDriver データベースドライバインスタンス
48
     */
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...
49 39
    public function getConnection($filepath)
50
    {
51 39
        $dsnHash = $this->classpathMap[$filepath];
52
53 39
        return $dsnHash !== null ? $this->connectionContainer->{$dsnHash} : null;
0 ignored issues
show
introduced by
The condition $dsnHash !== null is always true.
Loading history...
54
    }
55
56
    /**
57
     * 初期処理
58
     * @param Container 依存コンテナ
59
     */
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...
60 39
    private function initialize(Container $container)
61
    {
62 39
        $this->classpathMap = [];
63 39
        $this->connectionContainer = new Container();
64 39
        $logger = $container->logger;
65
        $innerException = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $innerException is dead and can be removed.
Loading history...
66
67 39
        foreach ($container->connectionContainerList as $connectionContainer) {
68 39
            $config = null;
69 39
            $configFile = new File($connectionContainer->configPath);
70
71 39
            if (!$configFile->exists()) {
72
                throw new DatabaseException("Database configuration file is not found: " . $configFile->getFilePath());
73
            }
74
75 39
            $ext = $configFile->getFileExtension();
76 39
            if ($ext === 'ini') {
77
                $config = parse_ini_file($configFile->getFilePath());
78 39
            } elseif ($ext === 'yml' || $ext === 'yaml') {
79 39
                $config = \Spyc::YAMLLoad($configFile->getFilePath());
80
            } else {
81
                throw new DatabaseException("Yaml or ini file only available database configuration file.");
82
            }
83
84 39
            $driverClassPath = $connectionContainer->driverClassPath;
85
86 39
            if (!class_exists($driverClassPath)) {
87
                throw new ClassNotFoundException("$driverClassPath is not defined.");
88
            }
89
90 39
            $dsnHash = "";
91 39
            $databaseConfigContainer = new Container(false);
92 39
            foreach ($config as $key => $value) {
93 39
                $dsnHash .= $key . $value;
94 39
                $databaseConfigContainer->set($key, $value);
95
            }
96 39
            $dsnHash = md5($dsnHash);
97
98 39
            $this->classpathMap[$connectionContainer->filepath] = $dsnHash;
99
100 39
            $this->connectionContainer->{$dsnHash} = function () use ($driverClassPath, $databaseConfigContainer, $logger) {
101 39
                $driver = new $driverClassPath($databaseConfigContainer);
102 39
                $driver->inject('logger', $logger);
103
104 39
                return $driver;
105 39
            };
106
        }
107
    }
108
}
109