Completed
Push — master ( 2dd80e...b85147 )
by Koldo
02:33
created

MarshalDelegatorsConfig::delegateFactories()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 7
c 1
b 0
f 1
dl 0
loc 13
rs 10
ccs 6
cts 7
cp 0.8571
cc 3
nc 2
nop 2
crap 3.0261
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
class MarshalDelegatorsConfig
11
{
12 9
    public function __invoke(ContainerConfig $dependencies): ContainerConfig
13
    {
14 9
        foreach ($dependencies->get('delegators') as $service => $delegatorNames) {
15 2
            $factory = $this->delegateFactories($dependencies, $service);
16 2
            if (!is_callable($factory)) {
17
                continue;
18
            }
19
            $dependencies->set(ContainerDelegatorFactory::class, static function () use ($delegatorNames, $factory) {
20 1
                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

20
                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

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