ProviderRepository::get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Copyright 2015 SURFnet bv
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupGateway\SamlStepupProviderBundle\Provider;
20
21
use Surfnet\StepupGateway\SamlStepupProviderBundle\Exception\InvalidConfigurationException;
22
use Surfnet\StepupGateway\SamlStepupProviderBundle\Exception\UnknownProviderException;
23
use function implode;
24
25
/**
26
 * @todo discuss (im)mutability
27
 */
28
final class ProviderRepository
29
{
30
    /**
31
     * @var []Provider
0 ignored issues
show
Documentation Bug introduced by
The doc comment []Provider at position 0 could not be parsed: Unknown type name '[' at position 0 in []Provider.
Loading history...
32
     */
33
    private $providers;
34
35
    public function __construct()
36
    {
37
        $this->providers = [];
38
    }
39
40
    /**
41
     * @param Provider $provider
42
     */
43
    public function addProvider(Provider $provider): void
44
    {
45
        if ($this->has($provider->getName())) {
46
            throw new InvalidConfigurationException(sprintf(
47
                'Provider "%s" has already been added to the repository',
48
                $provider->getName()
49
            ));
50
        }
51
52
        $this->providers[$provider->getName()] = $provider;
53
    }
54
55
    /**
56
     * @param string $providerName
57
     * @return bool
58
     */
59
    public function has($providerName)
60
    {
61
        return array_key_exists($providerName, $this->providers);
62
    }
63
64
    /**
65
     * @param string $providerName
66
     * @return Provider
67
     */
68
    public function get($providerName)
69
    {
70
        if (!$this->has($providerName)) {
71
            throw UnknownProviderException::create($providerName, implode(', ', array_keys($this->providers)));
72
        }
73
74
        return $this->providers[$providerName];
75
    }
76
}
77