Skip to content

Refactoring #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php-version: ['8.1', '8.2', '8.3']
php-version: ['8.1', '8.2', '8.3', '8.4']

steps:
- uses: actions/checkout@v1
Expand Down
42 changes: 38 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
{
"name": "phauthentic/error-response",
"name": "phauthentic/problem-details",
"type": "library",
"require-dev": {
"infection/infection": "^0.29.10",
"nyholm/psr7": "^1.8",
"phpmd/phpmd": "^2.15",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^9.5.0",
"squizlabs/php_codesniffer": "^3.9"
},
"license": "MIT",
"autoload": {
"psr-4": {
"Phauthentic\\ErrorResponse\\": "src/"
"Phauthentic\\ProblemDetails\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Phauthentic\\ErrorResponse\\Tests\\": "tests/"
"Phauthentic\\ProblemDetails\\Tests\\": "tests/"
}
},
"authors": [
Expand All @@ -31,6 +33,38 @@
},
"config": {
"sort-packages": true,
"bin-dir": "bin"
"bin-dir": "bin",
"allow-plugins": {
"infection/extension-installer": true
}
},
"scripts": {
"test": [
"phpunit"
],
"infection": [
"infection"
],
"test-coverage": [
"phpunit --coverage-text"
],
"test-coverage-html": [
"phpunit --coverage-html tmp/coverage/"
],
"cscheck": [
"phpcs src/ tests/ -s"
],
"csfix": [
"phpcbf src/ tests/"
],
"analyze": [
"phpstan analyse src/"
],
"analyse": [
"phpstan analyse src/"
],
"phpmd": [
"bin/phpmd ./src/ text phpmd.xml"
]
}
}
14 changes: 14 additions & 0 deletions infection.json5
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "vendor/infection/infection/resources/schema.json",
"source": {
"directories": [
"src"
]
},
"logs": {
"text": "."
},
"mutators": {
"@default": true
}
}
21 changes: 21 additions & 0 deletions phpmd.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0"?>
<ruleset name="My first PHPMD rule set"
xmlns="http://pmd.sf.net/ruleset/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0
http://pmd.sf.net/ruleset_xml_schema.xsd"
xsi:noNamespaceSchemaLocation="
http://pmd.sf.net/ruleset_xml_schema.xsd">
<description>
Phauthentic PHPMD rule set
</description>

<!-- Import the entire unused code rule set -->
<rule ref="rulesets/cleancode.xml" />
<rule ref="rulesets/unusedcode.xml" />
<rule ref="rulesets/naming.xml">
<exclude name="LongVariable" />
</rule>
<rule ref="rulesets/codesize.xml" />

</ruleset>
1 change: 0 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ parameters:
level: 8
paths:
- src
checkGenericClassInNonGenericObjectType: false
2 changes: 1 addition & 1 deletion src/ErrorResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Phauthentic\ErrorResponse;
namespace Phauthentic\ProblemDetails;

use InvalidArgumentException;

Expand Down
2 changes: 1 addition & 1 deletion src/ErrorResponseExceptionBasedFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Phauthentic\ErrorResponse;
namespace Phauthentic\ProblemDetails;

use Exception;

Expand Down
2 changes: 1 addition & 1 deletion src/ErrorResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Phauthentic\ErrorResponse;
namespace Phauthentic\ProblemDetails;

use Exception;

Expand Down
2 changes: 1 addition & 1 deletion src/ErrorResponseFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Phauthentic\ErrorResponse;
namespace Phauthentic\ProblemDetails;

use Exception;

Expand Down
2 changes: 1 addition & 1 deletion src/ErrorResponseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Phauthentic\ErrorResponse;
namespace Phauthentic\ProblemDetails;

