ManagerBuilder::getManagers()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
/*
4
 * Slim3 Doctrine integration (https://github.com/juliangut/slim-doctrine).
5
 *
6
 * @license BSD-3-Clause
7
 * @link https://github.com/juliangut/slim-doctrine
8
 * @author Julián Gutiérrez <[email protected]>
9
 */
10
11
namespace Jgut\Slim\Doctrine;
12
13
use Doctrine\Common\Annotations\AnnotationRegistry;
14
use Jgut\Doctrine\ManagerBuilder\AbstractBuilderCollection;
15
use Jgut\Doctrine\ManagerBuilder\CouchDBBuilder;
16
use Jgut\Doctrine\ManagerBuilder\ManagerBuilder as Builder;
17
use Jgut\Doctrine\ManagerBuilder\MongoDBBuilder;
18
use Jgut\Doctrine\ManagerBuilder\RelationalBuilder;
19
use Jgut\Doctrine\ManagerBuilder\Util\OptionsTrait;
20
use Symfony\Component\Console\Application;
21
22
/**
23
 * Slim-Doctrine managers integration.
24
 */
25
class ManagerBuilder extends AbstractBuilderCollection
26
{
27
    use OptionsTrait;
28
29
    const METADATA_MAPPING_ANNOTATION = Builder::METADATA_MAPPING_ANNOTATION;
30
    const METADATA_MAPPING_XML = Builder::METADATA_MAPPING_XML;
31
    const METADATA_MAPPING_YAML = Builder::METADATA_MAPPING_YAML;
32
    const METADATA_MAPPING_PHP = Builder::METADATA_MAPPING_PHP;
33
34
    const RELATIONAL_MANAGER_KEY = 'relational_manager_key';
35
    const MONGODB_MANAGER_KEY = 'mongodb_manager_key';
36
    const COUCHDB_MANAGER_KEY = 'couchdb_manager_key';
37
38
    const DEFAULT_RELATIONAL_MANAGER_KEY = 'entity_manager';
39
    const DEFAULT_MONGODB_MANAGER_KEY = 'mongodb_document_manager';
40
    const DEFAULT_COUCHDB_MANAGER_KEY = 'couchdb_document_manager';
41
42
    const RELATIONAL_MANAGER_NAME = 'relational_manager_name';
43
    const MONGODB_MANAGER_NAME = 'mongodb_manager_name';
44
    const COUCHDB_MANAGER_NAME = 'couchdb_manager_name';
45
46
    const DEFAULT_RELATIONAL_MANAGER_NAME = 'entityManager';
47
    const DEFAULT_MONGODB_MANAGER_NAME = 'mongoDocumentManager';
48
    const DEFAULT_COUCHDB_MANAGER_NAME = 'couchDocumentManager';
49
50
    /**
51
     * Global annotation loader control.
52
     *
53
     * @var bool
54
     */
55
    protected $globalLoaderRegister = true;
56
57
    /**
58
     * ManagerBuilder constructor.
59
     *
60
     * @param array $options
61
     */
62
    public function __construct(array $options = [])
63
    {
64
        $options = array_merge(
65
            [
66
                static::RELATIONAL_MANAGER_KEY => static::DEFAULT_RELATIONAL_MANAGER_KEY,
67
                static::MONGODB_MANAGER_KEY => static::DEFAULT_MONGODB_MANAGER_KEY,
68
                static::COUCHDB_MANAGER_KEY => static::DEFAULT_COUCHDB_MANAGER_KEY,
69
                static::RELATIONAL_MANAGER_NAME => static::DEFAULT_RELATIONAL_MANAGER_NAME,
70
                static::MONGODB_MANAGER_NAME => static::DEFAULT_MONGODB_MANAGER_NAME,
71
                static::COUCHDB_MANAGER_NAME => static::DEFAULT_COUCHDB_MANAGER_NAME,
72
            ],
73
            $options
74
        );
75
76
        $this->setOptions($options);
77
    }
78
79
    /**
80
     * Load Doctrine managers from settings array.
81
     *
82
     * @param array $settings
83
     *
84
     * @throws \RuntimeException
85
     *
86
     * @return $this
87
     */
88
    public function loadSettings(array $settings)
89
    {
90
        $relationalManagerKey = $this->getOption(static::RELATIONAL_MANAGER_KEY);
91
        if (array_key_exists($relationalManagerKey, $settings)) {
92
            $this->registerEntityManagers((array) $settings[$relationalManagerKey]);
93
        }
94
95
        $mongoDBManagerKey = $this->getOption(static::MONGODB_MANAGER_KEY);
96
        if (array_key_exists($mongoDBManagerKey, $settings)) {
97
            $this->registerMongoDBDocumentManagers((array) $settings[$mongoDBManagerKey]);
98
        }
99
100
        $couchDBManagerKey = $this->getOption(static::COUCHDB_MANAGER_KEY);
101
        if (array_key_exists($couchDBManagerKey, $settings)) {
102
            $this->registerCouchDBDocumentManagers((array) $settings[$couchDBManagerKey]);
103
        }
104
105
        return $this;
106
    }
107
108
    /**
109
     * Register ORM entity managers.
110
     *
111
     * @param array $settings
112
     *
113
     * @throws \RuntimeException
114
     */
115
    protected function registerEntityManagers(array $settings)
116
    {
117
        if (array_key_exists('connection', $settings)) {
118
            $settings = [$settings];
119
        }
120
121
        foreach ($settings as $name => $config) {
122
            if (!is_string($name)) {
123
                $name = $this->getOption(static::RELATIONAL_MANAGER_NAME);
124
            }
125
126
            $this->addBuilder(new RelationalBuilder($config, $name));
127
        }
128
    }
129
130
    /**
131
     * Register MongoDB ODM document managers.
132
     *
133
     * @param array $settings
134
     *
135
     * @throws \RuntimeException
136
     */
137
    protected function registerMongoDBDocumentManagers(array $settings)
138
    {
139
        if (array_key_exists('connection', $settings)) {
140
            $settings = [$settings];
141
        }
142
143
        foreach ($settings as $name => $config) {
144
            if (!is_string($name)) {
145
                $name = $this->getOption(static::MONGODB_MANAGER_NAME);
146
            }
147
148
            $this->addBuilder(new MongoDBBuilder($config, $name));
149
        }
150
    }
151
152
    /**
153
     * Register CouchDB ODM document managers.
154
     *
155
     * @param array $settings
156
     *
157
     * @throws \RuntimeException
158
     */
159
    protected function registerCouchDBDocumentManagers(array $settings)
160
    {
161
        if (array_key_exists('connection', $settings)) {
162
            $settings = [$settings];
163
        }
164
165
        foreach ($settings as $name => $config) {
166
            if (!is_string($name)) {
167
                $name = $this->getOption(static::COUCHDB_MANAGER_NAME);
168
            }
169
170
            $this->addBuilder(new CouchDBBuilder($config, $name));
171
        }
172
    }
173
174
    /**
175
     * Get registered builder's managers.
176
     *
177
     * @return \Doctrine\Common\Persistence\ObjectManager[]
178
     */
179
    public function getManagers()
180
    {
181
        $managers = array_map(
182
            function (Builder $builder) {
183
                return $builder->getManager();
184
            },
185
            $this->builders
186
        );
187
188
        $this->registerGlobalAnnotationLoader();
189
190
        return $managers;
191
    }
192
193
    /**
194
     * Get registered builder's manager.
195
     *
196
     * @param string $name
197
     *
198
     * @throws \RuntimeException
199
     *
200
     * @return \Doctrine\Common\Persistence\ObjectManager
201
     */
202
    public function getManager($name)
203
    {
204
        $builder = $this->getBuilder($name);
205
        if (!$builder instanceof Builder) {
206
            throw new \RuntimeException(sprintf('"%s" is not a registered manager', $name));
207
        }
208
209
        $manager = $builder->getManager();
210
211
        $this->registerGlobalAnnotationLoader();
212
213
        return $manager;
214
    }
215
216
    /**
217
     * Get console application.
218
     *
219
     * @return Application
220
     */
221
    public function getCLIApplication()
222
    {
223
        $application = new Application('Doctrine Manager Builder Command Line Interface');
224
        $application->setCatchExceptions(true);
225
226
        foreach ($this->builders as $builder) {
227
            foreach ($builder->getConsoleCommands() as $command) {
228
                $helperSet = $command->getHelperSet();
229
230
                $application->add($command)->setHelperSet($helperSet);
0 ignored issues
show
Bug introduced by
It seems like $helperSet defined by $command->getHelperSet() on line 228 can be null; however, Symfony\Component\Consol...Command::setHelperSet() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
231
            }
232
        }
233
234
        $this->registerGlobalAnnotationLoader();
235
236
        return $application;
237
    }
238
239
    /**
240
     * Register global annotation loader.
241
     * class_exists function.
242
     */
243
    protected function registerGlobalAnnotationLoader()
244
    {
245
        if ($this->globalLoaderRegister) {
246
            AnnotationRegistry::registerLoader('class_exists');
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\Common\Annotati...istry::registerLoader() has been deprecated with message: this method is deprecated and will be removed in doctrine/annotations 2.0 autoloading should be deferred to the globally registered autoloader by then. For now, use @example AnnotationRegistry::registerLoader('class_exists')

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
247
248
            $this->globalLoaderRegister = false;
249
        }
250
    }
251
}
252