Skip to content

Commit 14a7930

Browse files
committed
refactoring: buildResponseForException() moved to Builder
1 parent 91187ad commit 14a7930

File tree

2 files changed

+102
-99
lines changed

2 files changed

+102
-99
lines changed

src/CXml/Builder.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@
44

55
namespace CXml;
66

7+
use CXml\Exception\CXmlAuthenticationInvalidException;
8+
use CXml\Exception\CXmlConflictException;
9+
use CXml\Exception\CXmlCredentialInvalidException;
710
use CXml\Exception\CXmlException;
11+
use CXml\Exception\CXmlExpectationFailedException;
12+
use CXml\Exception\CXmlNotAcceptableException;
13+
use CXml\Exception\CXmlNotImplementedException;
14+
use CXml\Exception\CXmlPreconditionFailedException;
815
use CXml\Model\Credential;
916
use CXml\Model\CXml;
1017
use CXml\Model\Header;
@@ -23,6 +30,87 @@
2330

2431
class Builder
2532
{
33+
// according to cXML reference document
34+
private static array $exceptionMapping = [
35+
CXmlAuthenticationInvalidException::class => 401,
36+
CXmlNotAcceptableException::class => 406,
37+
CXmlConflictException::class => 409,
38+
CXmlPreconditionFailedException::class => 412,
39+
CXmlExpectationFailedException::class => 417,
40+
CXmlCredentialInvalidException::class => 417,
41+
CXmlNotImplementedException::class => 450,
42+
];
43+
44+
// TODO create enum for this?
45+
private static array $exceptionCodeMapping = [
46+
// cxml
47+
450 => 'Not Implemented',
48+
49+
// http - shamelessly copied from Symfony\Component\HttpFoundation\Response
50+
100 => 'Continue',
51+
101 => 'Switching Protocols',
52+
102 => 'Processing', // RFC2518
53+
103 => 'Early Hints',
54+
200 => 'OK',
55+
201 => 'Created',
56+
202 => 'Accepted',
57+
203 => 'Non-Authoritative Information',
58+
204 => 'No Content',
59+
205 => 'Reset Content',
60+
206 => 'Partial Content',
61+
207 => 'Multi-Status', // RFC4918
62+
208 => 'Already Reported', // RFC5842
63+
226 => 'IM Used', // RFC3229
64+
300 => 'Multiple Choices',
65+
301 => 'Moved Permanently',
66+
302 => 'Found',
67+
303 => 'See Other',
68+
304 => 'Not Modified',
69+
305 => 'Use Proxy',
70+
307 => 'Temporary Redirect',
71+
308 => 'Permanent Redirect', // RFC7238
72+
400 => 'Bad Request',
73+
401 => 'Unauthorized',
74+
402 => 'Payment Required',
75+
403 => 'Forbidden',
76+
404 => 'Not Found',
77+
405 => 'Method Not Allowed',
78+
406 => 'Not Acceptable',
79+
407 => 'Proxy Authentication Required',
80+
408 => 'Request Timeout',
81+
409 => 'Conflict',
82+
410 => 'Gone',
83+
411 => 'Length Required',
84+
412 => 'Precondition Failed',
85+
413 => 'Payload Too Large',
86+
414 => 'URI Too Long',
87+
415 => 'Unsupported Media Type',
88+
416 => 'Range Not Satisfiable',
89+
417 => 'Expectation Failed',
90+
418 => "I'm a teapot", // RFC2324
91+
421 => 'Misdirected Request', // RFC7540
92+
422 => 'Unprocessable Entity', // RFC4918
93+
423 => 'Locked', // RFC4918
94+
424 => 'Failed Dependency', // RFC4918
95+
425 => 'Too Early', // RFC-ietf-httpbis-replay-04
96+
426 => 'Upgrade Required', // RFC2817
97+
428 => 'Precondition Required', // RFC6585
98+
429 => 'Too Many Requests', // RFC6585
99+
431 => 'Request Header Fields Too Large', // RFC6585
100+
451 => 'Unavailable For Legal Reasons', // RFC7725
101+
500 => 'Internal Server Error',
102+
501 => 'Not Implemented',
103+
502 => 'Bad Gateway',
104+
503 => 'Service Unavailable',
105+
504 => 'Gateway Timeout',
106+
505 => 'HTTP Version Not Supported',
107+
506 => 'Variant Also Negotiates', // RFC2295
108+
507 => 'Insufficient Storage', // RFC4918
109+
508 => 'Loop Detected', // RFC5842
110+
510 => 'Not Extended', // RFC2774
111+
511 => 'Network Authentication Required', // RFC6585
112+
];
113+
26114
private readonly PayloadIdentityFactoryInterface $payloadIdentityFactory;
27115

28116
private ?PayloadInterface $payload = null;
@@ -169,4 +257,18 @@ public function build(string $deploymentMode = null): CXml
169257

170258
return $cXml;
171259
}
260+
261+
/**
262+
* @throws CXmlException
263+
*/
264+
public function buildResponseForException(CXmlException $exception): CXml
265+
{
266+
$statusCode = self::$exceptionMapping[$exception::class] ?? 500;
267+
$statusText = self::$exceptionCodeMapping[$statusCode] ?? 'Unknown status';
268+
$status = new Status($statusCode, $statusText, $exception->getMessage());
269+
270+
return $this
271+
->status($status)
272+
->build();
273+
}
172274
}

