Completed
Push — master ( 111bcd...99ca87 )
by Andrey
26:30
created

DefaultAnnotationReaderFactory::createService()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 29
rs 8.5806
cc 4
eloc 15
nc 4
nop 1
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
        $reader = new AnnotationReader();
39
        $indexedReader = new IndexedReader($reader);
40
41
        /** @var ModuleOptionsPluginManagerInterface $moduleOptionsPluginManager */
42
        $moduleOptionsPluginManager = $appServiceLocator->get(ModuleOptionsPluginManagerInterface::class);
43
44
        /** @var ModuleOptions  $moduleOptions */
45
        $moduleOptions = $moduleOptionsPluginManager->get(ModuleOptions::class);
46
47
        $annotationCacheName = $moduleOptions->getAnnotationCache();
48
49
        $annotationCache = $appServiceLocator->get($annotationCacheName);
50
51
        if (!$annotationCache instanceof Cache) {
52
            $errMsg = sprintf(
53
                'Annotation cache of type %s is invalid; must implement %s',
54
                (is_object($annotationCache) ? get_class($annotationCache) : gettype($annotationCache)),
55
                Cache::class
56
            );
57
            throw new Exception\RuntimeException($errMsg);
58
        }
59
60
61
        return new CachedReader($indexedReader, $annotationCache);
62
    }
63
}
64