Completed
Push — feature/tests-and-symfony-34 ( d06f89 )
by Michiel
04:18 queued 01:47
created

FailedResponseService   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 107
Duplicated Lines 41.12 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 44
loc 107
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A sendLoaCannotBeGiven() 20 20 1
A sendAuthenticationCancelledByUser() 24 24 1
A createRequesterFailureResponse() 0 9 1
A createResponseFailureResponse() 0 9 1

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
 * Copyright 2018 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\Gateway;
20
21
use SAML2\Constants;
22
use SAML2\Response as SAMLResponse;
23
use Surfnet\SamlBundle\Monolog\SamlAuthenticationLogger;
24
use Surfnet\StepupGateway\GatewayBundle\Saml\ResponseBuilder;
25
use Surfnet\StepupGateway\GatewayBundle\Saml\ResponseContext;
26
27
class FailedResponseService
28
{
29
    /** @var SamlAuthenticationLogger */
30
    private $samlLogger;
31
32
    /** @var ResponseBuilder */
33
    private $responseBuilder;
34
35
    /**
36
     * GatewayServiceProviderService constructor.
37
     * @param SamlAuthenticationLogger $samlLogger
38
     * @param ResponseBuilder $responseBuilder
39
     */
40
    public function __construct(
41
        SamlAuthenticationLogger $samlLogger,
42
        ResponseBuilder $responseBuilder
43
    ) {
44
        $this->samlLogger = $samlLogger;
45
        $this->responseBuilder = $responseBuilder;
46
    }
47
48
    /**
49
     * Return a SAMLResponse indicating that the given Loa is invalid.
50
     *
51
     * @param ResponseContext $responseContext
52
     * @return SAMLResponse
53
     */
54 View Code Duplication
    public function sendLoaCannotBeGiven(ResponseContext $responseContext)
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...
55
    {
56
        $originalRequestId = $responseContext->getInResponseTo();
57
58
        $logger = $this->samlLogger->forAuthentication($originalRequestId);
59
        $logger->notice('Loa cannot be given, creating Response with NoAuthnContext status');
60
61
        $response = $this->responseBuilder
62
            ->createNewResponse($responseContext)
63
            ->setResponseStatus(Constants::STATUS_RESPONDER, Constants::STATUS_NO_AUTHN_CONTEXT)
64
            ->get();
65
66
        $logger->notice(sprintf(
67
            'Responding to request "%s" with response based on response from the remote IdP with response "%s"',
68
            $responseContext->getInResponseTo(),
69
            $response->getId()
70
        ));
71
72
        return $response;
73
    }
74
75
    /**
76
     * Return a SAMLResponse indicating that the authentication is cancelled by the user.
77
     *
78
     * @param ResponseContext $responseContext
79
     * @return SAMLResponse
80
     */
81 View Code Duplication
    public function sendAuthenticationCancelledByUser(ResponseContext $responseContext)
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...
82
    {
83
        $originalRequestId = $responseContext->getInResponseTo();
84
85
        $logger = $this->samlLogger->forAuthentication($originalRequestId);
86
        $logger->notice('Authentication was cancelled by the user, creating Response with AuthnFailed status');
87
88
        $response = $this->responseBuilder
89
            ->createNewResponse($responseContext)
90
            ->setResponseStatus(
91
                Constants::STATUS_RESPONDER,
92
                Constants::STATUS_AUTHN_FAILED,
93
                'Authentication cancelled by user'
94
            )
95
            ->get();
96
97
        $logger->notice(sprintf(
98
            'Responding to request "%s" with response based on response from the remote IdP with response "%s"',
99
            $responseContext->getInResponseTo(),
100
            $response->getId()
101
        ));
102
103
        return $response;
104
    }
105
106
    /**
107
     * @param ResponseContext $responseContext
108
     * @return SAMLResponse
109
     */
110
    public function createRequesterFailureResponse(ResponseContext $responseContext)
111
    {
112
        $response = $this->responseBuilder
113
            ->createNewResponse($responseContext)
114
            ->setResponseStatus(Constants::STATUS_REQUESTER, Constants::STATUS_REQUEST_UNSUPPORTED)
115
            ->get();
116
117
        return $response;
118
    }
119
120
    /**
121
     * @param $context
122
     * @return SAMLResponse
123
     */
124
    public function createResponseFailureResponse($context)
125
    {
126
        $response = $this->responseBuilder
127
            ->createNewResponse($context)
128
            ->setResponseStatus(Constants::STATUS_RESPONDER)
129
            ->get();
130
131
        return $response;
132
    }
133
}
134