ResolvePartyEntityIdAction::doExecute()   B
last analyzed

Complexity

Conditions 10
Paths 29

Size

Total Lines 46
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 46
rs 7.6666
cc 10
nc 29
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
    public function __construct(
38
        LoggerInterface $logger,
39
        EntityDescriptorStoreInterface $spEntityDescriptorProvider,
40
        EntityDescriptorStoreInterface $idpEntityDescriptorProvider,
41
        TrustOptionsStoreInterface $trustOptionsProvider
42
    ) {
43
        parent::__construct($logger);
44
45
        $this->spEntityDescriptorProvider = $spEntityDescriptorProvider;
46
        $this->idpEntityDescriptorProvider = $idpEntityDescriptorProvider;
47
        $this->trustOptionsProvider = $trustOptionsProvider;
48
    }
49
50
    protected function doExecute(ProfileContext $context)
51
    {
52
        $partyContext = $context->getPartyEntityContext();
53
54
        if ($partyContext->getEntityDescriptor() && $partyContext->getTrustOptions()) {
55
            $this->logger->debug(
56
                sprintf('Party EntityDescriptor and TrustOptions already set for "%s"', $partyContext->getEntityDescriptor()->getEntityID()),
57
                LogHelper::getActionContext($context, $this, [
58
                    'partyEntityId' => $partyContext->getEntityDescriptor()->getEntityID(),
59
                ])
60
            );
61
62
            return;
63
        }
64
65
        $entityId = $partyContext->getEntityDescriptor() ? $partyContext->getEntityDescriptor()->getEntityID() : null;
66
        $entityId = $entityId ? $entityId : $partyContext->getEntityId();
67
        if (null == $entityId) {
68
            $message = 'EntityID is not set in the party context';
69
            $this->logger->critical($message, LogHelper::getActionErrorContext($context, $this));
70
            throw new LightSamlContextException($context, $message);
71
        }
72
73
        if (null == $partyContext->getEntityDescriptor()) {
74
            $partyEntityDescriptor = $this->getPartyEntityDescriptor(
75
                $context,
76
                ProfileContext::ROLE_IDP === $context->getOwnRole()
77
                ? $this->spEntityDescriptorProvider
78
                : $this->idpEntityDescriptorProvider,
79
                $context->getPartyEntityContext()->getEntityId()
80
            );
81
            $partyContext->setEntityDescriptor($partyEntityDescriptor);
82
            $this->logger->debug(
83
                sprintf('Known issuer resolved: "%s"', $partyEntityDescriptor->getEntityID()),
84
                LogHelper::getActionContext($context, $this, [
85
                    'partyEntityId' => $partyEntityDescriptor->getEntityID(),
86
                ])
87
            );
88
        }
89
90
        if (null == $partyContext->getTrustOptions()) {
91
            $trustOptions = $this->trustOptionsProvider->get($partyContext->getEntityDescriptor()->getEntityID());
92
            if (null === $trustOptions) {
93
                $trustOptions = new TrustOptions();
94
            }
95
            $partyContext->setTrustOptions($trustOptions);
96
        }
97
    }
98
99
    /**
100
     * @param string $entityId
101
     *
102
     * @return \LightSaml\Model\Metadata\EntityDescriptor
103
     */
104
    protected function getPartyEntityDescriptor(
105
        ProfileContext $context,
106
        EntityDescriptorStoreInterface $entityDescriptorProvider,
107
        $entityId
108
    ) {
109
        $partyEntityDescriptor = $entityDescriptorProvider->get($entityId);
110
        if (null === $partyEntityDescriptor) {
111
            $message = sprintf("Unknown issuer '%s'", $entityId);
112
            $this->logger->emergency($message, LogHelper::getActionErrorContext($context, $this));
113
            throw new LightSamlContextException($context, $message);
114
        }
115
116
        return $partyEntityDescriptor;
117
    }
118
}
119