Completed
Pull Request — develop (#93)
by Boy
05:37 queued 02:34
created

SamlEntityService::hasIdentityProvider()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 2
eloc 7
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\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 \Surfnet\StepupGateway\GatewayBundle\Entity\DoctrineSamlEntityRepository
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;
0 ignored issues
show
Documentation Bug introduced by
$samlEntityRepository is of type object<Surfnet\StepupGat...y\SamlEntityRepository>, but the property $samlEntityRepository was declared to be of type object<Surfnet\StepupGat...neSamlEntityRepository>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
47
        $this->loadedIdentityProviders = [];
48
        $this->loadedServiceProviders = [];
49
    }
50
51
    /**
52
     * @param string $entityId
53
     * @return IdentityProvider
54
     */
55 View Code Duplication
    public function getIdentityProvider($entityId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function getServiceProvider($entityId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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($entityId)
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