Skip to content

Commit

Permalink
refactoring: buildResponseForException() moved to Builder
Browse files Browse the repository at this point in the history
  • Loading branch information
mathielen committed Nov 13, 2024
1 parent 91187ad commit 14a7930
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 99 deletions.
102 changes: 102 additions & 0 deletions src/CXml/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@

namespace CXml;

use CXml\Exception\CXmlAuthenticationInvalidException;
use CXml\Exception\CXmlConflictException;
use CXml\Exception\CXmlCredentialInvalidException;
use CXml\Exception\CXmlException;
use CXml\Exception\CXmlExpectationFailedException;
use CXml\Exception\CXmlNotAcceptableException;
use CXml\Exception\CXmlNotImplementedException;
use CXml\Exception\CXmlPreconditionFailedException;
use CXml\Model\Credential;
use CXml\Model\CXml;
use CXml\Model\Header;
Expand All @@ -23,6 +30,87 @@

class Builder
{
// according to cXML reference document
private static array $exceptionMapping = [
CXmlAuthenticationInvalidException::class => 401,
CXmlNotAcceptableException::class => 406,
CXmlConflictException::class => 409,
CXmlPreconditionFailedException::class => 412,
CXmlExpectationFailedException::class => 417,
CXmlCredentialInvalidException::class => 417,
CXmlNotImplementedException::class => 450,
];

// TODO create enum for this?
private static array $exceptionCodeMapping = [
// cxml
450 => 'Not Implemented',

// http - shamelessly copied from Symfony\Component\HttpFoundation\Response
100 => 'Continue',
101 => 'Switching Protocols',
102 => 'Processing', // RFC2518
103 => 'Early Hints',
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
207 => 'Multi-Status', // RFC4918
208 => 'Already Reported', // RFC5842
226 => 'IM Used', // RFC3229
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
307 => 'Temporary Redirect',
308 => 'Permanent Redirect', // RFC7238
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Payload Too Large',
414 => 'URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Range Not Satisfiable',
417 => 'Expectation Failed',
418 => "I'm a teapot", // RFC2324
421 => 'Misdirected Request', // RFC7540
422 => 'Unprocessable Entity', // RFC4918
423 => 'Locked', // RFC4918
424 => 'Failed Dependency', // RFC4918
425 => 'Too Early', // RFC-ietf-httpbis-replay-04
426 => 'Upgrade Required', // RFC2817
428 => 'Precondition Required', // RFC6585
429 => 'Too Many Requests', // RFC6585
431 => 'Request Header Fields Too Large', // RFC6585
451 => 'Unavailable For Legal Reasons', // RFC7725
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported',
506 => 'Variant Also Negotiates', // RFC2295
507 => 'Insufficient Storage', // RFC4918
508 => 'Loop Detected', // RFC5842
510 => 'Not Extended', // RFC2774
511 => 'Network Authentication Required', // RFC6585
];

private readonly PayloadIdentityFactoryInterface $payloadIdentityFactory;

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

return $cXml;
}

/**
* @throws CXmlException
*/
public function buildResponseForException(CXmlException $exception): CXml
{
$statusCode = self::$exceptionMapping[$exception::class] ?? 500;
$statusText = self::$exceptionCodeMapping[$statusCode] ?? 'Unknown status';
$status = new Status($statusCode, $statusText, $exception->getMessage());

return $this
->status($status)
->build();
}
}
99 changes: 0 additions & 99 deletions src/CXml/Processor/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,7 @@

use CXml\Builder;
use CXml\Context;
use CXml\Exception\CXmlAuthenticationInvalidException;
use CXml\Exception\CXmlConflictException;
use CXml\Exception\CXmlCredentialInvalidException;
use CXml\Exception\CXmlException;
use CXml\Exception\CXmlExpectationFailedException;
use CXml\Exception\CXmlNotAcceptableException;
use CXml\Exception\CXmlNotImplementedException;
use CXml\Exception\CXmlPreconditionFailedException;
use CXml\Handler\HandlerInterface;
use CXml\Handler\HandlerRegistryInterface;
use CXml\Model\CXml;
Expand All @@ -32,87 +25,6 @@

class Processor
{
// according to cXML reference document
private static array $exceptionMapping = [
CXmlAuthenticationInvalidException::class => 401,
CXmlNotAcceptableException::class => 406,
CXmlConflictException::class => 409,
CXmlPreconditionFailedException::class => 412,
CXmlExpectationFailedException::class => 417,
CXmlCredentialInvalidException::class => 417,
CXmlNotImplementedException::class => 450,
];

// TODO create enum for this?
private static array $exceptionCodeMapping = [
// cxml
450 => 'Not Implemented',

// http - shamelessly copied from Symfony\Component\HttpFoundation\Response
100 => 'Continue',
101 => 'Switching Protocols',
102 => 'Processing', // RFC2518
103 => 'Early Hints',
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
207 => 'Multi-Status', // RFC4918
208 => 'Already Reported', // RFC5842
226 => 'IM Used', // RFC3229
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
307 => 'Temporary Redirect',
308 => 'Permanent Redirect', // RFC7238
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Payload Too Large',
414 => 'URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Range Not Satisfiable',
417 => 'Expectation Failed',
418 => "I'm a teapot", // RFC2324
421 => 'Misdirected Request', // RFC7540
422 => 'Unprocessable Entity', // RFC4918
423 => 'Locked', // RFC4918
424 => 'Failed Dependency', // RFC4918
425 => 'Too Early', // RFC-ietf-httpbis-replay-04
426 => 'Upgrade Required', // RFC2817
428 => 'Precondition Required', // RFC6585
429 => 'Too Many Requests', // RFC6585
431 => 'Request Header Fields Too Large', // RFC6585
451 => 'Unavailable For Legal Reasons', // RFC7725
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported',
506 => 'Variant Also Negotiates', // RFC2295
507 => 'Insufficient Storage', // RFC4918
508 => 'Loop Detected', // RFC5842
510 => 'Not Extended', // RFC2774
511 => 'Network Authentication Required', // RFC6585
];

public function __construct(private readonly HeaderProcessor $headerProcessor, private readonly HandlerRegistryInterface $handlerRegistry, private readonly Builder $builder, private readonly ?EventDispatcherInterface $eventDispatcher = null)
{
}
Expand Down Expand Up @@ -242,15 +154,4 @@ private function processRequest(Request $request, Context $context): CXml
->payload($response)
->build();
}

public function buildResponseForException(CXmlException $exception): CXml
{
$statusCode = self::$exceptionMapping[$exception::class] ?? 500;
$statusText = self::$exceptionCodeMapping[$statusCode] ?? 'Unknown status';
$status = new Status($statusCode, $statusText, $exception->getMessage());

return $this->builder
->status($status)
->build();
}
}

0 comments on commit 14a7930

Please sign in to comment.