1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2017 SURFnet B.V. |
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\Pdp\Dto; |
20
|
|
|
|
21
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Pdp\Dto\Response\AssociatedAdvice; |
22
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Pdp\Dto\Response\AttributeAssignment; |
23
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Pdp\Dto\Response\Category; |
24
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Pdp\Dto\Response\Obligation; |
25
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Pdp\Dto\Response\PolicyIdReference; |
26
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Pdp\Dto\Response\PolicyIdentifier; |
27
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Pdp\Dto\Response\PolicySetIdReference; |
28
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Pdp\Dto\Response\Status; |
29
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Pdp\Dto\Response\StatusCode; |
30
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Pdp\Exception\InvalidPdpResponseException; |
31
|
|
|
|
32
|
|
|
final class Response |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var Status |
36
|
|
|
*/ |
37
|
|
|
public $status; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var Category[] |
41
|
|
|
*/ |
42
|
|
|
public $categories; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var AssociatedAdvice[] |
46
|
|
|
*/ |
47
|
|
|
public $associatedAdvices; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var PolicyIdentifier |
51
|
|
|
*/ |
52
|
|
|
public $policyIdentifier; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
public $decision; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var Obligation[] |
61
|
|
|
*/ |
62
|
|
|
public $obligations; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param array $jsonData |
66
|
|
|
* @return Response |
67
|
|
|
* |
68
|
|
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity) Deserializing a response to named DTOs |
69
|
|
|
* @SuppressWarnings(PHPMD.NPathComplexity) A response has a lot of constraints |
70
|
|
|
* @SuppressWarnings(PHPMD.ExcessiveMethodLength) Private methods would merely be code relocation |
71
|
|
|
*/ |
72
|
|
|
public static function fromData(array $jsonData) |
73
|
|
|
{ |
74
|
|
|
if (!isset($jsonData['Response'])) { |
75
|
|
|
throw new InvalidPdpResponseException('Key "Response" was not found in the PDP response'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if (!is_array($jsonData['Response'])) { |
79
|
|
|
throw new InvalidPdpResponseException('Key "Response" is not an array'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
if (!isset($jsonData['Response'][0])) { |
84
|
|
|
throw new InvalidPdpResponseException('No response data found'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$responseData = $jsonData['Response'][0]; |
88
|
|
|
|
89
|
|
|
if (!isset($responseData['Status'])) { |
90
|
|
|
throw new InvalidPdpResponseException('Key "Status" was not found in the PDP response'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if (!isset($responseData['Decision'])) { |
94
|
|
|
throw new InvalidPdpResponseException('Key "Decision" was not found in the PDP response'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$response = new self; |
98
|
|
|
|
99
|
|
|
$response->status = new Status; |
100
|
|
|
$response->status->statusCode = new StatusCode; |
101
|
|
|
$response->status->statusCode->value = $responseData['Status']['StatusCode']['Value']; |
102
|
|
View Code Duplication |
if (isset($responseData['Status']['StatusDetail'])) { |
|
|
|
|
103
|
|
|
$response->status->statusDetail = $responseData['Status']['StatusDetail']; |
104
|
|
|
} |
105
|
|
View Code Duplication |
if (isset($responseData['Status']['StatusMessage'])) { |
|
|
|
|
106
|
|
|
$response->status->statusMessage = $responseData['Status']['StatusMessage']; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if (isset($responseData['Category'])) { |
110
|
|
|
foreach ($responseData['Category'] as $categoryData) { |
111
|
|
|
$category = new Category; |
112
|
|
|
$category->categoryId = $categoryData['CategoryId']; |
113
|
|
|
$category->attributes = []; |
114
|
|
|
|
115
|
|
|
foreach ($categoryData['Attribute'] as $attributeData) { |
116
|
|
|
$attribute = new Attribute; |
117
|
|
|
$attribute->attributeId = $attributeData['AttributeId']; |
118
|
|
|
$attribute->value = $attributeData['Value']; |
119
|
|
|
|
120
|
|
|
if (isset($attributeData['DataType'])) { |
121
|
|
|
$attribute->dataType = $attributeData['DataType']; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$category->attributes[] = $attribute; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$response->categories[] = $category; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
View Code Duplication |
if (isset($responseData['AssociatedAdvice'])) { |
|
|
|
|
132
|
|
|
foreach ($responseData['AssociatedAdvice'] as $associatedAdviceData) { |
133
|
|
|
$associatedAdvice = new AssociatedAdvice; |
134
|
|
|
$associatedAdvice->id = $associatedAdviceData['Id']; |
135
|
|
|
|
136
|
|
|
foreach ($associatedAdviceData['AttributeAssignment'] as $attributeAssignmentData) { |
137
|
|
|
$associatedAdvice->attributeAssignments[] = self::parseAttributeAssignmentData($attributeAssignmentData); |
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$response->associatedAdvices[] = $associatedAdvice; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
View Code Duplication |
if (isset($responseData['Obligations'])) { |
|
|
|
|
145
|
|
|
foreach ($responseData['Obligations'] as $obligationData) { |
146
|
|
|
$obligation = new Obligation; |
147
|
|
|
$obligation->id = $obligationData['Id']; |
148
|
|
|
|
149
|
|
|
foreach ($obligationData['AttributeAssignment'] as $attributeAssignmentData) { |
150
|
|
|
$obligation->attributeAssignments[] = self::parseAttributeAssignmentData($attributeAssignmentData); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$response->obligations[] = $obligation; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
if (isset($responseData['PolicyIdentifier'])) { |
158
|
|
|
$response->policyIdentifier = new PolicyIdentifier; |
159
|
|
|
|
160
|
|
View Code Duplication |
if (isset($responseData['PolicyIdentifier']['PolicySetIdReference'])) { |
|
|
|
|
161
|
|
|
foreach ($responseData['PolicyIdentifier']['PolicySetIdReference'] as $policySetIdReferenceData) { |
162
|
|
|
$policySetIdReference = new PolicySetIdReference; |
163
|
|
|
$policySetIdReference->version = $policySetIdReferenceData['Version']; |
164
|
|
|
$policySetIdReference->id = $policySetIdReferenceData['Id']; |
165
|
|
|
$response->policyIdentifier->policySetIdReference[] = $policySetIdReference; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
View Code Duplication |
if (isset($responseData['PolicyIdentifier']['PolicyIdReference'])) { |
|
|
|
|
170
|
|
|
foreach ($responseData['PolicyIdentifier']['PolicyIdReference'] as $policyIdReferenceData) { |
171
|
|
|
$policyIdReference = new PolicyIdReference; |
172
|
|
|
$policyIdReference->version = $policyIdReferenceData['Version']; |
173
|
|
|
$policyIdReference->id = $policyIdReferenceData['Id']; |
174
|
|
|
$response->policyIdentifier->policyIdReference[] = $policyIdReference; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
$response->decision = $responseData['Decision']; |
180
|
|
|
|
181
|
|
|
return $response; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param array $attributeAssignmentData |
186
|
|
|
* @return AttributeAssignment |
187
|
|
|
*/ |
188
|
|
|
private static function parseAttributeAssignmentData(array $attributeAssignmentData) |
189
|
|
|
{ |
190
|
|
|
$attributeAssignment = new AttributeAssignment; |
191
|
|
|
$attributeAssignment->category = $attributeAssignmentData['Category']; |
192
|
|
|
$attributeAssignment->attributeId = $attributeAssignmentData['AttributeId']; |
193
|
|
|
$attributeAssignment->value = $attributeAssignmentData['Value']; |
194
|
|
|
if (isset($attributeAssignmentData['DataType'])) { |
195
|
|
|
$attributeAssignment->dataType = $attributeAssignmentData['DataType']; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
return $attributeAssignment; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
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.