# cookiebot/3.10.1/addons/inc/Dependencies/DI/Definition/Resolver/FactoryResolver.php

Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA &amp; Google Consent Mode, version 3.10.1. 86 lines.

- Page: https://pluginprobe.com/plugins/cookiebot/3.10.1/code/addons/inc/Dependencies/DI/Definition/Resolver/FactoryResolver.php
- Raw: https://pluginprobe.com/plugins/cookiebot/3.10.1/raw/addons/inc/Dependencies/DI/Definition/Resolver/FactoryResolver.php
- Modified: 2021-03-23T11:49:48+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/cookiebot/3.10.1/code/addons/inc/Dependencies/DI/Definition/Resolver/FactoryResolver.php#L10-L20`.

```php
<?php
/**
 * PHP-DI
 *
 * @link      http://mnapoli.github.com/PHP-DI/
 * @copyright Matthieu Napoli (http://mnapoli.fr/)
 * @license   http://www.opensource.org/licenses/mit-license.php MIT (see the LICENSE file)
 */

namespace Cybot\Dependencies\DI\Definition\Resolver;

use Cybot\Dependencies\DI\Definition\Exception\DefinitionException;
use Cybot\Dependencies\DI\Definition\FactoryDefinition;
use Cybot\Dependencies\DI\Definition\Definition;
use Cybot\Dependencies\Interop\Container\ContainerInterface;

/**
 * Resolves a factory definition to a value.
 *
 * @since 4.0
 * @author Matthieu Napoli <matthieu@mnapoli.fr>
 */
class FactoryResolver implements DefinitionResolver
{
    /**
     * @var ContainerInterface
     */
    private $container;

    /**
     * The resolver needs a container. This container will be passed to the factory as a parameter
     * so that the factory can access other entries of the container.
     *
     * @param ContainerInterface $container
     */
    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    /**
     * Resolve a factory definition to a value.
     *
     * This will call the callable of the definition.
     *
     * @param FactoryDefinition $definition
     *
     * {@inheritdoc}
     */
    public function resolve(Definition $definition, array $parameters = [])
    {
        $this->assertIsFactoryDefinition($definition);

        $callable = $definition->getCallable();

        if (! is_callable($callable)) {
            throw new DefinitionException(sprintf(
                'The factory definition "%s" is not callable',
                $definition->getName()
            ));
        }

        return call_user_func($callable, $this->container);
    }

    /**
     * {@inheritdoc}
     */
    public function isResolvable(Definition $definition, array $parameters = [])
    {
        $this->assertIsFactoryDefinition($definition);

        return true;
    }

    private function assertIsFactoryDefinition(Definition $definition)
    {
        if (!$definition instanceof FactoryDefinition) {
            throw new \InvalidArgumentException(sprintf(
                'This definition resolver is only compatible with FactoryDefinition objects, %s given',
                get_class($definition)
            ));
        }
    }
}

```
