Completed
Push — master ( b5054b...5e1fc6 )
by Julián
02:29
created

CouchDBBuilder   B

Complexity

Total Complexity 21

Size/Duplication

Total Lines 213
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 18

Importance

Changes 0
Metric Value
wmc 21
lcom 2
cbo 18
dl 0
loc 213
rs 7.3333
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultOptions() 0 9 1
A wipe() 0 7 1
A buildManager() 0 19 3
A setUpGeneralConfigurations() 0 11 1
A setUpSpecificConfigurations() 0 6 2
A getAnnotationMappingDriver() 0 4 1
A getXmlMappingDriver() 0 6 2
A getYamlMappingDriver() 0 6 2
A getRepositoryFactory() 0 17 3
A getLuceneHandlerName() 0 4 2
B getConsoleCommands() 0 38 2
A getConsoleHelperSet() 0 8 1
1
<?php
2
3
/*
4
 * doctrine-manager-builder (https://github.com/juliangut/doctrine-manager-builder).
5
 * Doctrine2 managers builder.
6
 *
7
 * @license BSD-3-Clause
8
 * @link https://github.com/juliangut/doctrine-manager-builder
9
 * @author Julián Gutiérrez <[email protected]>
10
 */
11
12
namespace Jgut\Doctrine\ManagerBuilder;
13
14
use Doctrine\Common\Annotations\AnnotationReader;
15
use Doctrine\CouchDB\Tools\Console\Helper\CouchDBHelper;
16
use Doctrine\ODM\CouchDB\Configuration;
17
use Doctrine\ODM\CouchDB\DocumentRepository;
18
use Doctrine\ODM\CouchDB\Mapping\Driver\AnnotationDriver;
19
use Doctrine\ODM\CouchDB\Mapping\Driver\XmlDriver;
20
use Doctrine\ODM\CouchDB\Mapping\Driver\YamlDriver;
21
use Jgut\Doctrine\ManagerBuilder\CouchDB\DocumentManager;
22
use Jgut\Doctrine\ManagerBuilder\CouchDB\Repository\RepositoryFactory;
23
use Symfony\Component\Console\Command\Command;
24
use Symfony\Component\Console\Helper\HelperSet;
25
26
/**
27
 * Doctrine CouchDB Document Manager builder.
28
 */