src/CXml/Processor/Processor.php

Lines changed: 0 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,7 @@
66

77
use CXml\Builder;
88
use CXml\Context;
9-
use CXml\Exception\CXmlAuthenticationInvalidException;
10-
use CXml\Exception\CXmlConflictException;
11-
use CXml\Exception\CXmlCredentialInvalidException;
129
use CXml\Exception\CXmlException;
13-
use CXml\Exception\CXmlExpectationFailedException;
14-
use CXml\Exception\CXmlNotAcceptableException;
15-
use CXml\Exception\CXmlNotImplementedException;
16-
use CXml\Exception\CXmlPreconditionFailedException;
1710
use CXml\Handler\HandlerInterface;
1811
use CXml\Handler\HandlerRegistryInterface;
1912
use CXml\Model\CXml;
@@ -32,87 +25,6 @@
3225

3326
class Processor
3427
{
35-
// according to cXML reference document
36-
private static array $exceptionMapping = [
37-
CXmlAuthenticationInvalidException::class => 401,
38-
CXmlNotAcceptableException::class => 406,
39-
CXmlConflictException::class => 409,
40-
CXmlPreconditionFailedException::class => 412,
41-
CXmlExpectationFailedException::class => 417,
42-
CXmlCredentialInvalidException::class => 417,
43-
CXmlNotImplementedException::class => 450,
44-
];
45-
46-
// TODO create enum for this?
47-
private static array $exceptionCodeMapping = [
48-
// cxml
49-
450 => 'Not Implemented',
50-
51-
// http - shamelessly copied from Symfony\Component\HttpFoundation\Response
52-
100 => 'Continue',
53-
101 => 'Switching Protocols',
54-
102 => 'Processing', // RFC2518
55-
103 => 'Early Hints',
56-
200 => 'OK',
57-
201 => 'Created',
58-
202 => 'Accepted',
59-
203 => 'Non-Authoritative Information',
60-
204 => 'No Content',
61-
205 => 'Reset Content',
62-
206 => 'Partial Content',
63-
207 => 'Multi-Status', // RFC4918
64-
208 => 'Already Reported', // RFC5842
65-
226 => 'IM Used', // RFC3229
66-
300 => 'Multiple Choices',
67-
301 => 'Moved Permanently',
68-
302 => 'Found',
69-
303 => 'See Other',
70-
304 => 'Not Modified',
71-
305 => 'Use Proxy',
72-
307 => 'Temporary Redirect',
73-
308 => 'Permanent Redirect', // RFC7238
74-
400 => 'Bad Request',
75-
401 => 'Unauthorized',
76-
402 => 'Payment Required',
77-
403 => 'Forbidden',
78-
404 => 'Not Found',
79-
405 => 'Method Not Allowed',
80-
406 => 'Not Acceptable',
81-
407 => 'Proxy Authentication Required',
82-
408 => 'Request Timeout',
83-
409 => 'Conflict',
84-
410 => 'Gone',
85-
411 => 'Length Required',
86-
412 => 'Precondition Failed',
87-
413 => 'Payload Too Large',
88-
414 => 'URI Too Long',
89-
415 => 'Unsupported Media Type',
90-
416 => 'Range Not Satisfiable',
91-
417 => 'Expectation Failed',
92-
418 => "I'm a teapot", // RFC2324
93-
421 => 'Misdirected Request', // RFC7540
94-
422 => 'Unprocessable Entity', // RFC4918
95-
423 => 'Locked', // RFC4918
96-
424 => 'Failed Dependency', // RFC4918
97-
425 => 'Too Early', // RFC-ietf-httpbis-replay-04
98-
426 => 'Upgrade Required', // RFC2817
99-
428 => 'Precondition Required', // RFC6585
100-
429 => 'Too Many Requests', // RFC6585
101-
431 => 'Request Header Fields Too Large', // RFC6585
102-
451 => 'Unavailable For Legal Reasons', // RFC7725
103-
500 => 'Internal Server Error',
104-
501 => 'Not Implemented',
105-
502 => 'Bad Gateway',
106-
503 => 'Service Unavailable',
107-
504 => 'Gateway Timeout',
108-
505 => 'HTTP Version Not Supported',
109-
506 => 'Variant Also Negotiates', // RFC2295
110-
507 => 'Insufficient Storage', // RFC4918
111-
508 => 'Loop Detected', // RFC5842
112-
510 => 'Not Extended', // RFC2774
113-
511 => 'Network Authentication Required', // RFC6585
114-
];
115-
11628
public function __construct(private readonly HeaderProcessor $headerProcessor, private readonly HandlerRegistryInterface $handlerRegistry, private readonly Builder $builder, private readonly ?EventDispatcherInterface $eventDispatcher = null)
11729
{
11830
}
@@ -242,15 +154,4 @@ private function processRequest(Request $request, Context $context): CXml
242154
->payload($response)
243155
->build();
244156
}
245-
246-
public function buildResponseForException(CXmlException $exception): CXml
247-
{
248-
$statusCode = self::$exceptionMapping[$exception::class] ?? 500;
249-
$statusText = self::$exceptionCodeMapping[$statusCode] ?? 'Unknown status';
250-
$status = new Status($statusCode, $statusText, $exception->getMessage());
251-
252-
return $this->builder
253-
->status($status)
254-
->build();
255-
}
256157
}

0 commit comments

Comments
 (0)