Completed
Push — master ( 47c243...d4dc72 )
by Julián
09:16
created

ManagerBuilder::loadSettings()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 8
nop 1
1
<?php
2
/**
3
 * Slim3 Doctrine integration (https://github.com/juliangut/slim-doctrine)
4
 *
5
 * @link https://github.com/juliangut/slim-doctrine for the canonical source repository
6
 *
7
 * @license https://raw.githubusercontent.com/juliangut/slim-doctrine/master/LICENSE
8
 */
9
10
namespace Jgut\Slim\Doctrine;
11
12
use Interop\Container\ContainerInterface;
13
use Jgut\Doctrine\ManagerBuilder\CouchDBBuilder;
14
use Jgut\Doctrine\ManagerBuilder\ManagerBuilder as Builder;
15
use Jgut\Doctrine\ManagerBuilder\MongoDBBuilder;
16
use Jgut\Doctrine\ManagerBuilder\RelationalBuilder;
17
use Symfony\Component\Console\Application;
18
19
/**
20
 * Slim-Doctrine managers integration.
21
 */
22
class ManagerBuilder
23
{
24
    /**
25
     * @var Builder[]
26
     */
27
    protected $builders = [];
28
29
    /**
30
     * Load Doctrine managers from settings array.
31
     *
32
     * @param array $settings
33
     *
34
     * @throws \RuntimeException
35
     *
36
     * @return $this
37
     */
38
    public function loadSettings(array $settings)
39
    {
40
        if (array_key_exists('entity_manager', $settings)) {
41
            $this->registerEntityManagers((array) $settings['entity_manager']);
42
        }
43
44
        if (array_key_exists('mongodb_document_manager', $settings)) {
45
            $this->registerMongoDBDocumentManagers((array) $settings['mongodb_document_manager']);
46
        }
47
48
        if (array_key_exists('couchdb_document_manager', $settings)) {
49
            $this->registerCouchDBDocumentManagers((array) $settings['couchdb_document_manager']);
50
        }
51
52
        return $this;
53
    }
54
55
    /**
56
     * Register ORM entity managers.
57
     *
58
     * @param array $settings
59
     *
60
     * @throws \RuntimeException
61
     */
62
    protected function registerEntityManagers(array $settings)
63
    {
64
        if (array_key_exists('connection', $settings)) {
65
            $settings = [$settings];
66
        }
67
68
        foreach ($settings as $name => $config) {
69
            if (!is_string($name)) {
70
                $name = 'entityManager';
71
            }
72
73
            $this->addBuilder(new RelationalBuilder($config, $name));
74
        }
75
    }
76
77
    /**
78
     * Register MongoDB ODM document managers.
79
     *
80
     * @param array $settings
81
     *
82
     * @throws \RuntimeException
83
     */
84
    protected function registerMongoDBDocumentManagers(array $settings)
85
    {
86
        if (array_key_exists('connection', $settings)) {
87
            $settings = [$settings];
88
        }
89
90
        foreach ($settings as $name => $config) {
91
            if (!is_string($name)) {
92
                $name = 'mongoDocumentManager';
93
            }
94
95
            $this->addBuilder(new MongoDBBuilder($config, $name));
96
        }
97
    }
98
99
    /**
100
     * Register CouchDB ODM document managers.
101
     *
102
     * @param array $settings
103
     *
104
     * @throws \RuntimeException
105
     */
106
    protected function registerCouchDBDocumentManagers(array $settings)
107
    {
108
        if (array_key_exists('connection', $settings)) {
109
            $settings = [$settings];
110
        }
111
112
        foreach ($settings as $name => $config) {
113
            if (!is_string($name)) {
114
                $name = 'couchDocumentManager';
115
            }
116
117
            $this->addBuilder(new CouchDBBuilder($config, $name));
118
        }
119
    }
120
121
    /**
122
     * Add manager builder.
123
     *
124
     * @param Builder $builder
125
     *
126
     * @throws \RuntimeException
127
     *
128
     * @return $this
129
     */
130
    public function addBuilder(Builder $builder)
131
    {
132
        $builderName = (string) $builder->getName();
133
134
        if ($builderName === '') {
135
            throw new \RuntimeException('Only named manager builders allowed');
136
        }
137
138
        if (array_key_exists($builderName, $this->builders)) {
139
            throw new \RuntimeException(sprintf('"%s" manager builder is already registered', $builderName));
140
        }
141
142
        $this->builders[$builder->getName()] = $builder;
143
144
        return $this;
145
    }
146
147
    /**
148
     * Get registered builder's managers.
149
     *
150
     * @return \Doctrine\Common\Persistence\ObjectManager[]
151
     */
152
    public function getManagers()
153
    {
154
        return array_map(
155
            function (Builder $builder) {
156
                return $builder->getManager();
157
            },
158
            $this->builders
159
        );
160
    }
161
162
    /**
163
     * Get registered builder's manager.
164
     *
165
     * @param string $name
166
     *
167
     * @throws \RuntimeException
168
     *
169
     * @return \Doctrine\Common\Persistence\ObjectManager
170
     */
171
    public function getManager($name)
172
    {
173
        if (!array_key_exists($name, $this->builders)) {
174
            throw new \RuntimeException(sprintf('"%s" is not a registered manager', $name));
175
        }
176
177
        return $this->builders[$name]->getManager();
178
    }
179
180
    /**
181
     * Get console application.
182
     *
183
     * @return Application
184
     */
185
    public function getCLIApplication()
186
    {
187
        $application = new Application('Doctrine Manager Builder Command Line Interface');
188
        $application->setCatchExceptions(true);
189
190
        foreach ($this->builders as $builder) {
191
            $helperSet = $builder->getConsoleHelperSet();
192
193
            foreach ($builder->getConsoleCommands() as $command) {
194
                $application->add($command)->setHelperSet($helperSet);
195
            }
196
        }
197
198
        return $application;
199
    }
200
}
201