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

FileDriverAbstractFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A canCreateServiceWithName() 0 4 2
A createServiceWithName() 0 15 2
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\AbstractFactoryInterface;
9
use Zend\ServiceManager\MutableCreationOptionsInterface;
10
use Zend\ServiceManager\MutableCreationOptionsTrait;
11
use Zend\ServiceManager\ServiceLocatorInterface;
12
use ReflectionClass;
13
use Metadata\Driver\AbstractFileDriver;
14
15
/**
16
 * Class FileDriverAbstractFactory
17
 *
18
 * @package Nnx\JmsSerializerModule\MetadataDriver
19
 */
20
class FileDriverAbstractFactory implements AbstractFactoryInterface, MutableCreationOptionsInterface
21
{
22
    use MutableCreationOptionsTrait;
23
24
    /**
25
     * @inheritdoc
26
     *
27
     * @param ServiceLocatorInterface $serviceLocator
28
     * @param                         $name
29
     * @param                         $requestedName
30
     *
31
     * @return bool|void
32
     */
33
    public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
34
    {
35
        return class_exists($requestedName) && is_subclass_of($requestedName, AbstractFileDriver::class);
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Metadata\Driver\AbstractFileDriver::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
36
    }
37
38
    /**
39
     * @inheritdoc
40
     *
41
     * @param ServiceLocatorInterface $serviceLocator
42
     * @param                         $name
43
     * @param                         $requestedName
44
     *
45
     * @return AbstractFileDriver
46
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
47
     * @throws \Nnx\JmsSerializerModule\MetadataDriver\Exception\RuntimeException
48
     */
49
    public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
50
    {
51
        $creationOptions = $this->getCreationOptions();
52
53
        if (!array_key_exists('fileLocator', $creationOptions)) {
54
            $errMsg = 'File locator name not specified';
55
            throw new Exception\RuntimeException($errMsg);
56
        }
57
58
        $fileLocatorName = $creationOptions['fileLocator'];
59
        $fileLocator = $serviceLocator->get($fileLocatorName);
60
61
        $r = new ReflectionClass($requestedName);
62
        return $r->newInstance($fileLocator);
63
    }
64
}
65