getPartyEntityDescriptor()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 2.3149

Importance

Changes 0
Metric Value
dl 14
loc 14
c 0
b 0
f 0
ccs 4
cts 7
cp 0.5714
rs 9.7998
cc 2
nc 2
nop 3
crap 2.3149
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\Action\Profile\Inbound\Message;
13
14
use LightSaml\Action\Profile\AbstractProfileAction;
15
use LightSaml\Context\Profile\Helper\LogHelper;
16
use LightSaml\Context\Profile\ProfileContext;
17
use LightSaml\Error\LightSamlContextException;
18
use LightSaml\Meta\TrustOptions\TrustOptions;
19
use LightSaml\Store\EntityDescriptor\EntityDescriptorStoreInterface;
20
use LightSaml\Store\TrustOptions\TrustOptionsStoreInterface;
21
use Psr\Log\LoggerInterface;
22
23
/**
24
 * Looks up inbound message Issuer in entity descriptor providers and sets it to the party context.
25
 */
26
class ResolvePartyEntityIdAction extends AbstractProfileAction
27
{
28
    /** @var EntityDescriptorStoreInterface */
29
    private $spEntityDescriptorProvider;
30
31
    /** @var EntityDescriptorStoreInterface */
32
    private $idpEntityDescriptorProvider;
33
34
    /** @var TrustOptionsStoreInterface */
35
    protected $trustOptionsProvider;
36
37
    /**
38
     * @param LoggerInterface                $logger
39
     * @param EntityDescriptorStoreInterface $spEntityDescriptorProvider
40
     * @param EntityDescriptorStoreInterface $idpEntityDescriptorProvider
41
     * @param TrustOptionsStoreInterface     $trustOptionsProvider
42
     */
43 8
    public function __construct(
44
        LoggerInterface $logger,
45
        EntityDescriptorStoreInterface $spEntityDescriptorProvider,
46
        EntityDescriptorStoreInterface $idpEntityDescriptorProvider,
47
        TrustOptionsStoreInterface $trustOptionsProvider
48
    ) {
49 8
        parent::__construct($logger);
50
51 8
        $this->spEntityDescriptorProvider = $spEntityDescriptorProvider;
52 8
        $this->idpEntityDescriptorProvider = $idpEntityDescriptorProvider;
53 8
        $this->trustOptionsProvider = $trustOptionsProvider;
54 8
    }
55
56
    /**
57
     * @param ProfileContext $context
58
     */
59 7
    protected function doExecute(ProfileContext $context)
60
    {
61 7
        $partyContext = $context->getPartyEntityContext();
62
63 7
        if ($partyContext->getEntityDescriptor() && $partyContext->getTrustOptions()) {
64 1
            $this->logger->debug(
65 1
                sprintf('Party EntityDescriptor and TrustOptions already set for "%s"', $partyContext->getEntityDescriptor()->getEntityID()),
66 1
                LogHelper::getActionContext($context, $this, array(
67 1
                    'partyEntityId' => $partyContext->getEntityDescriptor()->getEntityID(),
68
                ))
69
            );
70
71 1
            return;
72
        }
73
74 6
        $entityId = $partyContext->getEntityDescriptor() ? $partyContext->getEntityDescriptor()->getEntityID() : null;
75 6
        $entityId = $entityId ? $entityId : $partyContext->getEntityId();
76 6
        if (null == $entityId) {
77 1
            $message = 'EntityID is not set in the party context';
78 1
            $this->logger->critical($message, LogHelper::getActionErrorContext($context, $this));
79 1
            throw new LightSamlContextException($context, $message);
80
        }
81
82 5
        if (null == $partyContext->getEntityDescriptor()) {
83 3
            $partyEntityDescriptor = $this->getPartyEntityDescriptor(
84 3
                $context,
85 3
                ProfileContext::ROLE_IDP === $context->getOwnRole()
86 1
                ? $this->spEntityDescriptorProvider
87 3
                : $this->idpEntityDescriptorProvider,
88 3
                $context->getPartyEntityContext()->getEntityId()
89
            );
90 3
            $partyContext->setEntityDescriptor($partyEntityDescriptor);
91 3
            $this->logger->debug(
92 3
                sprintf('Known issuer resolved: "%s"', $partyEntityDescriptor->getEntityID()),
93 3
                LogHelper::getActionContext($context, $this, array(
94 3
                    'partyEntityId' => $partyEntityDescriptor->getEntityID(),
95
                ))
96
            );
97
        }
98
99 5
        if (null == $partyContext->getTrustOptions()) {
100 3
            $trustOptions = $this->trustOptionsProvider->get($partyContext->getEntityDescriptor()->getEntityID());
101 3
            if (null === $trustOptions) {
102 1
                $trustOptions = new TrustOptions();
103
            }
104 3
            $partyContext->setTrustOptions($trustOptions);
105
        }
106 5
    }
107
108
    /**
109
     * @param ProfileContext                 $context
110
     * @param EntityDescriptorStoreInterface $entityDescriptorProvider
111
     * @param string                         $entityId
112
     *
113
     * @return \LightSaml\Model\Metadata\EntityDescriptor
114
     */
115 3 View Code Duplication
    protected function getPartyEntityDescriptor(
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...
116
        ProfileContext $context,
117
        EntityDescriptorStoreInterface $entityDescriptorProvider,
118
        $entityId
119
    ) {
120 3
        $partyEntityDescriptor = $entityDescriptorProvider->get($entityId);
121 3
        if (null === $partyEntityDescriptor) {
122
            $message = sprintf("Unknown issuer '%s'", $entityId);
123
            $this->logger->emergency($message, LogHelper::getActionErrorContext($context, $this));
124
            throw new LightSamlContextException($context, $message);
125
        }
126
127 3
        return $partyEntityDescriptor;
128
    }
129
}
130