Completed
Push — master ( 8e5e55...6fad72 )
by Iqbal
10:57
created

AbstractStorageManager   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
c 1
b 0
f 1
lcom 1
cbo 2
dl 0
loc 72
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A instance() 0 8 2
A buildInstance() 0 8 2
A supportEngines() 0 4 1
A buildEngine() 0 4 1
1
<?php
2
3
namespace Borobudur\EventSourcing\Storage;
4
5
use Borobudur\EventSourcing\Exception\InvalidArgumentException;
6
use Borobudur\EventSourcing\Exception\RuntimeException;
7
use Borobudur\EventSourcing\Snapshot\SnapshotInterface;
8
9
/**
10
 * @author      Iqbal Maulana <[email protected]>
11
 * @created     8/22/16
12
 */
13
abstract class AbstractStorageManager
14
{
15
    /**
16
     * @var StorageInterface|SnapshotInterface[]
17
     */
18
    protected static $instances = array();
19
20
    /**
21
     * Build or get an engine if already built.
22
     *
23
     * @param string      $engine
24
     * @param string      $database
25
     * @param string|null $host
26
     * @param string|null $user
27
     * @param string|null $pass
28
     * @param int|null    $port
29
     *
30
     * @return StorageInterface|SnapshotInterface
31
     */
32
    final public static function instance($engine, $database, $host = null, $user = null, $pass = null, $port = null)
33
    {
34
        if (!isset(static::$instances[$engine])) {
35
            static::buildInstance($engine, $host, $port, $database, $user, $pass);
36
        }
37
38
        return static::$instances[$engine];
39
    }
40
41
    /**
42
     * Build and register the engine.
43
     *
44
     * @param string $engine
45
     * @param string $host
46
     * @param int    $port
47
     * @param string $database
48
     * @param string $user
49
     * @param string $pass
50
     *
51
     * @throws InvalidArgumentException
52
     */
53
    protected static function buildInstance($engine, $host, $port, $database, $user, $pass)
54
    {
55
        if (in_array($engine, static::supportEngines())) {
56
            static::$instances[$engine] = static::buildEngine($engine, $host, $port, $database, $user, $pass);
57
        } else {
58
            throw new InvalidArgumentException(sprintf('Storage engine "%s" not found.', $engine));
59
        }
60
    }
61
62
    /**
63
     * @return array
64
     */
65
    protected static function supportEngines()
66
    {
67
        return array();
68
    }
69
70
    /**
71
     * @param string $engine
72
     * @param string $host
73
     * @param int    $port
74
     * @param string $database
75
     * @param string $user
76
     * @param string $pass
77
     *
78
     * @return mixed
79
     */
80
    protected static function buildEngine($engine, $host, $port, $database, $user, $pass)
81
    {
82
        throw new RuntimeException('There are no implement of build engine.');
83
    }
84
}
85