Passed
Pull Request — develop (#295)
by Peter
04:30
created

ResponseBuilder::setResponseStatus()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 11
nc 6
nop 3
dl 0
loc 21
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Copyright 2014 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
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
Coding Style introduced by
Missing @link tag in file comment
Loading history...
18
19
namespace Surfnet\StepupGateway\GatewayBundle\Saml;
20
21
use SAML2\Constants;
22
use SAML2\Response;
23
use Surfnet\SamlBundle\Exception\LogicException;
24
25
class ResponseBuilder
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class ResponseBuilder
Loading history...
26
{
27
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
28
     * @var Response
29
     */
30
    private $response;
0 ignored issues
show
Coding Style introduced by
Private member variable "response" must be prefixed with an underscore
Loading history...
31
32
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
33
     * @var \Surfnet\StepupGateway\GatewayBundle\Saml\ResponseContext
34
     */
35
    private $responseContext;
0 ignored issues
show
Coding Style introduced by
Private member variable "responseContext" must be prefixed with an underscore
Loading history...
36
37
    public function createNewResponse(ResponseContext $context)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function createNewResponse()
Loading history...
38
    {
39
        if ($this->response) {
40
            throw new LogicException('Cannot create a new Response when still building a response.');
41
        }
42
43
        $this->responseContext = $context;
44
45
        $response = new Response();
46
        $response->setDestination($context->getDestination());
47
        $response->setIssuer($context->getIssuer());
48
        $response->setIssueInstant($context->getIssueInstant());
49
        $response->setInResponseTo($context->getInResponseTo());
50
51
        $this->response = $response;
52
53
        return $this;
54
    }
55
56
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
57
     * @param string $status
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
58
     * @param string|null $subStatus
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
59
     * @param string|null $message
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
60
     * @return $this
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
61
     */
62
    public function setResponseStatus($status, $subStatus = null, $message = null)
63
    {
64
        if (!$this->isValidResponseStatus($status)) {
65
            throw new LogicException(sprintf('Trying to set invalid Response Status'));
66
        }
67
68
        if ($subStatus && !$this->isValidResponseSubStatus($subStatus)) {
69
            throw new LogicException(sprintf('Trying to set invalid Response SubStatus'));
70
        }
71
72
        $status = ['Code' => $status];
73
        if ($subStatus) {
74
            $status['SubCode'] = $subStatus;
75
        }
76
        if ($message) {
77
            $status['Message'] = $message;
78
        }
79
80
        $this->response->setStatus($status);
81
82
        return $this;
83
    }
84
85
    public function get()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function get()
Loading history...
86
    {
87
        $response = $this->response;
88
89
        $this->response = null;
90
        $this->responseContext = null;
91
92
        return $response;
93
    }
94
95
    private function isValidResponseStatus($status)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function isValidResponseStatus()
Loading history...
Coding Style introduced by
Private method name "ResponseBuilder::isValidResponseStatus" must be prefixed with an underscore
Loading history...
96
    {
97
        return in_array($status, [
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
98
            Constants::STATUS_SUCCESS,            // weeee!
99
            Constants::STATUS_REQUESTER,          // Something is wrong with the AuthnRequest
100
            Constants::STATUS_RESPONDER,          // Something went wrong with the Response
101
            Constants::STATUS_VERSION_MISMATCH,   // The version of the request message was incorrect
102
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
103
    }
104
105
    private function isValidResponseSubStatus($subStatus)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function isValidResponseSubStatus()
Loading history...
Coding Style introduced by
Private method name "ResponseBuilder::isValidResponseSubStatus" must be prefixed with an underscore
Loading history...
106
    {
107
        return in_array($subStatus, [
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
108
            Constants::STATUS_AUTHN_FAILED,               // failed authentication
109
            Constants::STATUS_INVALID_ATTR,
110
            Constants::STATUS_INVALID_NAMEID_POLICY,
111
            Constants::STATUS_NO_AUTHN_CONTEXT,           // insufficient Loa or Loa cannot be met
112
            Constants::STATUS_NO_AVAILABLE_IDP,
113
            Constants::STATUS_NO_PASSIVE,
114
            Constants::STATUS_NO_SUPPORTED_IDP,
115
            Constants::STATUS_PARTIAL_LOGOUT,
116
            Constants::STATUS_PROXY_COUNT_EXCEEDED,
117
            Constants::STATUS_REQUEST_DENIED,
118
            Constants::STATUS_REQUEST_UNSUPPORTED,
119
            Constants::STATUS_REQUEST_VERSION_DEPRECATED,
120
            Constants::STATUS_REQUEST_VERSION_TOO_HIGH,
121
            Constants::STATUS_REQUEST_VERSION_TOO_LOW,
122
            Constants::STATUS_RESOURCE_NOT_RECOGNIZED,
123
            Constants::STATUS_TOO_MANY_RESPONSES,
124
            Constants::STATUS_UNKNOWN_ATTR_PROFILE,
125
            Constants::STATUS_UNKNOWN_PRINCIPAL,
126
            Constants::STATUS_UNSUPPORTED_BINDING,
127
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
128
    }
129
}
130