ObjectFactory   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 8
dl 0
loc 24
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 13 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\Container\Factory;
6
7
use Arp\Container\Factory\Exception\ServiceFactoryException;
8
use Psr\Container\ContainerInterface;
9
10
/**
11
 * Factory which will create a new instance of a class based on the requested $name
12
 *
13
 * @author  Alex Patterson <[email protected]>
14
 * @package Arp\Container\Factory
15
 */
16
final class ObjectFactory implements ServiceFactoryInterface
17
{
18
    /**
19
     * @param ContainerInterface $container
20
     * @param string             $name
21
     * @param array|null         $options
22
     *
23
     * @return object
24
     *
25
     * @throws ServiceFactoryException If the requested $name of the service is not a valid class name
26
     */
27
    public function __invoke(ContainerInterface $container, string $name, array $options = null): object
28
    {
29
        if (!class_exists($name, true)) {
30
            throw new ServiceFactoryException(
31
                sprintf(
32
                    'Unable to create a new object from requested service \'%s\': '
33
                    . 'The service name does not resolve to a valid class name',
34
                    $name
35
                )
36
            );
37
        }
38
39
        return (null === $options) ? new $name() : new $name(...$options);
40
    }
41
}
42