Test Failed
Push — feature-laravel-5.4 ( 475d96...446986 )
by Kirill
07:30
created

Manager::__construct()   A

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 2
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
 * @package App\Services\DataProviders
16
 */
17
class Manager implements \IteratorAggregate
18
{
19
    /**
20
     * @var array
21
     */
22
    private $providers;
23
24
    /**
25
     * @var Container
26
     */
27
    private $container;
28
29
    /**
30
     * Manager constructor.
31
     * @param array     $providers
32
     * @param Container $container
33
     */
34
    public function __construct(array $providers, Container $container)
35
    {
36
        $this->container = $container;
37
38
        foreach ($providers as $alias => $class) {
39
            $this->register($alias, $class);
40
        }
41
    }
42
43
    /**
44
     * @param string                       $alias
45
     * @param string|DataProviderInterface $provider
46
     * @return $this|Manager
47
     */
48
    public function register(string $alias, string $provider): Manager
49
    {
50
        $this->providers[$alias] = $this->container->make($provider);
0 ignored issues
show
Bug introduced by
It seems like $provider defined by parameter $provider on line 48 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...
51
52
        return $this;
53
    }
54
55
    /**
56
     * @param string $alias
57
     * @return DataProviderInterface
58
     * @throws \InvalidArgumentException
59
     */
60
    public function get(string $alias): DataProviderInterface
61
    {
62
        if (! array_key_exists($alias, $this->providers)) {
63
            throw new \InvalidArgumentException($alias . ' data provider does not exists.');
64
        }
65
66
        return $this->providers[$alias];
67
    }
68
69
    /**
70
     * @return \Generator|DataProviderInterface[]
71
     */
72
    public function getIterator(): \Generator
73
    {
74
        foreach ($this->providers as $alias => $instance) {
75
            yield $alias => $instance;
76
        }
77
    }
78
}