Introduce Response for Modules to create a testable way for module responses

This commit is contained in:
Philipp 2021-11-21 20:06:36 +01:00
parent ad5b0762b0
commit 561aba18e3
No known key found for this signature in database
GPG key ID: 24A7501396EB5432
37 changed files with 309 additions and 113 deletions

75
src/Module/Response.php Normal file
View file

@ -0,0 +1,75 @@
<?php
namespace Friendica\Module;
use Friendica\Capabilities\ICanReadAndWriteToResponds;
use Friendica\Capabilities\IRespondToRequests;
use Friendica\Network\HTTPException\InternalServerErrorException;
class Response implements ICanReadAndWriteToResponds
{
/**
* @var string[][]
*/
protected $headers = [];
/**
* @var string
*/
protected $content = '';
/**
* @var string
*/
protected $type = IRespondToRequests::TYPE_CONTENT;
/**
* {@inheritDoc}
*/
public function addHeader(string $key, string $value)
{
$this->headers[$key][] = $value;
}
/**
* {@inheritDoc}
*/
public function addContent(string $content)
{
$this->content .= $content;
}
/**
* {@inheritDoc}
*/
public function getHeaders(): array
{
return $this->headers;
}
/**
* {@inheritDoc}
*/
public function getContent(): string
{
return $this->content;
}
/**
* {@inheritDoc}
*/
public function setType(string $type)
{
if (!in_array($type, IRespondToRequests::ALLOWED_TYPES)) {
throw new InternalServerErrorException('wrong type');
}
$this->type = $type;
}
/**
* {@inheritDoc}
*/
public function getTyp(): string
{
return $this->type;
}
}