File: /var/www/vhosts/creativefellows.nl/fvr.creativefellows.nl/src/Action/Action.php
<?php
namespace App\Action;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use App\Domain\Language\LanguageData;
use Psr\Container\ContainerInterface;
use Slim\Exception\HttpBadRequestException;
use Slim\Exception\HttpNotFoundException;
abstract class Action
{
/**
* @var PhpRenderer
*/
protected $responder;
/**
* @var Request
*/
protected $request;
/**
* @var Response
*/
protected $response;
/**
* @var Container
*/
protected $container;
/**
* @var array
*/
protected $args;
public function __construct(ContainerInterface $container, PhpRenderer $responder, LanguageData $language)
{
$this->responder = $responder;
$this->container = $container;
$this->language = $language;
}
/**
* @param Request $request
* @param Response $response
* @param array $args
* @return Response
* @throws HttpNotFoundException
* @throws HttpBadRequestException
*/
public function __invoke(Request $request, Response $response, $args): Response
{
$this->request = $request;
$this->response = $response;
$this->args = $args;
return $this->action();
}
/**
* @return Response
* @throws DomainRecordNotFoundException
* @throws HttpBadRequestException
*/
abstract protected function action(): Response;
/**
* @param ActionPayload $payload
* @return Response
*/
protected function respond($payload,$status=200): Response
{
$json = json_encode($payload, JSON_PRETTY_PRINT);
$this->response->getBody()->write($json);
return $this->response
->withHeader('Content-Type', 'application/json')
->withStatus($status);
}
}