| Total Complexity | 18 |
| Total Lines | 188 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 0 | ||
| 1 | <?php |
||
| 11 | class ExternalSchemaListDocNormalizer |
||
| 12 | { |
||
| 13 | /** @var DefinitionRefResolver */ |
||
| 14 | private $definitionRefResolver; |
||
| 15 | /** @var TypeDocNormalizer */ |
||
| 16 | private $typeDocNormalizer; |
||
| 17 | /** @var ErrorDocNormalizer */ |
||
| 18 | private $errorDocNormalizer; |
||
| 19 | /** @var ShapeNormalizer */ |
||
| 20 | private $shapeNormalizer; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @param DefinitionRefResolver $definitionRefResolver |
||
| 24 | * @param TypeDocNormalizer $typeDocNormalizer |
||
| 25 | * @param ErrorDocNormalizer $errorDocNormalizer |
||
| 26 | * @param ShapeNormalizer $shapeNormalizer |
||
| 27 | */ |
||
| 28 | 7 | public function __construct( |
|
| 38 | 7 | } |
|
| 39 | |||
| 40 | /** |
||
| 41 | * @param ServerDoc $doc |
||
| 42 | * @return array |
||
| 43 | */ |
||
| 44 | 7 | public function normalize(ServerDoc $doc) |
|
| 45 | { |
||
| 46 | 7 | return array_merge( |
|
| 47 | 7 | $this->getMethodsExternalSchemaList($doc), |
|
| 48 | 7 | $this->getMethodErrorsExternalSchemaList($doc), |
|
| 49 | 7 | $this->getServerErrorsExtraSchemaList($doc), |
|
| 50 | 7 | $this->getDefaultSchemaList($doc) |
|
| 51 | ); |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param ServerDoc $doc |
||
| 56 | * |
||
| 57 | * @return array |
||
| 58 | */ |
||
| 59 | 7 | protected function getMethodsExternalSchemaList(ServerDoc $doc) |
|
| 60 | { |
||
| 61 | 7 | $list = []; |
|
| 62 | 7 | foreach ($doc->getMethodList() as $method) { |
|
| 63 | // Merge extra definitions |
||
| 64 | 4 | $list = array_merge($list, $this->getMethodExternalSchemaList($method)); |
|
| 65 | } |
||
| 66 | |||
| 67 | 7 | return $list; |
|
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @param ServerDoc $doc |
||
| 72 | * |
||
| 73 | * @return array |
||
| 74 | */ |
||
| 75 | 7 | protected function getMethodErrorsExternalSchemaList(ServerDoc $doc) |
|
| 76 | { |
||
| 77 | 7 | $list = []; |
|
| 78 | 7 | foreach ($doc->getMethodList() as $method) { |
|
| 79 | 4 | $list = array_merge( |
|
| 80 | 4 | $list, |
|
| 81 | 4 | $this->normalizeErrorList( |
|
| 82 | 4 | $method->getCustomErrorList(), |
|
| 83 | 4 | DefinitionRefResolver::CUSTOM_ERROR_DEFINITION_TYPE |
|
| 84 | ) |
||
| 85 | ); |
||
| 86 | } |
||
| 87 | |||
| 88 | 7 | return $list; |
|
| 89 | } |
||
| 90 | |||
| 91 | |||
| 92 | /** |
||
| 93 | * @param ServerDoc $doc |
||
| 94 | * |
||
| 95 | * @return array |
||
| 96 | */ |
||
| 97 | 7 | protected function getServerErrorsExtraSchemaList(ServerDoc $doc) |
|
| 107 | ) |
||
| 108 | ); |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @param MethodDoc $method |
||
| 113 | * |
||
| 114 | * @return array[] |
||
| 115 | */ |
||
| 116 | 4 | protected function getMethodExternalSchemaList(MethodDoc $method) : array |
|
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @param ServerDoc $doc |
||
| 142 | * @return array |
||
| 143 | */ |
||
| 144 | 7 | protected function getDefaultSchemaList(ServerDoc $doc) |
|
| 145 | { |
||
| 146 | $propertyList = [ |
||
| 147 | 7 | 'code' => [ |
|
| 148 | 'type' => 'integer', |
||
| 149 | ], |
||
| 150 | ]; |
||
| 151 | |||
| 152 | 7 | $codeList = []; |
|
| 153 | 7 | foreach ($doc->getServerErrorList() as $errorDoc) { |
|
| 154 | 2 | $codeList[] = $errorDoc->getCode(); |
|
| 155 | } |
||
| 156 | 7 | foreach ($doc->getGlobalErrorList() as $errorDoc) { |
|
| 157 | 2 | $codeList[] = $errorDoc->getCode(); |
|
| 158 | } |
||
| 159 | 7 | foreach ($doc->getMethodList() as $method) { |
|
| 160 | 4 | foreach ($method->getCustomErrorList() as $errorDoc) { |
|
| 161 | 4 | $codeList[] = $errorDoc->getCode(); |
|
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | 7 | $codeList = array_unique($codeList); |
|
| 166 | 7 | if (count($codeList) > 0) { |
|
| 167 | 4 | $propertyList['code']['enum'] = $codeList; |
|
| 168 | } |
||
| 169 | |||
| 170 | return [ |
||
| 171 | 'Default-Error' => [ |
||
| 172 | 'allOf' => [ |
||
| 173 | 7 | $this->shapeNormalizer->getErrorShapeDefinition(), |
|
| 174 | [ |
||
| 175 | 7 | 'type' => 'object', |
|
| 176 | 7 | 'properties' => $propertyList, |
|
| 177 | ], |
||
| 178 | ], |
||
| 179 | ], |
||
| 180 | ]; |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @param array $errorDocList |
||
| 185 | * @param string $definitionType |
||
| 186 | * |
||
| 187 | * @return array |
||
| 188 | */ |
||
| 189 | 7 | private function normalizeErrorList(array $errorDocList, $definitionType) |
|
| 199 | } |
||
| 200 | } |
||
| 201 |