Skip to content

Commit

Permalink
Removed magic field (#46)
Browse files Browse the repository at this point in the history
* Removed magic field

* Fixes
  • Loading branch information
DimanKuskov authored Mar 2, 2020
1 parent e7009c5 commit aba3638
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 51 deletions.
27 changes: 3 additions & 24 deletions src/CodeGenerator/Dto/PhpParserDtoFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
final class PhpParserDtoFactory implements DtoFactory
{
private const DUPLICATE_NAME_CLASS_PREFIX = 'Property';
public const RESPONSE_CODE_VARIABLE_NAME = '_openApiResponseCode';

private BuilderFactory $factory;
private NamingStrategy $namingStrategy;
Expand Down Expand Up @@ -236,37 +235,17 @@ public function generateDtoClassGraph(SchemaBasedDtoDefinition $definition) : ar
}

$responseCode = (int) $definition->responseCode();
if ($responseCode === 0) {
$propertyDefinition = new ClassPropertyDefinition(self::RESPONSE_CODE_VARIABLE_NAME, 'int');
$propertyDefinition->makeNotNullable();
$propertyDefinition->setDescription('Response HTTP code');
$classBuilder->addStmt($this->generateClassProperty($propertyDefinition));

$constructorRequired = true;
$constructorBuilder
->addParam($this
->factory
->param('httpCode')
->setType('int'))
->addStmt(new Assign(
new Variable('this->' . self::RESPONSE_CODE_VARIABLE_NAME),
new Variable('httpCode')
));
$responseCodeStmt = new Variable('this->' . self::RESPONSE_CODE_VARIABLE_NAME);
} else {
$responseCodeStmt = $this->factory->val($responseCode);
}

$classBuilder
->addStmt(
$this
->factory
->method('_getResponseCode')
->makePublic()
->setReturnType('int')
->makeStatic()
->setReturnType('?int')
->addStmt(
new Return_(
$responseCodeStmt
$this->factory->val($responseCode !== 0 ? $responseCode : null)
)
)
);
Expand Down
28 changes: 18 additions & 10 deletions src/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use OnMoon\OpenApiServerBundle\Exception\ApiCallFailed;
use OnMoon\OpenApiServerBundle\Interfaces\ApiLoader;
use OnMoon\OpenApiServerBundle\Interfaces\Dto;
use OnMoon\OpenApiServerBundle\Interfaces\GetResponseCode;
use OnMoon\OpenApiServerBundle\Interfaces\RequestHandler;
use OnMoon\OpenApiServerBundle\Interfaces\ResponseDto;
use OnMoon\OpenApiServerBundle\Interfaces\SetClientIp;
Expand Down Expand Up @@ -79,10 +80,12 @@ public function handle(Request $request) : Response
$requestDto = $this->createRequestDto($request, $route, $operation);
$this->eventDispatcher->dispatch(new RequestDtoEvent($requestDto, $operation, $path, $method));

$responseDto = $this->executeRequestHandler($request, $route, $operation, $requestDto);
$requestHandler = $this->getRequestHandler($request, $this->getRequestHandlerInterface($route, $operation));

$responseDto = $this->executeRequestHandler($requestHandler, $operation, $requestDto);
$this->eventDispatcher->dispatch(new ResponseDtoEvent($responseDto, $operation, $path, $method));

$response = $this->createResponse($responseDto);
$response = $this->createResponse($requestHandler, $responseDto);
$this->eventDispatcher->dispatch(new ResponseEvent($response, $operation, $path, $method));

return $response;
Expand Down Expand Up @@ -123,15 +126,12 @@ private function createRequestDto(
}

private function executeRequestHandler(
Request $request,
Route $route,
RequestHandler $requestHandler,
Operation $operation,
?Dto $requestDto
) : ?ResponseDto {
$requestHandlerMethodName = $this->namingStrategy->stringToMethodName($operation->operationId);

$requestHandler = $this->getRequestHandler($request, $this->getRequestHandlerInterface($route, $operation));

/** @var ResponseDto|null $responseDto */
$responseDto = $requestDto !== null ?
$requestHandler->{$requestHandlerMethodName}($requestDto) :
Expand Down Expand Up @@ -173,18 +173,26 @@ private function getOperation(OpenApi $specification, string $path, string $meth
return $operation;
}

private function createResponse(?ResponseDto $responseDto = null) : Response
private function createResponse(RequestHandler $requestHandler, ?ResponseDto $responseDto = null) : Response
{
$response = new JsonResponse();
$response->setEncodingOptions(JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR);

$statusCode = null;

if ($responseDto instanceof ResponseDto) {
$response->setContent($this->serializer->createResponse($responseDto));
$response->setStatusCode($responseDto->_getResponseCode());
} else {
$response->setStatusCode(200);
$statusCode = $responseDto::_getResponseCode() ?? $statusCode;
}

if ($requestHandler instanceof GetResponseCode) {
$statusCode = $requestHandler->getResponseCode($statusCode) ?? $statusCode;
}

$statusCode ??= Response::HTTP_OK;

$response->setStatusCode($statusCode);

return $response;
}

Expand Down
10 changes: 10 additions & 0 deletions src/Interfaces/GetResponseCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace OnMoon\OpenApiServerBundle\Interfaces;

interface GetResponseCode
{
public function getResponseCode(?int $guessedCode) : ?int;
}
2 changes: 1 addition & 1 deletion src/Interfaces/ResponseDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ interface ResponseDto extends Dto
/**
* @internal
*/
public function _getResponseCode() : int;
public static function _getResponseCode() : ?int;
}
13 changes: 3 additions & 10 deletions src/Mapper/DtoMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use ArrayAccess;
use ArrayObject;
use BadMethodCallException;
use OnMoon\OpenApiServerBundle\CodeGenerator\Dto\PhpParserDtoFactory;
use OnMoon\OpenApiServerBundle\CodeGenerator\Factory\OperationDefinitionFactory;
use OnMoon\OpenApiServerBundle\CodeGenerator\Naming\NamingStrategy;
use OnMoon\OpenApiServerBundle\Exception\CannotMapToDto;
Expand Down Expand Up @@ -55,7 +54,7 @@ public function __construct(NamingStrategy $namingStrategy)
* @throws ReflectionException
* @throws StringsException
*/
public function map($from, string $toDTO, int $httpCode = 0, ?callable $propertyMapper = null) : object
public function map($from, string $toDTO, ?callable $propertyMapper = null) : object
{
if (! class_exists($toDTO)) {
throw CannotMapToDto::becauseRootClassDoesNotExist($toDTO);
Expand All @@ -66,12 +65,6 @@ public function map($from, string $toDTO, int $httpCode = 0, ?callable $property

$properties = $reflectionClass->getProperties();
foreach ($properties as $property) {
if ($property->getName() === PhpParserDtoFactory::RESPONSE_CODE_VARIABLE_NAME) {
$property->setAccessible(true);
$property->setValue($instance, $httpCode);
continue;
}

$nextMapper = null;
if ($propertyMapper !== null) {
/** @var mixed $mappedProperty */
Expand Down Expand Up @@ -148,7 +141,7 @@ public function map($from, string $toDTO, int $httpCode = 0, ?callable $property
);
}

$value[] = $this->map($item, $fullClass, 0, $nextMapper);
$value[] = $this->map($item, $fullClass, $nextMapper);
}
} else {
/** @var mixed $item */
Expand All @@ -171,7 +164,7 @@ public function map($from, string $toDTO, int $httpCode = 0, ?callable $property
}

/** @var mixed $value */
$value = $this->map($rawValue, $typeName, 0, $nextMapper);
$value = $this->map($rawValue, $typeName, $nextMapper);
} else {
throw CannotMapToDto::becauseUnknownType($typeName, $property->getName(), $toDTO);
}
Expand Down
7 changes: 1 addition & 6 deletions src/Serializer/ReflectionDtoSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace OnMoon\OpenApiServerBundle\Serializer;

use OnMoon\OpenApiServerBundle\CodeGenerator\Dto\PhpParserDtoFactory;
use OnMoon\OpenApiServerBundle\Interfaces\Dto;
/** phpcs:disable SlevomatCodingStandard.Namespaces.UnusedUses.UnusedUse */
use OnMoon\OpenApiServerBundle\Interfaces\RequestHandler;
Expand Down Expand Up @@ -58,11 +57,7 @@ public function createRequestDto(

public function createResponse(object $dto) : string
{
return $this->serializer->serialize(
$dto,
'json',
[AbstractNormalizer::IGNORED_ATTRIBUTES => [PhpParserDtoFactory::RESPONSE_CODE_VARIABLE_NAME]]
);
return $this->serializer->serialize($dto, 'json');
}

/**
Expand Down

0 comments on commit aba3638

Please sign in to comment.