ResolvePartyEntityIdAction   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 104
Duplicated Lines 13.46 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
dl 14
loc 104
c 0
b 0
f 0
wmc 13
lcom 1
cbo 9
ccs 42
cts 45
cp 0.9333
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
B doExecute() 0 48 10
A getPartyEntityDescriptor() 14 14 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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