DoctrineObjectConstructorFactory::createService()   C
last analyzed

Complexity

Conditions 7
Paths 5

Size

Total Lines 40
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 40
rs 6.7272
cc 7
eloc 24
nc 5
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\ObjectConstructor;
7
8
use Nnx\JmsSerializerModule\DoctrineObjectEngine\DoctrineObjectEngineInterface;
9
use Zend\ServiceManager\FactoryInterface;
10
use Zend\ServiceManager\MutableCreationOptionsInterface;
11
use Zend\ServiceManager\MutableCreationOptionsTrait;
12
use Zend\ServiceManager\ServiceLocatorInterface;
13
use Doctrine\Common\Persistence\ManagerRegistry;
14
use JMS\Serializer\Construction\ObjectConstructorInterface;
15
16
17
/**
18
 * Class DoctrineObjectConstructorFactory
19
 *
20
 * @package Nnx\JmsSerializerModule\ObjectConstructor
21
 */
22
class DoctrineObjectConstructorFactory implements FactoryInterface, MutableCreationOptionsInterface
23
{
24
    use MutableCreationOptionsTrait;
25
26
    /**
27
     * @inheritdoc
28
     *
29
     * @param ServiceLocatorInterface $serviceLocator
30
     *
31
     * @return DoctrineObjectConstructor
32
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
33
     * @throws \Nnx\JmsSerializerModule\ObjectConstructor\Exception\RuntimeException
34
     */
35
    public function createService(ServiceLocatorInterface $serviceLocator)
36
    {
37
        $creationOptions = $this->getCreationOptions();
38
39
        if (!array_key_exists('managerRegistry', $creationOptions)) {
40
            $errMsg = 'Manager registry service name not specified';
41
            throw new Exception\RuntimeException($errMsg);
42
        }
43
        $managerRegistry = $serviceLocator->get($creationOptions['managerRegistry']);
44
45
        if (!$managerRegistry instanceof ManagerRegistry) {
46
            $errMsg = sprintf(
47
                'Manager registry of type %s is invalid; must implement %s',
48
                (is_object($managerRegistry) ? get_class($managerRegistry) : gettype($managerRegistry)),
49
                ManagerRegistry::class
50
            );
51
            throw new Exception\RuntimeException($errMsg);
52
        }
53
54
        if (!array_key_exists('fallbackConstructor', $creationOptions)) {
55
            $errMsg = 'Fallback constructor name not specified';
56
            throw new Exception\RuntimeException($errMsg);
57
        }
58
        $fallbackConstructor = $serviceLocator->get($creationOptions['fallbackConstructor']);
59
60
        if (!$fallbackConstructor instanceof ObjectConstructorInterface) {
61
            $errMsg = sprintf(
62
                'Fallback constructor of type %s is invalid; must implement %s',
63
                (is_object($fallbackConstructor) ? get_class($fallbackConstructor) : gettype($fallbackConstructor)),
64
                ObjectConstructorInterface::class
65
            );
66
            throw new Exception\RuntimeException($errMsg);
67
        }
68
69
        /** @var DoctrineObjectEngineInterface $doctrineObjectEngine */
70
        $doctrineObjectEngine = $serviceLocator->get(DoctrineObjectEngineInterface::class);
71
72
73
        return new DoctrineObjectConstructor($managerRegistry, $fallbackConstructor, $doctrineObjectEngine);
74
    }
75
}
76