/**
* ErrorResponseInterface
Expand Down
41 changes: 36 additions & 5 deletions src/ErrorResponseMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,51 @@

declare(strict_types=1);

namespace Phauthentic\ErrorResponse;
namespace Phauthentic\ProblemDetails;

use Exception;
use JsonException;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

/**
*
*/
class ErrorResponseMiddleware implements MiddlewareInterface
{
/**
* @param ResponseFactoryInterface $responseFactory
* @param ErrorResponseExceptionBasedFactoryInterface $errorResponseFactory
* @param array<int, string> $exceptionClasses
* @return void
* @param bool $onlyOnJsonRequests
*/
public function __construct(
protected ResponseFactoryInterface $responseFactory,
protected ErrorResponseExceptionBasedFactoryInterface $errorResponseFactory,
protected array $exceptionClasses = [Exception::class]
protected array $exceptionClasses = [Exception::class],
protected bool $onlyOnJsonRequests = true
) {
}

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
/**
* {@inheritDoc}
*
* @param ServerRequestInterface $request
* @param RequestHandlerInterface $handler
* @return ResponseInterface
* @throws Exception
*/
public function process(
ServerRequestInterface $request,
RequestHandlerInterface $handler
): ResponseInterface {
if (!$this->isJsonRequest($request)) {
return $handler->handle($request);
}

try {
return $handler->handle($request);
} catch (Exception $exception) {
Expand All @@ -41,6 +60,12 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
}
}

protected function isJsonRequest(ServerRequestInterface $request): bool
{
return $this->onlyOnJsonRequests
&& in_array('application/json', $request->getHeader('Accept'), true);
}

protected function isAnInterceptableException(Exception $exception): bool
{
foreach ($this->exceptionClasses as $class) {
Expand All @@ -52,6 +77,11 @@ protected function isAnInterceptableException(Exception $exception): bool
return false;
}

/**
* @param ErrorResponseInterface $errorResponse
* @return string
* @throws JsonException
*/
protected function errorResponseToJson(ErrorResponseInterface $errorResponse): string
{
return json_encode($errorResponse->toArray(), JSON_THROW_ON_ERROR);
Expand All @@ -60,6 +90,7 @@ protected function errorResponseToJson(ErrorResponseInterface $errorResponse): s
public function createResponse(ErrorResponseInterface $errorResponse): ResponseInterface
{
$response = $this->responseFactory->createResponse($errorResponse->getStatus());

$body = $response->getBody();
$body->write($this->errorResponseToJson($errorResponse));

Expand Down
2 changes: 1 addition & 1 deletion src/MultiErrorCollectionResponseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Phauthentic\ErrorResponse;
namespace Phauthentic\ProblemDetails;

use InvalidArgumentException;

Expand Down
2 changes: 1 addition & 1 deletion tests/CustomException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Phauthentic\ErrorResponse\Tests;
namespace Phauthentic\ProblemDetails\Tests;

use Exception;

Expand Down
8 changes: 4 additions & 4 deletions tests/ErrorResponseFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

declare(strict_types=1);

namespace Phauthentic\ErrorResponse\Tests;
namespace Phauthentic\ProblemDetails\Tests;

use Exception;
use Phauthentic\ErrorResponse\ErrorResponse;
use Phauthentic\ErrorResponse\ErrorResponseFactory;
use Phauthentic\ErrorResponse\ErrorResponseInterface;
use Phauthentic\ProblemDetails\ErrorResponse;
use Phauthentic\ProblemDetails\ErrorResponseFactory;
use Phauthentic\ProblemDetails\ErrorResponseInterface;
use PHPUnit\Framework\TestCase;

class ErrorResponseFactoryTest extends TestCase
Expand Down
9 changes: 5 additions & 4 deletions tests/ErrorResponseMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

declare(strict_types=1);

namespace Phauthentic\ErrorResponse\Tests;
namespace Phauthentic\ProblemDetails\Tests;

use Exception;
use Nyholm\Psr7\Factory\Psr17Factory;
use Phauthentic\ErrorResponse\ErrorResponseFactory;
use Phauthentic\ErrorResponse\ErrorResponseMiddleware;
use Phauthentic\ProblemDetails\ErrorResponseFactory;
use Phauthentic\ProblemDetails\ErrorResponseMiddleware;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

class ErrorResponseMiddlewareTest extends TestCase
{
public function testProcessErrorResponse(): void
public function testThatExceptionInHandlerWillReturnErrorResponse(): void
{
$middleware = new ErrorResponseMiddleware(
new Psr17Factory(),
Expand All @@ -38,6 +38,7 @@ public function testProcessErrorResponse(): void
(string)$response->getBody()
);
$this->assertSame(500, $response->getStatusCode());
$this->assertSame('application/problem+json', $response->getHeaderLine('Content-Type'));
}

public function testProcessHandlesUnhandledException(): void
Expand Down
4 changes: 2 additions & 2 deletions tests/ErrorResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

declare(strict_types=1);

namespace Phauthentic\ErrorResponse\Tests;
namespace Phauthentic\ProblemDetails\Tests;

use InvalidArgumentException;
use Phauthentic\ErrorResponse\ErrorResponse;
use Phauthentic\ProblemDetails\ErrorResponse;
use PHPUnit\Framework\TestCase;

class ErrorResponseTest extends TestCase
Expand Down
Loading