29
class CouchDBBuilder extends AbstractManagerBuilder
30
{
31
    /**
32
     * {@inheritdoc}
33
     */
34
    protected function getDefaultOptions()
35
    {
36
        return [
37
            'connection' => [], // Array or \Doctrine\CouchDB\CouchDBClient
38
            'proxies_namespace' => 'DoctrineCouchDBODMProxy',
39
            'metadata_cache_namespace' => 'DoctrineCouchDBODMMetadataCache',
40
            'default_repository_class' => DocumentRepository::class,
41
        ];
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    protected function wipe()
48
    {
49
        $this->manager = null;
50
        $this->mappingDriver = null;
51
        $this->metadataCacheDriver = null;
52
        $this->eventManager = null;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     *
58
     * @throws \InvalidArgumentException
59
     * @throws \RuntimeException
60
     * @throws \UnexpectedValueException
61
     *
62
     * @return DocumentManager
63
     */
64
    protected function buildManager()
65
    {
66
        $config = new Configuration;
67
68
        $this->setUpGeneralConfigurations($config);
69
        $this->setUpSpecificConfigurations($config);
70
71
        $documentManager = DocumentManager::create($this->getOption('connection'), $config, $this->getEventManager());
72
73
        if ($this->getRepositoryFactory() !== null) {
74
            $documentManager->setRepositoryFactory($this->getRepositoryFactory());
75
        }
76
77
        if ($this->getDefaultRepositoryClass() !== null) {
78
            $documentManager->setDefaultRepositoryClassName($this->getDefaultRepositoryClass());
79
        }
80
81
        return $documentManager;
82
    }
83
84
    /**
85
     * Set up general manager configurations.
86
     *
87
     * @param Configuration $config
88
     */
89
    protected function setUpGeneralConfigurations(Configuration $config)
90
    {
91
        $this->setupAnnotationMetadata();
92
        $config->setMetadataDriverImpl($this->getMetadataMappingDriver());
93
94
        $config->setProxyDir($this->getProxiesPath());
95
        $config->setProxyNamespace($this->getProxiesNamespace());
96
        $config->setAutoGenerateProxyClasses($this->getProxiesAutoGeneration());
97
98
        $config->setMetadataCacheImpl($this->getMetadataCacheDriver());
99
    }
100
101
    /**
102
     * Set up manager specific configurations.
103
     *
104
     * @param Configuration $config
105
     */
106
    protected function setUpSpecificConfigurations(Configuration $config)
107
    {
108
        if ($this->getLuceneHandlerName() !== null) {
109
            $config->setLuceneHandlerName($this->getLuceneHandlerName());
110
        }
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    protected function getAnnotationMappingDriver(array $paths)
117
    {
118
        return new AnnotationDriver(new AnnotationReader, $paths);
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124
    protected function getXmlMappingDriver(array $paths, $extension = null)
125
    {
126
        $extension = $extension ?: XmlDriver::DEFAULT_FILE_EXTENSION;
127
128
        return new XmlDriver($paths, $extension);
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134
    protected function getYamlMappingDriver(array $paths, $extension = null)
135
    {
136
        $extension = $extension ?: YamlDriver::DEFAULT_FILE_EXTENSION;
137
138
        return new YamlDriver($paths, $extension);
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     *
144
     * @throws \InvalidArgumentException
145
     *
146
     * @return RepositoryFactory|null
147
     */
148
    protected function getRepositoryFactory()
149
    {
150
        if (!array_key_exists('repository_factory', $this->options)) {
151
            return;
152
        }
153
154
        $repositoryFactory = $this->options['repository_factory'];
155
156
        if (!$repositoryFactory instanceof RepositoryFactory) {
157
            throw new \InvalidArgumentException(sprintf(
158
                'Invalid factory class "%s". It must be a Jgut\Doctrine\ManagerBuilder\CouchDB\RepositoryFactory.',
159
                get_class($repositoryFactory)
160
            ));
161
        }
162
163
        return $repositoryFactory;
164
    }
165
166
    /**
167
     * Get Lucene handler name.
168
     *
169
     * @return string|null
170
     */
171
    protected function getLuceneHandlerName()
172
    {
173
        return $this->hasOption('lucene_handler_name') ? (string) $this->getOption('lucene_handler_name') : null;
174
    }
175
176
    /**
177
     * {@inheritdoc}
178
     *
179
     * @throws \InvalidArgumentException
180
     * @throws \RuntimeException
181
     * @throws \Symfony\Component\Console\Exception\InvalidArgumentException
182
     * @throws \Symfony\Component\Console\Exception\LogicException
183
     * @throws \UnexpectedValueException
184
     *
185
     * @return Command[]
186
     */
187
    public function getConsoleCommands()
188
    {
189
        $commands = [
190
            // CouchDB
191
            new \Doctrine\CouchDB\Tools\Console\Command\ReplicationStartCommand,
192
            new \Doctrine\CouchDB\Tools\Console\Command\ReplicationCancelCommand,
193
            new \Doctrine\CouchDB\Tools\Console\Command\ViewCleanupCommand,
194
            new \Doctrine\CouchDB\Tools\Console\Command\CompactDatabaseCommand,
195
            new \Doctrine\CouchDB\Tools\Console\Command\CompactViewCommand,
196
            new \Doctrine\CouchDB\Tools\Console\Command\MigrationCommand,
197
198
            // ODM
199
            new \Doctrine\ODM\CouchDB\Tools\Console\Command\GenerateProxiesCommand,
200
            new \Doctrine\ODM\CouchDB\Tools\Console\Command\UpdateDesignDocCommand,
201
        ];
202
        $commandPrefix = (string) $this->getName();
203
204
        if ($commandPrefix !== '') {
205
            $commands = array_map(
206
                function (Command $command) use ($commandPrefix) {
207
                    $commandNames = array_map(
208
                        function ($commandName) use ($commandPrefix) {
209
                            return preg_replace('/^couchdb:/', $commandPrefix . ':', $commandName);
210
                        },
211
                        array_merge([$command->getName()], $command->getAliases())
212
                    );
213
214
                    $command->setName(array_shift($commandNames));
215
                    $command->setAliases($commandNames);
216
217
                    return $command;
218
                },
219
                $commands
220
            );
221
        }
222
223
        return $commands;
224
    }
225
226
    /**
227
     * {@inheritdoc}
228
     *
229
     * @throws \InvalidArgumentException
230
     * @throws \RuntimeException
231
     * @throws \UnexpectedValueException
232
     */
233
    public function getConsoleHelperSet()
234
    {
235
        $documentManager = $this->getManager();
236
237
        return new HelperSet([
238
            'dm' => new CouchDBHelper($documentManager->getCouchDBClient(), $documentManager),
239
        ]);
240
    }
241
}
242