protected function createServiceFactory(
        $requestedName, ReflectionClass $reflection, ContainerInterface $container
    ) {
        /** @var array $mappings */
        $mappings = $this->getServiceMapping($container, $requestedName);

        if ($reflection->implementsInterface(Factory\FactoryInterface::class)) {
            $factory = $reflection->newInstance($mappings['namespace'], $mappings['service'], $this->namespace);
        } else {
            $factory = $reflection->newInstance();
        }

        $this->lookupCache[$requestedName]['factoryInstance'] = $factory;

        if ($factory instanceof Factory\FactoryInterface) {
            /** @var Factory\FactoryInterface $factory */
            $factory->setContainer($container);
        }

        return $factory($container, $requestedName);
    }