ServiceFactory   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 39
ccs 9
cts 9
cp 1
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A make() 0 9 3
A __construct() 0 4 1
1
<?php
2
3
/**
4
 * Bridge Spiral-Core
5
 *
6
 * @author Vlad Shashkov <[email protected]>
7
 * @copyright Copyright (c) 2021, The Myaza Software
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Bridge\Core;
13
14
use Psr\Container\ContainerExceptionInterface;
15
use Psr\Container\ContainerInterface;
16
use Spiral\Core\FactoryInterface;
17
18
final class ServiceFactory implements FactoryInterface
19
{
20
    /**
21
     * @var ContainerInterface
22
     */
23
    private $container;
24
25
    /**
26
     * @var iterable<ServiceIdResolverInterface>
27
     */
28
    private $serviceIdResolvers;
29
30
    /**
31
     * ServiceFactory constructor.
32
     *
33
     * @param ServiceIdResolverInterface[] $serviceIdResolvers
34
     */
35 2
    public function __construct(ContainerInterface $container, iterable $serviceIdResolvers)
36
    {
37 2
        $this->container          = $container;
38 2
        $this->serviceIdResolvers = $serviceIdResolvers;
39 2
    }
40
41
    /**
42
     * @param array<string,mixed> $parameters
43
     *
44
     * @throws ContainerExceptionInterface
45
     *
46
     * @return mixed|object|null
47
     */
48 2
    public function make(string $alias, array $parameters = [])
49
    {
50 2
        foreach ($this->serviceIdResolvers as $resolver) {
51 1
            if ($resolver->support($alias, $parameters)) {
52 1
                return $this->container->get($resolver->resolve($alias, $parameters));
53
            }
54
        }
55
56 1
        return $this->container->get($alias);
57
    }
58
}
59