CredentialContainerProvider   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 20
c 1
b 0
f 0
dl 0
loc 49
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addExtraCredential() 0 9 2
A register() 0 15 2
A __construct() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the LightSAML-Core package.
5
 *
6
 * (c) Milos Tomic <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace LightSaml\Bridge\Pimple\Container\Factory;
13
14
use LightSaml\Bridge\Pimple\Container\CredentialContainer;
15
use LightSaml\Build\Container\OwnContainerInterface;
16
use LightSaml\Build\Container\PartyContainerInterface;
17
use LightSaml\Credential\CredentialInterface;
18
use LightSaml\Error\LightSamlBuildException;
19
use LightSaml\Store\Credential\Factory\CredentialFactory;
20
use Pimple\Container;
21
use Pimple\ServiceProviderInterface;
22
23
class CredentialContainerProvider implements ServiceProviderInterface
24
{
25
    /** @var PartyContainerInterface */
26
    private $partyContainer;
27
28
    /** @var OwnContainerInterface */
29
    private $ownContainer;
30
31
    /** @var CredentialInterface[] */
32
    private $extraCredentials = [];
33
34
    public function __construct(PartyContainerInterface $partyContainer, OwnContainerInterface $ownContainer)
35
    {
36
        $this->ownContainer = $ownContainer;
37
        $this->partyContainer = $partyContainer;
38
    }
39
40
    /**
41
     * @return CredentialContainerProvider
42
     */
43
    public function addExtraCredential(CredentialInterface $credential)
44
    {
45
        if (null === $credential->getEntityId()) {
0 ignored issues
show
introduced by
The condition null === $credential->getEntityId() is always false.
Loading history...
46
            throw new \InvalidArgumentException('Extra credential must have entityID');
47
        }
48
49
        $this->extraCredentials[] = $credential;
50
51
        return $this;
52
    }
53
54
    /**
55
     * @param Container $pimple A container instance
56
     */
57
    public function register(Container $pimple)
58
    {
59
        $ownCredentials = $this->ownContainer->getOwnCredentials();
60
        if (empty($ownCredentials)) {
61
            throw new LightSamlBuildException('There are no own credentials');
62
        }
63
64
        $pimple[CredentialContainer::CREDENTIAL_STORE] = function () {
65
            $factory = new CredentialFactory();
66
67
            return $factory->build(
68
                $this->partyContainer->getIdpEntityDescriptorStore(),
69
                $this->partyContainer->getSpEntityDescriptorStore(),
70
                $this->ownContainer->getOwnCredentials(),
71
                $this->extraCredentials
72
            );
73
        };
74
    }
75
}
76