Passed
Push — master ( 5ff919...5bf459 )
by Gabor
05:53
created

MultiConnectorContainer::getConnectorByName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 7.1
6
 *
7
 * @copyright 2012 - 2017 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link      http://www.gixx-web.com
11
 */
12
declare(strict_types = 1);
13
14
namespace WebHemi\Data\Connector;
15
16
use InvalidArgumentException;
17
use WebHemi\Data\ConnectorInterface;
18
use WebHemi\Data\MultiConnectorContainerInterface;
19
20
/**
21
 * Class MultiConnectorContainer
22
 */
23
class MultiConnectorContainer implements MultiConnectorContainerInterface
24
{
25
    /** @var array */
26
    private $connectors = [];
27
28
    /**
29
     * MultiConnectorInterface constructor.
30
     *
31
     * @param ConnectorInterface[] ...$connectorInterfaces
32
     */
33 2
    public function __construct(ConnectorInterface ...$connectorInterfaces)
34
    {
35
        /** @var ConnectorInterface $connector */
36 2
        foreach ($connectorInterfaces as $connector) {
37 2
            $this->connectors[$connector->getConnectorName()] = $connector;
0 ignored issues
show
Bug introduced by
The method getConnectorName cannot be called on $connector (of type array<integer,object<Web...ta\ConnectorInterface>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
38
        }
39 2
    }
40
41
    /**
42
     * Returns a Connector instance by name.
43
     *
44
     * @param string $name
45
     * @throws InvalidArgumentException
46
     * @return ConnectorInterface
47
     */
48 1
    public function getConnectorByName(string $name) : ConnectorInterface
49
    {
50 1
        if (!isset($this->connectors[$name])) {
51 1
            throw new InvalidArgumentException(
52 1
                sprintf('% is not a registered connector name.', $name),
53 1
                1000
54
            );
55
        }
56
57 1
        return clone $this->connectors[$name];
58
    }
59
}
60