LeagueAdapter   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 18.18%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
eloc 33
c 1
b 0
f 0
dl 0
loc 83
ccs 6
cts 33
cp 0.1818
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A bind() 0 6 2
A __construct() 0 3 1
A addServiceProviders() 0 4 2
A createServices() 0 16 3
A alias() 0 3 1
A createService() 0 12 2
A share() 0 3 1
A addServiceProvider() 0 8 2
1
<?php
2
3
namespace Panamax\Adapters\League;
4
5
use League\Container\DefinitionContainerInterface;
6
use League\Container\ServiceProvider\ServiceProviderInterface;
7
use Panamax\Adapters\AbstractContainerAdapter;
8
use Panamax\Contracts\ContainerAdapterInterface;
9
use Panamax\Contracts\ProviderContainerInterface;
10
use Panamax\Contracts\ServiceCreatorInterface;
11
use Panamax\Contracts\ServiceFactoryInterface;
12
use Panamax\Exceptions\InvalidServiceProviderException;
13
use Panamax\Traits\ServiceCreatorTrait;
14
use Psr\Container\ContainerInterface;
15
use TypeError;
16
17
class LeagueAdapter extends AbstractContainerAdapter implements ContainerAdapterInterface, ProviderContainerInterface, ServiceCreatorInterface
18
{
19
    use ServiceCreatorTrait;
20
21
    /**
22
     * @var DefinitionContainerInterface
23
     */
24
    protected ContainerInterface $container;
25
26 3
    public function __construct(DefinitionContainerInterface $container)
27
    {
28 3
        $this->container = $container;
29
    }
30
31 3
    public function bind(string $id, $concrete = null, ?bool $shared = null)
32
    {
33 3
        $definition = $this->container->add($id, $concrete);
0 ignored issues
show
Bug introduced by
The method add() does not exist on Psr\Container\ContainerInterface. It seems like you code against a sub-type of Psr\Container\ContainerInterface such as League\Container\DefinitionContainerInterface or Symfony\Component\Depend...rameterBag\ContainerBag. ( Ignorable by Annotation )

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

33
        /** @scrutinizer ignore-call */ 
34
        $definition = $this->container->add($id, $concrete);
Loading history...
34
35 3
        if (isset($shared)) {
36 3
            $definition->setShared($shared);
37
        }
38
    }
39
40
    public function share(string $id, $concrete = null)
41
    {
42
        $this->container->addShared($id, $concrete);
0 ignored issues
show
Bug introduced by
The method addShared() does not exist on Psr\Container\ContainerInterface. It seems like you code against a sub-type of Psr\Container\ContainerInterface such as League\Container\DefinitionContainerInterface. ( Ignorable by Annotation )

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

42
        $this->container->/** @scrutinizer ignore-call */ 
43
                          addShared($id, $concrete);
Loading history...
43
    }
44
45
    public function alias(string $id, string $alias)
46
    {
47
        $this->container->add($alias, $id);
48
    }
49
50
    public function addServiceProvider(string $provider, ?array $args = null)
51
    {
52
        try {
53
            $this->container->addServiceProvider(new $provider());
0 ignored issues
show
Bug introduced by
The method addServiceProvider() does not exist on Psr\Container\ContainerInterface. It seems like you code against a sub-type of Psr\Container\ContainerInterface such as League\Container\DefinitionContainerInterface or Panamax\Contracts\ProviderContainerInterface or Panamax\Adapters\League\LeagueAdapter or Panamax\Adapters\League\LeagueAdapter. ( Ignorable by Annotation )

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

53
            $this->container->/** @scrutinizer ignore-call */ 
54
                              addServiceProvider(new $provider());
Loading history...
54
        } catch (TypeError $e) {
55
            throw new InvalidServiceProviderException(
56
                __METHOD__,
57
                ServiceProviderInterface::class
58
            );
59
        }
60
    }
61
62
    public function addServiceProviders(iterable $providers)
63
    {
64
        foreach ($providers as $provider) {
65
            $this->addServiceProvider($provider);
66
        }
67
    }
68
69
    public function createServices(iterable $services)
70
    {
71
        foreach ($services as $service) {
72
            $id = $service['id'];
73
            $factory = $service['factory'];
74
            $args = $service['args'] ?? [];
75
76
            unset($service['id'], $service['factory'], $service['args']);
77
78
            if ($this->isValidFactory($factory)) {
79
                $factory = $this->getResolvedFactory($factory);
80
            } else {
81
                throw $this->invalidFactoryException($id, $factory);
82
            }
83
84
            $this->createService($id, $factory, $args, $service);
85
        }
86
    }
87
88
    public function createService(string $id, ServiceFactoryInterface $factory, array $args = [], $options = [])
89
    {
90
        $definition = $this->container->add(
91
            $id,
92
            $factory->pledge($this->container, $args)
93
        );
94
95
        if ($shared = $options['shared'] ?? null) {
96
            $definition->setShared($shared);
97
        }
98
99
        array_map([$definition, 'addTag'], $options['tags'] ?? []);
100
    }
101
}
102