Completed
Push — master ( 71fe6c...2dd80e )
by Koldo
02:32
created

MarshalDelegatorsConfig   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 19
c 1
b 0
f 1
dl 0
loc 38
ccs 0
cts 30
cp 0
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 20 3
A delegateFactories() 0 14 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Antidot\Container;
6
7
use Psr\Container\ContainerInterface;
8
use function is_callable;
9
10
/**
11
 *
12
 * Class MarshalDelegatorsConfig
13
 * @package Antidot\Container
14
 */
15
class MarshalDelegatorsConfig
16
{
17
    public function __invoke(ContainerInterface $container, ContainerConfig $dependencies): ContainerConfig
18
    {
19
        foreach ($dependencies->get('delegators') as $service => $delegatorNames) {
20
            $factory = $this->delegateFactories($container, $dependencies, $service);
21
            if (!is_callable($factory)) {
22
                continue;
23
            }
24
            $dependencies->set(ContainerDelegatorFactory::class, static function () use ($delegatorNames, $factory) {
25
                return new ContainerDelegatorFactory($delegatorNames, $factory);
0 ignored issues
show
Deprecated Code introduced by
The class Antidot\Container\ContainerDelegatorFactory has been deprecated: will remove in version 1.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

25
                return /** @scrutinizer ignore-deprecated */ new ContainerDelegatorFactory($delegatorNames, $factory);
Loading history...
Bug introduced by
It seems like $factory can also be of type null; however, parameter $factory of Antidot\Container\Contai...rFactory::__construct() does only seem to accept callable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

25
                return new ContainerDelegatorFactory($delegatorNames, /** @scrutinizer ignore-type */ $factory);
Loading history...
26
            });
27
            $dependencies->set(
28
                $service,
29
                static function(ContainerInterface $container) use ($service) {
30
                    $callable = $container->get(ContainerDelegatorFactory::class);
31
                    return $callable($container, $service);
32
                }
33
            );
34
        }
35
36
        return $dependencies;
37
    }
38
39
    private function delegateFactories(
40
        ContainerInterface $container,
41
        ContainerConfig $dependencies,
42
        string $service
43
    ): ?callable {
44
        if (false === $dependencies->has($service)) {
45
            return null;
46
        }
47
        // Marshal from factory
48
        $serviceFactory = $dependencies->get($service);
49
        return static function () use ($service, $serviceFactory, $container) {
50
            return is_callable($serviceFactory)
51
                ? $serviceFactory($container, $service)
52
                : (new $serviceFactory())($container, $service);
53
        };
54
    }
55
}
56