Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 7d30314

Browse files
committed
Refactor: extract method
Extracted parameter resolution to a new method, and modified factory to use array_map with that method to create the list of parameters for instantiating the controller.
1 parent bc75c3d commit 7d30314

File tree

1 file changed

+46
-34
lines changed

1 file changed

+46
-34
lines changed

src/Controller/LazyControllerAbstractFactory.php

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Interop\Container\ContainerInterface;
1111
use ReflectionClass;
12+
use ReflectionParameter;
1213
use Zend\Console\Adapter\AdapterInterface as ConsoleAdapterInterface;
1314
use Zend\Filter\FilterPluginManager;
1415
use Zend\Hydrator\HydratorPluginManager;
@@ -113,40 +114,9 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o
113114
return new $requestedName();
114115
}
115116

116-
$parameters = [];
117-
foreach ($reflectionParameters as $parameter) {
118-
if ($parameter->isArray()
119-
&& $parameter->getName() === 'config'
120-
&& $container->has('config')
121-
) {
122-
$parameters[] = $container->get('config');
123-
continue;
124-
}
125-
126-
if ($parameter->isArray()) {
127-
$parameters[] = [];
128-
continue;
129-
}
130-
131-
if (! $parameter->getClass()) {
132-
$parameters[] = null;
133-
continue;
134-
}
135-
136-
$type = $parameter->getClass()->getName();
137-
$type = isset($this->aliases[$type]) ? $this->aliases[$type] : $type;
138-
139-
if (! $container->has($type)) {
140-
throw new ServiceNotFoundException(sprintf(
141-
'Unable to create controller "%s"; unable to resolve parameter "%s" using type hint "%s"',
142-
$requestedName,
143-
$parameter->getName(),
144-
$type
145-
));
146-
}
147-
148-
$parameters[] = $container->get($type);
149-
}
117+
$parameters = array_map(function (ReflectionParameter $parameter) use ($container, $requestedName) {
118+
return $this->resolveParameter($parameter, $container, $requestedName);
119+
}, $reflectionParameters);
150120

151121
return new $requestedName(...$parameters);
152122
}
@@ -162,4 +132,46 @@ public function canCreate(ContainerInterface $container, $requestedName)
162132

163133
return in_array(DispatchableInterface::class, class_implements($requestedName), true);
164134
}
135+
136+
/**
137+
* Resolve a parameter to a value.
138+
*
139+
* @param ReflectionClass $parameter
140+
* @param ContainerInterface $container
141+
* @param string $requestedName
142+
* @return mixed
143+
* @throws ServiceNotFoundException If type-hinted parameter cannot be
144+
* resolved to a service in the container.
145+
*/
146+
private function resolveParameter(ReflectionParameter $parameter, ContainerInterface $container, $requestedName)
147+
{
148+
if ($parameter->isArray()
149+
&& $parameter->getName() === 'config'
150+
&& $container->has('config')
151+
) {
152+
return $container->get('config');
153+
}
154+
155+
if ($parameter->isArray()) {
156+
return [];
157+
}
158+
159+
if (! $parameter->getClass()) {
160+
return;
161+
}
162+
163+
$type = $parameter->getClass()->getName();
164+
$type = isset($this->aliases[$type]) ? $this->aliases[$type] : $type;
165+
166+
if (! $container->has($type)) {
167+
throw new ServiceNotFoundException(sprintf(
168+
'Unable to create controller "%s"; unable to resolve parameter "%s" using type hint "%s"',
169+
$requestedName,
170+
$parameter->getName(),
171+
$type
172+
));
173+
}
174+
175+
return $container->get($type);
176+
}
165177
}

0 commit comments

Comments
 (0)