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

DoctrineObjectConstructorFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 0
cbo 4
dl 0
loc 50
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C createService() 0 36 7
1
<?php
2
/**
3
 * @link    https://github.com/nnx-framework/jms-serializer-module
4
 * @author  Malofeykin Andrey  <[email protected]>
5
 */
6
namespace Nnx\JmsSerializerModule\ObjectConstructor;
7
8
use Zend\ServiceManager\FactoryInterface;
9
use Zend\ServiceManager\MutableCreationOptionsInterface;
10
use Zend\ServiceManager\MutableCreationOptionsTrait;
11
use Zend\ServiceManager\ServiceLocatorInterface;
12
use Doctrine\Common\Persistence\ManagerRegistry;
13
use JMS\Serializer\Construction\ObjectConstructorInterface;
14
use JMS\Serializer\Construction\DoctrineObjectConstructor;
15
16
/**
17
 * Class DoctrineObjectConstructorFactory
18
 *
19
 * @package Nnx\JmsSerializerModule\ObjectConstructor
20
 */
21
class DoctrineObjectConstructorFactory implements FactoryInterface, MutableCreationOptionsInterface
22
{
23
    use MutableCreationOptionsTrait;
24
25
    /**
26
     * @inheritdoc
27
     *
28
     * @param ServiceLocatorInterface $serviceLocator
29
     *
30
     * @return DoctrineObjectConstructor
31
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
32
     * @throws \Nnx\JmsSerializerModule\ObjectConstructor\Exception\RuntimeException
33
     */
34
    public function createService(ServiceLocatorInterface $serviceLocator)
35
    {
36
        $creationOptions = $this->getCreationOptions();
37
38
        if (!array_key_exists('managerRegistry', $creationOptions)) {
39
            $errMsg = 'Manager registry service name not specified';
40
            throw new Exception\RuntimeException($errMsg);
41
        }
42
        $managerRegistry = $serviceLocator->get($creationOptions['managerRegistry']);
43
44
        if (!$managerRegistry instanceof ManagerRegistry) {
45
            $errMsg = sprintf(
46
                'Manager registry of type %s is invalid; must implement %s',
47
                (is_object($managerRegistry) ? get_class($managerRegistry) : gettype($managerRegistry)),
48
                ManagerRegistry::class
49
            );
50
            throw new Exception\RuntimeException($errMsg);
51
        }
52
53
        if (!array_key_exists('fallbackConstructor', $creationOptions)) {
54
            $errMsg = 'Fallback constructor name not specified';
55
            throw new Exception\RuntimeException($errMsg);
56
        }
57
        $fallbackConstructor = $serviceLocator->get($creationOptions['fallbackConstructor']);
58
59
        if (!$fallbackConstructor instanceof ObjectConstructorInterface) {
60
            $errMsg = sprintf(
61
                'Fallback constructor of type %s is invalid; must implement %s',
62
                (is_object($fallbackConstructor) ? get_class($fallbackConstructor) : gettype($fallbackConstructor)),
63
                ObjectConstructorInterface::class
64
            );
65
            throw new Exception\RuntimeException($errMsg);
66
        }
67
68
        return new DoctrineObjectConstructor($managerRegistry, $fallbackConstructor);
69
    }
70
}
71