Complex classes like MongoDBBuilder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MongoDBBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | class MongoDBBuilder extends AbstractManagerBuilder |
||
35 | { |
||
36 | /** |
||
37 | * Logger callable. |
||
38 | * |
||
39 | * @var callable |
||
40 | */ |
||
41 | protected $loggerCallable; |
||
42 | |||
43 | /** |
||
44 | * {@inheritdoc} |
||
45 | */ |
||
46 | protected function getDefaultOptions() |
||
47 | { |
||
48 | return [ |
||
49 | 'connection' => [], // Array or \Doctrine\MongoDB\Connection |
||
50 | 'proxies_namespace' => 'DoctrineMongoDBODMProxy', |
||
51 | 'metadata_cache_namespace' => 'DoctrineMongoDBODMMetadataCache', |
||
52 | 'default_repository_class' => DocumentRepository::class, |
||
53 | 'hydrators_namespace' => 'DoctrineMongoDBODMHydrator', |
||
54 | 'hydrators_auto_generation' => AbstractProxyFactory::AUTOGENERATE_NEVER, |
||
55 | 'persistent_collections_namespace' => 'DoctrineMongoDBODMPersistentCollection', |
||
56 | 'persistent_collections_auto_generation' => AbstractProxyFactory::AUTOGENERATE_NEVER, |
||
57 | ]; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | */ |
||
63 | protected function wipe() |
||
64 | { |
||
65 | $this->manager = null; |
||
66 | $this->mappingDriver = null; |
||
67 | $this->metadataCacheDriver = null; |
||
68 | $this->eventManager = null; |
||
69 | $this->loggerCallable = null; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * {@inheritdoc} |
||
74 | * |
||
75 | * @throws \Doctrine\ODM\MongoDB\MongoDBException |
||
76 | * @throws \InvalidArgumentException |
||
77 | * @throws \RuntimeException |
||
78 | * @throws \UnexpectedValueException |
||
79 | * |
||
80 | * @return DocumentManager |
||
81 | */ |
||
82 | protected function buildManager() |
||
83 | { |
||
84 | $config = new Configuration; |
||
85 | |||
86 | $this->setUpGeneralConfigurations($config); |
||
87 | $this->setUpSpecificConfigurations($config); |
||
88 | |||
89 | $eventManager = $this->getEventManager(); |
||
90 | if ($this->getEventSubscribers() !== null) { |
||
91 | /* @var array $eventSubscribers */ |
||
92 | $eventSubscribers = $this->getEventSubscribers(); |
||
93 | |||
94 | foreach ($eventSubscribers as $eventSubscriber) { |
||
95 | $eventManager->addEventSubscriber($eventSubscriber); |
||
96 | } |
||
97 | } |
||
98 | |||
99 | return DocumentManager::create($this->getConnection($config), $config, $eventManager); |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * Set up general manager configurations. |
||
104 | * |
||
105 | * @param Configuration $config |
||
106 | */ |
||
107 | protected function setUpGeneralConfigurations(Configuration $config) |
||
108 | { |
||
109 | $this->setupAnnotationMetadata(); |
||
110 | $config->setMetadataDriverImpl($this->getMetadataMappingDriver()); |
||
111 | |||
112 | $config->setProxyDir($this->getProxiesPath()); |
||
113 | $config->setProxyNamespace($this->getProxiesNamespace()); |
||
114 | $config->setAutoGenerateProxyClasses($this->getProxiesAutoGeneration()); |
||
115 | |||
116 | if ($this->getRepositoryFactory() !== null) { |
||
117 | $config->setRepositoryFactory($this->getRepositoryFactory()); |
||
118 | } |
||
119 | |||
120 | if ($this->getDefaultRepositoryClass() !== null) { |
||
121 | $config->setDefaultRepositoryClassName($this->getDefaultRepositoryClass()); |
||
122 | } |
||
123 | |||
124 | $config->setMetadataCacheImpl($this->getMetadataCacheDriver()); |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Set up manager specific configurations. |
||
129 | * |
||
130 | * @param Configuration $config |
||
131 | */ |
||
132 | protected function setUpSpecificConfigurations(Configuration $config) |
||
133 | { |
||
134 | $config->setHydratorDir($this->getHydratorsPath()); |
||
135 | $config->setHydratorNamespace($this->getHydratorsNamespace()); |
||
136 | $config->setAutoGenerateHydratorClasses($this->getHydratorsAutoGeneration()); |
||
137 | |||
138 | $config->setPersistentCollectionDir($this->getPersistentCollectionPath()); |
||
139 | $config->setPersistentCollectionNamespace($this->getPersistentCollectionNamespace()); |
||
140 | $config->setAutoGeneratePersistentCollectionClasses($this->getAutoGeneratePersistentCollection()); |
||
141 | |||
142 | if ($this->getDefaultDatabase() !== null) { |
||
143 | $config->setDefaultDB($this->getDefaultDatabase()); |
||
144 | } |
||
145 | |||
146 | if ($this->getLoggerCallable() !== null) { |
||
147 | $config->setLoggerCallable($this->getLoggerCallable()); |
||
148 | } |
||
149 | |||
150 | foreach ($this->getCustomTypes() as $type => $class) { |
||
151 | if (Type::hasType($type)) { |
||
152 | Type::overrideType($type, $class); |
||
153 | } else { |
||
154 | Type::addType($type, $class); |
||
155 | } |
||
156 | } |
||
157 | |||
158 | foreach ($this->getCustomFilters() as $name => $filterClass) { |
||
159 | $config->addFilter($name, $filterClass); |
||
160 | } |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Create MongoDB Connection. |
||
165 | * |
||
166 | * @param Configuration $config |
||
167 | * |
||
168 | * @throws \InvalidArgumentException |
||
169 | * @throws \RuntimeException |
||
170 | * |
||
171 | * @return Connection |
||
172 | */ |
||
173 | protected function getConnection(Configuration $config) |
||
174 | { |
||
175 | $connection = $this->getOption('connection'); |
||
176 | |||
177 | switch (true) { |
||
178 | case is_array($connection): |
||
179 | $connection = new Connection( |
||
180 | array_key_exists('server', $connection) ? $connection['server'] : null, |
||
181 | array_key_exists('options', $connection) ? $connection['options'] : [], |
||
182 | $config, |
||
183 | $this->getEventManager() |
||
184 | ); |
||
185 | break; |
||
186 | |||
187 | case $connection instanceof Connection: |
||
188 | if ($connection->getEventManager() !== $this->getEventManager()) { |
||
189 | throw new \RuntimeException( |
||
190 | 'Cannot use different EventManager instances for DocumentManager and Connection.' |
||
191 | ); |
||
192 | } |
||
193 | break; |
||
194 | |||
195 | default: |
||
196 | throw new \InvalidArgumentException('Invalid argument: ' . $connection); |
||
197 | } |
||
198 | |||
199 | return $connection; |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * {@inheritdoc} |
||
204 | */ |
||
205 | protected function getAnnotationMappingDriver(array $paths) |
||
206 | { |
||
207 | return new AnnotationDriver(new AnnotationReader, $paths); |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * {@inheritdoc} |
||
212 | */ |
||
213 | protected function getXmlMappingDriver(array $paths, $extension = null) |
||
214 | { |
||
215 | $extension = $extension ?: XmlDriver::DEFAULT_FILE_EXTENSION; |
||
216 | |||
217 | return new XmlDriver($paths, $extension); |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * {@inheritdoc} |
||
222 | */ |
||
223 | protected function getYamlMappingDriver(array $paths, $extension = null) |
||
224 | { |
||
225 | $extension = $extension ?: YamlDriver::DEFAULT_FILE_EXTENSION; |
||
226 | |||
227 | return new YamlDriver($paths, $extension); |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * {@inheritdoc} |
||
232 | * |
||
233 | * @throws \InvalidArgumentException |
||
234 | * |
||
235 | * @return RepositoryFactory|null |
||
236 | */ |
||
237 | protected function getRepositoryFactory() |
||
238 | { |
||
239 | if (!array_key_exists('repository_factory', $this->options)) { |
||
240 | return; |
||
241 | } |
||
242 | |||
243 | $repositoryFactory = $this->options['repository_factory']; |
||
244 | |||
245 | if (!$repositoryFactory instanceof RepositoryFactory) { |
||
246 | throw new \InvalidArgumentException(sprintf( |
||
247 | 'Invalid factory class "%s". It must be a Doctrine\ODM\MongoDB\Repository\RepositoryFactory.', |
||
248 | get_class($repositoryFactory) |
||
249 | )); |
||
250 | } |
||
251 | |||
252 | return $repositoryFactory; |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * Retrieve hydrators path. |
||
257 | * |
||
258 | * @return string |
||
259 | */ |
||
260 | protected function getHydratorsPath() |
||
261 | { |
||
262 | return (string) $this->getOption('hydrators_path', sys_get_temp_dir()); |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * Retrieve hydrators namespace. |
||
267 | * |
||
268 | * @return null|string |
||
269 | */ |
||
270 | protected function getHydratorsNamespace() |
||
271 | { |
||
272 | $proxyNamespace = $this->getOption('hydrators_namespace'); |
||
273 | |||
274 | return is_string($proxyNamespace) ? $proxyNamespace : null; |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * Retrieve hydrators generation strategy. |
||
279 | * |
||
280 | * @return int |
||
281 | */ |
||
282 | protected function getHydratorsAutoGeneration() |
||
283 | { |
||
284 | return (int) $this->getOption('hydrators_auto_generation'); |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * Retrieve persistent collections path. |
||
289 | * |
||
290 | * @return string |
||
291 | */ |
||
292 | protected function getPersistentCollectionPath() |
||
293 | { |
||
294 | return (string) $this->getOption('persistent_collections_path', sys_get_temp_dir()); |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * Retrieve persistent collections namespace. |
||
299 | * |
||
300 | * @return null|string |
||
301 | */ |
||
302 | protected function getPersistentCollectionNamespace() |
||
303 | { |
||
304 | $collectionNamespace = $this->getOption('persistent_collections_namespace'); |
||
305 | |||
306 | return is_string($collectionNamespace) ? $collectionNamespace : null; |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * Retrieve persistent collections generation strategy. |
||
311 | * |
||
312 | * @return int |
||
313 | */ |
||
314 | protected function getAutoGeneratePersistentCollection() |
||
315 | { |
||
316 | return (int) $this->getOption('persistent_collections_auto_generation'); |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * Get default database. |
||
321 | * |
||
322 | * @return string|null |
||
323 | */ |
||
324 | protected function getDefaultDatabase() |
||
325 | { |
||
326 | return $this->hasOption('default_database') ? (string) $this->getOption('default_database') : null; |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * Retrieve logger callable. |
||
331 | * |
||
332 | * @return callable|null |
||
333 | */ |
||
334 | protected function getLoggerCallable() |
||
335 | { |
||
336 | if (!is_callable($this->loggerCallable)) { |
||
337 | $loggerCallable = $this->getOption('logger_callable'); |
||
338 | |||
339 | if (is_callable($loggerCallable)) { |
||
340 | $this->loggerCallable = $loggerCallable; |
||
341 | } |
||
342 | } |
||
343 | |||
344 | return $this->loggerCallable; |
||
345 | } |
||
346 | |||
347 | /** |
||
348 | * Retrieve custom types. |
||
349 | * |
||
350 | * @return array |
||
351 | */ |
||
352 | protected function getCustomTypes() |
||
353 | { |
||
354 | $types = (array) $this->getOption('custom_types'); |
||
355 | |||
356 | return array_filter( |
||
357 | $types, |
||
358 | function ($name) { |
||
359 | return is_string($name); |
||
360 | }, |
||
361 | ARRAY_FILTER_USE_KEY |
||
362 | ); |
||
363 | } |
||
364 | |||
365 | /** |
||
366 | * Get custom registered filters. |
||
367 | * |
||
368 | * @return array |
||
369 | */ |
||
370 | protected function getCustomFilters() |
||
382 | |||
383 | /** |
||
384 | * {@inheritdoc} |
||
385 | * |
||
386 | * @throws \Doctrine\ODM\MongoDB\MongoDBException |
||
387 | * @throws \InvalidArgumentException |
||
388 | * @throws \RuntimeException |
||
389 | * @throws \Symfony\Component\Console\Exception\InvalidArgumentException |
||
390 | * @throws \Symfony\Component\Console\Exception\LogicException |
||
391 | * @throws \UnexpectedValueException |
||
392 | * |
||
393 | * @return Command[] |
||
394 | */ |
||
395 | public function getConsoleCommands() |
||
396 | { |
||
397 | $commands = [ |
||
398 | // ODM |
||
399 | new \Doctrine\ODM\MongoDB\Tools\Console\Command\GenerateDocumentsCommand, |
||
400 | new \Doctrine\ODM\MongoDB\Tools\Console\Command\GenerateHydratorsCommand, |
||
401 | new \Doctrine\ODM\MongoDB\Tools\Console\Command\GenerateProxiesCommand, |
||
436 | |||
437 | /** |
||
438 | * Get console helper set. |
||
439 | * |
||
440 | * @return \Symfony\Component\Console\Helper\HelperSet |
||
441 | */ |
||
442 | protected function getConsoleHelperSet() |
||
451 | } |
||
452 |