Manager   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 62
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A register() 0 6 1
A get() 0 8 2
A getIterator() 0 6 2
1
<?php
2
/**
3
 * This file is part of laravel.su package.
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace App\Services\DataProviders;
10
11
use Illuminate\Contracts\Container\Container;
12
13
/**
14
 * Class Manager.
15
 */
16
class Manager implements \IteratorAggregate
17
{
18
    /**
19
     * @var array
20
     */
21
    private $providers;
22
23
    /**
24
     * @var Container
25
     */
26
    private $container;
27
28
    /**
29
     * Manager constructor.
30
     * @param array     $providers
31
     * @param Container $container
32
     */
33
    public function __construct(array $providers, Container $container)
34
    {
35
        $this->container = $container;
36
37
        foreach ($providers as $alias => $class) {
38
            $this->register($alias, $class);
39
        }
40
    }
41
42
    /**
43
     * @param  string                       $alias
44
     * @param  string|DataProviderInterface $provider
45
     * @return $this|Manager
46
     */
47
    public function register(string $alias, string $provider): Manager
48
    {
49
        $this->providers[$alias] = $this->container->make($provider);
0 ignored issues
show
Bug introduced by
It seems like $provider defined by parameter $provider on line 47 can also be of type object<App\Services\Data...\DataProviderInterface>; however, Illuminate\Contracts\Container\Container::make() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
50
51
        return $this;
52
    }
53
54
    /**
55
     * @param  string                    $alias
56
     * @return DataProviderInterface
57
     * @throws \InvalidArgumentException
58
     */
59
    public function get(string $alias): DataProviderInterface
60
    {
61
        if (! array_key_exists($alias, $this->providers)) {
62
            throw new \InvalidArgumentException($alias . ' data provider does not exists.');
63
        }
64
65
        return $this->providers[$alias];
66
    }
67
68
    /**
69
     * @return \Generator|DataProviderInterface[]
70
     */
71
    public function getIterator(): \Generator
72
    {
73
        foreach ($this->providers as $alias => $instance) {
74
            yield $alias => $instance;
75
        }
76
    }
77
}
78