MongoManager   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 4
dl 0
loc 149
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A addDatabase() 0 10 2
A createDatabase() 0 18 1
B database() 0 30 4
A getDatabases() 0 11 2
A createInjection() 0 4 1
1
<?php
2
/**
3
 * Spiral, Core Components
4
 *
5
 * @author Wolfy-J
6
 */
7
8
namespace Spiral\ODM;
9
10
use MongoDB\Driver\Manager;
11
use Spiral\Core\Container;
12
use Spiral\Core\Container\InjectorInterface;
13
use Spiral\Core\Container\SingletonInterface;
14
use Spiral\Core\FactoryInterface;
15
use Spiral\ODM\Configs\MongoConfig;
16
use Spiral\ODM\Entities\MongoDatabase;
17
use Spiral\ODM\Exceptions\ODMException;
18
19
/**
20
 * Manages set of mongo databases and injects them into container when needed.
21
 */
22
class MongoManager implements InjectorInterface, SingletonInterface
23
{
24
    /**
25
     * @var MongoDatabase[]
26
     */
27
    private $databases = [];
28
29
    /**
30
     * @var MongoConfig
31
     */
32
    protected $config;
33
34
    /**
35
     * @var FactoryInterface
36
     */
37
    protected $factory;
38
39
    /**
40
     * @param MongoConfig      $config
41
     * @param FactoryInterface $factory
42
     */
43
    public function __construct(MongoConfig $config, FactoryInterface $factory = null)
44
    {
45
        $this->config = $config;
46
        $this->factory = $factory ?? new Container();
47
    }
48
49
    /**
50
     * Add new database in a database pull, database name will be automatically selected from given
51
     * instance.
52
     *
53
     * @param string        $name Internal database name.
54
     * @param MongoDatabase $database
55
     *
56
     * @return self|$this
57
     *
58
     * @throws ODMException
59
     */
60
    public function addDatabase(string $name, MongoDatabase $database): MongoManager
61
    {
62
        if (isset($this->databases[$name])) {
63
            throw new ODMException("Database '{$name}' already exists");
64
        }
65
66
        $this->databases[$name] = $database;
67
68
        return $this;
69
    }
70
71
    /**
72
     * Register new mongo database using given name and connection options (compatible with MongoDB
73
     * class).
74
     *
75
     * @param string $name     Internal database name (for injections and etc).
76
     * @param string $server   Server uri.
77
     * @param string $database Database name.
78
     * @param array  $driverOptions
79
     * @param array  $options  Database options.
80
     *
81
     * @return MongoDatabase
82
     */
83
    public function createDatabase(
84
        string $name,
85
        string $server,
86
        string $database,
87
        array $driverOptions = [],
88
        array $options = []
89
    ): MongoDatabase {
90
        //Database will be automatically connected here.
91
        $instance = $this->factory->make(MongoDatabase::class, [
92
            'databaseName' => $database,
93
            'manager'      => new Manager($server, $options, $driverOptions),
94
            'options'      => $options
95
        ]);
96
97
        $this->addDatabase($name, $instance);
98
99
        return $instance;
100
    }
101
102
    /**
103
     * Create specified or select default instance of MongoDatabase.
104
     *
105
     * @param string $database Database name (internal).
106
     *
107
     * @return MongoDatabase
108
     *
109
     * @throws ODMException
110
     */
111
    public function database(string $database = null): MongoDatabase
112
    {
113
        if (empty($database)) {
114
            $database = $this->config->defaultDatabase();
115
        }
116
117
        //Spiral support ability to link multiple virtual databases together using aliases
118
        $database = $this->config->resolveAlias($database);
119
120
        if (isset($this->databases[$database])) {
121
            return $this->databases[$database];
122
        }
123
124
        if (!$this->config->hasDatabase($database)) {
125
            throw new ODMException(
126
                "Unable to initiate MongoDatabase, no presets for '{$database}' found"
127
            );
128
        }
129
130
        $options = $this->config->databaseOptions($database);
131
132
        //Initiating database instance
133
        return $this->createDatabase(
134
            $database,
135
            $options['server'],
136
            $options['database'],
137
            $options['driverOptions'] ?? [],
138
            $options['options'] ?? []
139
        );
140
    }
141
142
    /**
143
     * Get every know database.
144
     *
145
     * @return MongoDatabase[]
146
     */
147
    public function getDatabases(): array
148
    {
149
        $result = [];
150
151
        //Include manually added databases
152
        foreach ($this->config->databaseNames() as $name) {
153
            $result[] = $this->database($name);
154
        }
155
156
        return $result;
157
    }
158
159
    /**
160
     * Automatic injection of MongoDatabase.
161
     *
162
     * {@inheritdoc}
163
     *
164
     * @return MongoDatabase
165
     */
166
    public function createInjection(\ReflectionClass $class, string $context = null)
167
    {
168
        return $this->database($context);
169
    }
170
}