Completed
Push — master ( 99ca87...95904e )
by Andrey
23:44
created

DataFactory::createService()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
rs 9.4285
cc 3
eloc 11
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\ObjectConstructor\DoctrineObjectConstructor;
7
8
use Nnx\JmsSerializerModule\DataContainer\DataContainerInterface;
9
use Nnx\JmsSerializerModule\DataContainer\EntityInterface;
10
use Zend\ServiceManager\FactoryInterface;
11
use Zend\ServiceManager\MutableCreationOptionsInterface;
12
use Zend\ServiceManager\MutableCreationOptionsTrait;
13
use Zend\ServiceManager\ServiceLocatorInterface;
14
15
/**
16
 * Class DataFactory
17
 *
18
 * @package Nnx\JmsSerializerModule\ObjectConstructor\DoctrineObjectConstructor
19
 */
20
class DataFactory implements FactoryInterface, MutableCreationOptionsInterface
21
{
22
    use MutableCreationOptionsTrait;
23
24
    /**
25
     * @param ServiceLocatorInterface $serviceLocator
26
     *
27
     * @return Data
28
     * @throws \Nnx\JmsSerializerModule\ObjectConstructor\DoctrineObjectConstructor\Exception\RuntimeException
29
     */
30
    public function createService(ServiceLocatorInterface $serviceLocator)
31
    {
32
        $creationOptions = $this->getCreationOptions();
33
        if (!array_key_exists('dataContainer', $creationOptions)) {
34
            $errMsg = 'Data container not found in creation options';
35
            throw new Exception\RuntimeException($errMsg);
36
        }
37
        /** @var  DataContainerInterface $dataContainer */
38
        $dataContainer = $creationOptions['dataContainer'];
39
40
        if (!array_key_exists('entity', $creationOptions)) {
41
            $errMsg = 'Entity not found in creation options';
42
            throw new Exception\RuntimeException($errMsg);
43
        }
44
        /** @var  EntityInterface $entity */
45
        $entity = $creationOptions['entity'];
46
47
        return new Data($dataContainer, $entity);
48
    }
49
}
50