ProviderRepository::addProvider()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * Copyright 2014 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\StepupRa\SamlStepupProviderBundle\Provider;
20
21
use Surfnet\StepupRa\SamlStepupProviderBundle\Exception\InvalidConfigurationException;
22
use Surfnet\StepupRa\SamlStepupProviderBundle\Exception\UnknownProviderException;
23
24
final class ProviderRepository
25
{
26
    /**
27
     * @var []Provider
28
     */
29
    private $providers;
30
31
    public function __construct()
32
    {
33
        $this->providers = [];
34
    }
35
36
    /**
37
     * @param Provider $provider
38
     */
39
    public function addProvider(Provider $provider)
40
    {
41
        if ($this->has($provider->getName())) {
42
            throw new InvalidConfigurationException(sprintf(
43
                'Provider "%s" has already been added to the repository',
44
                $provider->getName()
45
            ));
46
        }
47
48
        $this->providers[$provider->getName()] = $provider;
49
    }
50
51
    /**
52
     * @param string $providerName
53
     * @return bool
54
     */
55
    public function has($providerName)
56
    {
57
        return array_key_exists($providerName, $this->providers);
58
    }
59
60
    /**
61
     * @param string $providerName
62
     * @return Provider
63
     */
64
    public function get($providerName)
65
    {
66
        if (!$this->has($providerName)) {
67
            throw UnknownProviderException::create($providerName, array_keys($this->providers));
68
        }
69
70
        return $this->providers[$providerName];
71
    }
72
}
73