CouchDBBuilder::getConsoleHelperSet()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
30
 */
31
class CouchDBBuilder extends AbstractManagerBuilder
32
{
33
    /**
34
     * {@inheritdoc}
35
     */
36
    protected function getDefaultOptions()
37
    {
38
        return [
39
            'connection' => [], // Array or \Doctrine\CouchDB\CouchDBClient
40
            'proxies_namespace' => 'DoctrineCouchDBODMProxy',
41
            'metadata_cache_namespace' => 'DoctrineCouchDBODMMetadataCache',
42
            'default_repository_class' => DocumentRepository::class,
43
        ];
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    protected function wipe()
50
    {
51
        $this->manager = null;
52
        $this->mappingDriver = null;
53
        $this->metadataCacheDriver = null;
54
        $this->eventManager = null;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     *
60
     * @throws \InvalidArgumentException
61
     * @throws \RuntimeException
62
     * @throws \UnexpectedValueException
63
     *
64
     * @return DocumentManager
65
     */
66
    protected function buildManager()
67
    {
68
        $config = new Configuration;
69
70
        $this->setUpGeneralConfigurations($config);
71
        $this->setUpSpecificConfigurations($config);
72
73
        $documentManager = DocumentManager::create($this->getOption('connection'), $config, $this->getEventManager());
74
75
        if ($this->getRepositoryFactory() !== null) {
76
            $documentManager->setRepositoryFactory($this->getRepositoryFactory());
77
        }
78
79
        if ($this->getDefaultRepositoryClass() !== null) {
80
            $documentManager->setDefaultRepositoryClassName($this->getDefaultRepositoryClass());
81
        }
82
83
        return $documentManager;
84
    }
85
86
    /**
87
     * Set up general manager configurations.
88
     *
89
     * @param Configuration $config
90
     */
91
    protected function setUpGeneralConfigurations(Configuration $config)
92
    {
93
        $this->setupAnnotationMetadata();
94
        $config->setMetadataDriverImpl($this->getMetadataMappingDriver());
95
96
        $config->setProxyDir($this->getProxiesPath());
97
        $config->setProxyNamespace($this->getProxiesNamespace());
98
        $config->setAutoGenerateProxyClasses($this->getProxiesAutoGeneration());
99
100
        $config->setMetadataCacheImpl($this->getMetadataCacheDriver());
101
    }
102
103
    /**
104
     * Set up manager specific configurations.
105
     *
106
     * @param Configuration $config
107
     */
108
    protected function setUpSpecificConfigurations(Configuration $config)
109
    {
110
        if ($this->getLuceneHandlerName() !== null) {
111
            $config->setLuceneHandlerName($this->getLuceneHandlerName());
112
        }
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    protected function getAnnotationMappingDriver(array $paths)
119
    {
120
        return new AnnotationDriver(new AnnotationReader, $paths);
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    protected function getXmlMappingDriver(array $paths, $extension = null)
127
    {
128
        $extension = $extension ?: XmlDriver::DEFAULT_FILE_EXTENSION;
129
130
        return new XmlDriver($paths, $extension);
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136
    protected function getYamlMappingDriver(array $paths, $extension = null)
137
    {
138
        $extension = $extension ?: YamlDriver::DEFAULT_FILE_EXTENSION;
139
140
        return new YamlDriver($paths, $extension);
141
    }
142
143
    /**
144
     * {@inheritdoc}
145
     *
146
     * @throws \InvalidArgumentException
147
     *
148
     * @return RepositoryFactory|null
149
     */
150
    protected function getRepositoryFactory()
151
    {
152
        if (!array_key_exists('repository_factory', $this->options)) {
153
            return;
154
        }
155
156
        $repositoryFactory = $this->options['repository_factory'];
157
158
        if (!$repositoryFactory instanceof RepositoryFactory) {
159
            throw new \InvalidArgumentException(sprintf(
160
                'Invalid factory class "%s". It must be a Jgut\Doctrine\ManagerBuilder\CouchDB\RepositoryFactory.',
161
                get_class($repositoryFactory)
162
            ));
163
        }
164
165
        return $repositoryFactory;
166
    }
167
168
    /**
169
     * Get Lucene handler name.
170
     *
171
     * @return string|null
172
     */
173
    protected function getLuceneHandlerName()
174
    {
175
        return $this->hasOption('lucene_handler_name') ? (string) $this->getOption('lucene_handler_name') : null;
176
    }
177
178
    /**
179
     * {@inheritdoc}
180
     *
181
     * @throws \InvalidArgumentException
182
     * @throws \RuntimeException
183
     * @throws \Symfony\Component\Console\Exception\InvalidArgumentException
184
     * @throws \Symfony\Component\Console\Exception\LogicException
185
     * @throws \UnexpectedValueException
186
     *
187
     * @return Command[]
188
     */
189
    public function getConsoleCommands()
190
    {
191
        $commands = [
192
            // CouchDB
193
            new \Doctrine\CouchDB\Tools\Console\Command\ReplicationStartCommand,
194
            new \Doctrine\CouchDB\Tools\Console\Command\ReplicationCancelCommand,
195
            new \Doctrine\CouchDB\Tools\Console\Command\ViewCleanupCommand,
196
            new \Doctrine\CouchDB\Tools\Console\Command\CompactDatabaseCommand,
197
            new \Doctrine\CouchDB\Tools\Console\Command\CompactViewCommand,
198
            new \Doctrine\CouchDB\Tools\Console\Command\MigrationCommand,
199
200
            // ODM
201
            new \Doctrine\ODM\CouchDB\Tools\Console\Command\GenerateProxiesCommand,
202
            new \Doctrine\ODM\CouchDB\Tools\Console\Command\UpdateDesignDocCommand,
203
        ];
204
205
        $helperSet = $this->getConsoleHelperSet();
206
        $commandPrefix = (string) $this->getName();
207
208
        $commands = array_map(
209
            function (Command $command) use ($helperSet, $commandPrefix) {
210
                if ($commandPrefix !== '') {
211
                    $commandNames = array_map(
212
                        function ($commandName) use ($commandPrefix) {
213
                            $key = preg_match('/^couchdb:odm:/', $commandName) ? 'couchdb_odm' : 'couchdb';
214
215
                            return preg_replace(
216
                                '/^couchdb:(odm:)?/',
217
                                $key . ':' . $commandPrefix . ':',
218
                                $commandName
219
                            );
220
                        },
221
                        array_merge([$command->getName()], $command->getAliases())
222
                    );
223
224
                    $command->setName(array_shift($commandNames));
225
                    $command->setAliases($commandNames);
226
                }
227
228
                $command->setHelperSet($helperSet);
229
230
                return $command;
231
            },
232
            $commands
233
        );
234
235
        return $commands;
236
    }
237
238
    /**
239
     * Get console helper set.
240
     *
241
     * @return \Symfony\Component\Console\Helper\HelperSet
242
     */
243
    protected function getConsoleHelperSet()
244
    {
245
        /* @var DocumentManager $documentManager */
246
        $documentManager = $this->getManager();
247
248
        return new HelperSet([
249
            'dm' => new CouchDBHelper($documentManager->getCouchDBClient(), $documentManager),
250
        ]);
251
    }
252
}
253