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 Nnx\ModuleOptions\ModuleOptionsPluginManagerInterface; |
9
|
|
|
use Zend\Serializer\Adapter\AdapterInterface; |
10
|
|
|
use Zend\ServiceManager\AbstractFactoryInterface; |
11
|
|
|
use Zend\ServiceManager\AbstractPluginManager; |
12
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
13
|
|
|
use Nnx\JmsSerializerModule\Options\ModuleOptions; |
14
|
|
|
use Metadata\Driver\FileLocatorInterface; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class FileLocatorAbstractFactory |
19
|
|
|
* |
20
|
|
|
* @package Nnx\JmsSerializerModule\MetadataDriver |
21
|
|
|
*/ |
22
|
|
View Code Duplication |
class FileLocatorAbstractFactory implements AbstractFactoryInterface |
|
|
|
|
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 0 === strpos($requestedName, 'nnxJmsSerializer.fileLocators.'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @inheritdoc |
40
|
|
|
* |
41
|
|
|
* @param ServiceLocatorInterface $serviceLocator |
42
|
|
|
* @param $name |
43
|
|
|
* @param $requestedName |
44
|
|
|
* |
45
|
|
|
* @return AdapterInterface |
46
|
|
|
* @throws \Nnx\JmsSerializerModule\MetadataDriver\Exception\RuntimeException |
47
|
|
|
* @throws \Nnx\JmsSerializerModule\Options\Exception\InvalidArgumentException |
48
|
|
|
* @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
49
|
|
|
*/ |
50
|
|
|
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) |
51
|
|
|
{ |
52
|
|
|
$appServiceLocator = $serviceLocator instanceof AbstractPluginManager ? $serviceLocator->getServiceLocator() : $serviceLocator; |
53
|
|
|
|
54
|
|
|
$fileLocatorName = substr($requestedName, 30); |
55
|
|
|
|
56
|
|
|
/** @var ModuleOptionsPluginManagerInterface $moduleOptionsManager */ |
57
|
|
|
$moduleOptionsManager = $appServiceLocator->get(ModuleOptionsPluginManagerInterface::class); |
58
|
|
|
|
59
|
|
|
/** @var ModuleOptions $moduleOptions */ |
60
|
|
|
$moduleOptions = $moduleOptionsManager->get(ModuleOptions::class); |
61
|
|
|
|
62
|
|
|
$fileLocatorConfig = $moduleOptions->getFileLocator($fileLocatorName); |
63
|
|
|
|
64
|
|
|
$name = $fileLocatorConfig->getName(); |
65
|
|
|
$options = $fileLocatorConfig->getOptions(); |
66
|
|
|
|
67
|
|
|
$fileLocator = $serviceLocator->get( |
68
|
|
|
$name, |
69
|
|
|
$options |
|
|
|
|
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
if (!$fileLocator instanceof FileLocatorInterface) { |
73
|
|
|
$errMsg = sprintf( |
74
|
|
|
'File locator of type %s is invalid; must implement %s', |
75
|
|
|
(is_object($fileLocator) ? get_class($fileLocator) : gettype($fileLocator)), |
76
|
|
|
FileLocatorInterface::class |
77
|
|
|
); |
78
|
|
|
throw new Exception\RuntimeException($errMsg); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $fileLocator; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
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.