StorageManager::instance()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 6
1
<?php
2
/*
3
 * This file is part of the Borobudur-Cqrs package.
4
 *
5
 * (c) Hexacodelabs <http://hexacodelabs.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Borobudur\Cqrs\ReadModel\Storage;
12
13
use Borobudur\Cqrs\Exception\InvalidArgumentException;
14
use Borobudur\Cqrs\ReadModel\Storage\Pdo\Pdo;
15
use Borobudur\Cqrs\ReadModel\Storage\Pdo\PdoConfig;
16
17
/**
18
 * @author      Iqbal Maulana <[email protected]>
19
 * @created     8/19/15
20
 */
21
class StorageManager
22
{
23
    /**
24
     * @var array
25
     */
26
    private static $pdo = array('mysql', 'pgsql');
27
28
    /**
29
     * @var StorageInterface[]
30
     */
31
    private static $instances = array();
32
33
    /**
34
     * @var StorageInterface
35
     */
36
    private $engine;
37
38
    /**
39
     * Private constructor.
40
     *
41
     * @param string      $engine
42
     * @param string      $host
43
     * @param int         $port
44
     * @param string      $database
45
     * @param string|null $user
46
     * @param string|null $pass
47
     */
48
    private function __construct($engine, $host, $port, $database, $user = null, $pass = null)
49
    {
50
        call_user_func_array(array($this, 'buildStorage'), func_get_args());
51
    }
52
53
    /**
54
     * Build singleton instance storage engine.
55
     *
56
     * @param string      $engine
57
     * @param string      $host
58
     * @param int         $port
59
     * @param string      $database
60
     * @param string|null $user
61
     * @param string|null $pass
62
     *
63
     * @return StorageInterface
64
     */
65
    public static function instance($engine, $host, $port, $database, $user = null, $pass = null)
66
    {
67
        if (!isset(self::$instances[$engine])) {
68
            $engine = new StorageManager($engine, $host, $port, $database, $user, $pass);
69
70
            return $engine->getEngine();
71
        }
72
73
        return self::$instances[$engine];
74
    }
75
76
    /**
77
     * Get storage engine.
78
     *
79
     * @return StorageInterface
80
     */
81
    public function getEngine()
82
    {
83
        return $this->engine;
84
    }
85
86
    /**
87
     * @param string      $engine
88
     * @param string      $host
89
     * @param int         $port
90
     * @param string      $database
91
     * @param string|null $user
92
     * @param string|null $pass
93
     */
94
    private function buildStorage($engine, $host, $port, $database, $user, $pass)
95
    {
96
        if (in_array($engine, self::$pdo)) {
97
            $config = new PdoConfig($engine, $host, $port, $database, $user, $pass);
98
            self::$instances[$engine] = $this->engine = new Pdo($config);
99
100
            return;
101
        }
102
103
        throw new InvalidArgumentException(sprintf('Storage engine "%s" not found.', $engine));
104
    }
105
}
106