SamlEntityService   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 90
rs 10
c 0
b 0
f 0
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A hasIdentityProvider() 0 12 2
A hasServiceProvider() 0 12 2
A __construct() 0 5 1
A getIdentityProvider() 0 10 3
A getServiceProvider() 0 10 3
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\StepupGateway\GatewayBundle\Service;
20
21
use Surfnet\SamlBundle\Entity\IdentityProvider;
22
use Surfnet\SamlBundle\Entity\ServiceProviderRepository;
23
use Surfnet\StepupGateway\GatewayBundle\Entity\SamlEntityRepository;
24
use Surfnet\StepupGateway\GatewayBundle\Entity\ServiceProvider;
25
use Surfnet\StepupGateway\GatewayBundle\Exception\RuntimeException;
26
27
class SamlEntityService implements ServiceProviderRepository
28
{
29
    /**
30
     * @var SamlEntityRepository
31
     */
32
    private $samlEntityRepository;
33
34
    /**
35
     * @var \Surfnet\SamlBundle\Entity\IdentityProvider[]
36
     */
37
    private $loadedIdentityProviders;
38
39
    /**
40
     * @var \Surfnet\SamlBundle\Entity\ServiceProvider[]
41
     */
42
    private $loadedServiceProviders;
43
44
    public function __construct(SamlEntityRepository $samlEntityRepository)
45
    {
46
        $this->samlEntityRepository = $samlEntityRepository;
47
        $this->loadedIdentityProviders = [];
48
        $this->loadedServiceProviders = [];
49
    }
50
51
    /**
52
     * @param string $entityId
53
     * @return IdentityProvider
54
     */
55
    public function getIdentityProvider($entityId)
56
    {
57
        if (!array_key_exists($entityId, $this->loadedIdentityProviders) && !$this->hasIdentityProvider($entityId)) {
58
            throw new RuntimeException(sprintf(
59
                'Failed at attempting to load unknown IdentityProvider with EntityId "%s"',
60
                $entityId
61
            ));
62
        }
63
64
        return $this->loadedIdentityProviders[$entityId];
65
    }
66
67
    /**
68
     * @param string $entityId
69
     * @return bool
70
     */
71
    public function hasIdentityProvider($entityId)
72
    {
73
        $samlEntity = $this->samlEntityRepository->getIdentityProvider($entityId);
74
75
        if (!$samlEntity) {
76
            return false;
77
        }
78
79
        $identityProvider = $samlEntity->toIdentityProvider();
80
        $this->loadedIdentityProviders[$identityProvider->getEntityId()] = $identityProvider;
81
82
        return true;
83
    }
84
85
    /**
86
     * @param string $entityId
87
     * @return ServiceProvider
88
     */
89
    public function getServiceProvider(string $entityId): \Surfnet\SamlBundle\Entity\ServiceProvider
90
    {
91
        if (!array_key_exists($entityId, $this->loadedServiceProviders) && !$this->hasServiceProvider($entityId)) {
92
            throw new RuntimeException(sprintf(
93
                'Failed at attempting to load unknown ServiceProvider with EntityId "%s"',
94
                $entityId
95
            ));
96
        }
97
98
        return $this->loadedServiceProviders[$entityId];
99
    }
100
101
    /**
102
     * @param string $entityId
103
     * @return bool
104
     */
105
    public function hasServiceProvider(string $entityId): bool
106
    {
107
        $samlEntity = $this->samlEntityRepository->getServiceProvider($entityId);
108
109
        if (!$samlEntity) {
110
            return false;
111
        }
112
113
        $serviceProvider = $samlEntity->toServiceProvider();
114
        $this->loadedServiceProviders[$serviceProvider->getEntityId()] = $serviceProvider;
115
116
        return true;
117
    }
118
}
119