Manager::get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
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