AnnotationDriverFactory::createService()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 18
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 18
loc 18
rs 9.4285
cc 3
eloc 10
nc 3
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\MetadataDriver;
7
8
use Zend\ServiceManager\FactoryInterface;
9
use Zend\ServiceManager\MutableCreationOptionsInterface;
10
use Zend\ServiceManager\MutableCreationOptionsTrait;
11
use Zend\ServiceManager\ServiceLocatorInterface;
12
use JMS\Serializer\Metadata\Driver\AnnotationDriver;
13
use Doctrine\Common\Annotations\Reader;
14
15
16
/**
17
 * Class AnnotationDriverFactory
18
 *
19
 * @package Nnx\JmsSerializerModule\MetadataDriver
20
 */
21 View Code Duplication
class AnnotationDriverFactory implements FactoryInterface, MutableCreationOptionsInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
22
{
23
    use MutableCreationOptionsTrait;
24
25
    /**
26
     * @param ServiceLocatorInterface $serviceLocator
27
     *
28
     * @return AnnotationDriver
29
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
30
     * @throws \Nnx\JmsSerializerModule\MetadataDriver\Exception\RuntimeException
31
     */
32
    public function createService(ServiceLocatorInterface $serviceLocator)
33
    {
34
        $creationOptions = $this->getCreationOptions();
35
36
        if (!array_key_exists('reader', $creationOptions)) {
37
            $errMsg = 'Annotation reader not specified';
38
            throw new Exception\RuntimeException($errMsg);
39
        }
40
41
        $reader = $serviceLocator->get($creationOptions['reader']);
42
43
        if (!$reader instanceof Reader) {
44
            $errMsg = sprintf('Annotation reader not implement %s', Reader::class);
45
            throw new Exception\RuntimeException($errMsg);
46
        }
47
48
        return new AnnotationDriver($reader);
49
    }
50
}
51