Passed
Push — master ( 76b08e...5cd6b2 )
by Ryuichi
58:40 queued 56:20
created

ConnectionManager   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Test Coverage

Coverage 82.05%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
c 1
b 0
f 0
dl 0
loc 90
ccs 32
cts 39
cp 0.8205
rs 10
wmc 12

4 Methods

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