DefaultAnnotationReaderFactory   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
c 2
b 0
f 0
lcom 0
cbo 8
dl 0
loc 44
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B createService() 0 32 4
1
<?php
2
/**
3
 * @link    https://github.com/nnx-framework/jms-serializer-module
4
 * @author  Malofeykin Andrey  <[email protected]>
5
 */
6
namespace Nnx\JmsSerializerModule\MetadataReader;
7
8
use Doctrine\Common\Annotations\IndexedReader;
9
use Nnx\ModuleOptions\ModuleOptionsPluginManagerInterface;
10
use Zend\ServiceManager\AbstractPluginManager;
11
use Zend\ServiceManager\FactoryInterface;
12
use Zend\ServiceManager\ServiceLocatorInterface;
13
use Doctrine\Common\Annotations\AnnotationReader;
14
use Nnx\JmsSerializerModule\Options\ModuleOptions;
15
use Doctrine\Common\Cache\Cache;
16
use Doctrine\Common\Annotations\CachedReader;
17
18
/**
19
 * Class DefaultAnnotationReaderFactory
20
 *
21
 * @package Nnx\JmsSerializerModule\MetadataReader
22
 */
23
class DefaultAnnotationReaderFactory implements FactoryInterface
24
{
25
    /**
26
     * @inheritdoc
27
     *
28
     * @param ServiceLocatorInterface $serviceLocator
29
     *
30
     * @return mixed
31
     * @throws \Nnx\JmsSerializerModule\MetadataReader\Exception\RuntimeException
32
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
33
     */
34
    public function createService(ServiceLocatorInterface $serviceLocator)
35
    {
36
        $appServiceLocator = $serviceLocator instanceof AbstractPluginManager ? $serviceLocator->getServiceLocator() : $serviceLocator;
37
38
//        AnnotationRegistry::registerLoader(function ($class) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
39
//            return (bool) class_exists($class);
40
//        });
41
        $reader = new AnnotationReader();
42
        $indexedReader = new IndexedReader($reader);
43
44
        /** @var ModuleOptionsPluginManagerInterface $moduleOptionsPluginManager */
45
        $moduleOptionsPluginManager = $appServiceLocator->get(ModuleOptionsPluginManagerInterface::class);
46
47
        /** @var ModuleOptions  $moduleOptions */
48
        $moduleOptions = $moduleOptionsPluginManager->get(ModuleOptions::class);
49
50
        $annotationCacheName = $moduleOptions->getAnnotationCache();
51
52
        $annotationCache = $appServiceLocator->get($annotationCacheName);
53
54
        if (!$annotationCache instanceof Cache) {
55
            $errMsg = sprintf(
56
                'Annotation cache of type %s is invalid; must implement %s',
57
                (is_object($annotationCache) ? get_class($annotationCache) : gettype($annotationCache)),
58
                Cache::class
59
            );
60
            throw new Exception\RuntimeException($errMsg);
61
        }
62
63
64
        return new CachedReader($indexedReader, $annotationCache);
65
    }
66
}
67