Skip to content
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

[TASK] refactor Controller using ResponseFactory as DI injected #37

Open
calien666 opened this issue Feb 8, 2024 · 0 comments
Open

Comments

@calien666
Copy link
Member

calien666 commented Feb 8, 2024

From #35 mentioned by @sbuerk:

I would suggest to let the ResponseFactory and the StreamFactory be injected in the constructor and use the factories for creating the response by adding a helper method createResponse()

protected ResponseFactoryInterface $responseFactory;
protected StreamFactoryInterface $streamFactory;

public function __construct(
  // ...
  ResponseFactoryInterface $responseFactory,
  StreamFactoryInterface $streamFactory, 
  // ...
) {
  $this->responseFactory = $responseFactory;
  $this->streamFactory = $streamFactory;
}

protected function createResponse(string $content, int $status = 200, string $reasonPhrase = '', string contentType = 'text/html; charset=utf-8'): ResponseInterface
{
  return $this->responseFactory->createResponse()
    ->withHeader('Content-Type', $contentType)
    ->withStatus($status, $reasonPhrase)
    ->withBody($this->streamFactory->createStream($content));
}

and than replace this HtmlRespone with:

return $this->createResponse(
  'Action not allowed',
  400,
  'Action not allowed',  
);

and similar for the other HtmlResponse's

Originally posted by @sbuerk in #35 (comment)

@calien666 calien666 changed the title I would suggest to let the ResponseFactory and the StreamFactory be injected in the constructor and use the factories for creating the response by adding a helper method createResponse() [TASK] refactor Controller using ResponseFactory as DI injected Feb 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant