Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
CRAP
60.00% covered (warning)
60.00%
21 / 35
ServiceOptionsFactory
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
35.50
60.00% covered (warning)
60.00%
21 / 35
 __invoke
0.00% covered (danger)
0.00%
0 / 1
35.50
60.00% covered (warning)
60.00%
21 / 35
<?php
namespace AssetsBundle\Factory;
class ServiceOptionsFactory implements \Zend\ServiceManager\Factory\FactoryInterface
{
    /**
     * @see \Zend\ServiceManager\Factory\FactoryInterface::__invoke()
     * @param \Interop\Container\ContainerInterface $oServiceLocator
     * @param string $sRequestedName
     * @param array $aOptions
     * @throws \UnexpectedValueException
     * @return \AssetsBundle\Service\ServiceOptions
     */
    public function __invoke(\Interop\Container\ContainerInterface $oServiceLocator, $sRequestedName, array $aOptions = null)
    {
        $aConfiguration = $oServiceLocator->get('Config');
        if (!isset($aConfiguration['assets_bundle'])) {
            throw new \UnexpectedValueException('AssetsBundle configuration is undefined');
        }
        $aOptions = $aConfiguration['assets_bundle'];
        if ($aOptions instanceof \Traversable) {
            $aOptions = \Zend\Stdlib\ArrayUtils::iteratorToArray($aOptions);
        } elseif (!is_array($aOptions)) {
            throw new \InvalidArgumentException('"assets_bundle" configuration expects an array or Traversable object; received "' . (is_object($aOptions) ? get_class($aOptions) : gettype($aOptions)) . '"');
        }
        //Define base URL of the application
        if (!isset($aOptions['baseUrl'])) {
            if (($oRequest = $oServiceLocator->get('request')) instanceof \Zend\Http\PhpEnvironment\Request) {
                $aOptions['baseUrl'] = $oRequest->getBaseUrl();
            } else {
                $oRequest = new \Zend\Http\PhpEnvironment\Request();
                $aOptions['baseUrl'] = $oRequest->getBaseUrl();
            }
        }
        //Retrieve filters
        if (isset($aOptions['view_helper_plugins'])) {
            $aViewHelperPlugins = $aOptions['view_helper_plugins'];
            if ($aViewHelperPlugins instanceof \Traversable) {
                $aViewHelperPlugins = \Zend\Stdlib\ArrayUtils::iteratorToArray($aOptions);
            } elseif (!is_array($aViewHelperPlugins)) {
                throw new \InvalidArgumentException('Assets bundle "filters" option expects an array or Traversable object; received "' . (is_object($aViewHelperPlugins) ? get_class($aViewHelperPlugins) : gettype($aViewHelperPlugins)) . '"');
            }
            $oViewHelperPluginManager = $oServiceLocator->get('ViewHelperManager');
            foreach ($aViewHelperPlugins as $sAssetFileType => $oViewHelperPlugin) {
                if (!\AssetsBundle\AssetFile\AssetFile::assetFileTypeExists($sAssetFileType)) {
                    throw new \LogicException('Asset file type "' . $sAssetFileType . '" is not valid');
                }
                if (is_string($oViewHelperPlugin)) {
                    if ($oViewHelperPluginManager->has($oViewHelperPlugin)) {
                        $oViewHelperPlugin = $oViewHelperPluginManager->get($oViewHelperPlugin);
                    } elseif (class_exists($oViewHelperPlugin)) {
                        $oViewHelperPlugin = new $oViewHelperPlugin();
                    } else {
                        throw new \LogicException('View helper plugin "' . $oViewHelperPlugin . '" is not a registered service or an existing class');
                    }
                    if ($oViewHelperPlugin instanceof \Zend\View\Helper\HelperInterface) {
                        $aViewHelperPlugins[$sAssetFileType] = $oViewHelperPlugin;
                    } else {
                        throw new \LogicException('View helper plugin expects an instance of "\Zend\View\Helper\HelperInterface", "' . get_class($oViewHelperPlugin) . '" given');
                    }
                }
            }
            $aOptions['view_helper_plugins'] = $aViewHelperPlugins;
        }
        // Unset filters
        unset($aOptions['filters']);
        return new \AssetsBundle\Service\ServiceOptions($aOptions);
    }
}