Skip to content

Instantly share code, notes, and snippets.

@hgraca
Created August 22, 2018 15:27
Show Gist options
  • Save hgraca/96e9f0b3d22626c50a94ade772e62654 to your computer and use it in GitHub Desktop.
Save hgraca/96e9f0b3d22626c50a94ade772e62654 to your computer and use it in GitHub Desktop.
ADR - Action-Domain-Responder
<?php
// Source: https://github.com/pmjones/adr-example/blob/master/src/Web/Blog/Add/BlogAddAction.php
namespace Pmjones\Adr\Web\Blog\Add;
use Pmjones\Adr\Domain\Blog\BlogService;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
class BlogAddAction
{
protected $domain;
protected $responder;
public function __construct(
BlogService $domain,
BlogAddResponder $responder
) {
$this->domain = $domain;
$this->responder = $responder;
}
public function __invoke(Request $request) : Response
{
$payload = $this->domain->newPost();
return $this->responder->__invoke($request, $payload);
}
}
<?php
// Source: https://github.com/pmjones/adr-example/blob/master/src/Web/Blog/Add/BlogAddResponder.php
namespace Pmjones\Adr\Web\Blog\Add;
use Pmjones\Adr\Web\Blog\BlogResponder;
class BlogAddResponder extends BlogResponder
{
protected function new() : void
{
$this->renderTemplate('add');
}
}
<?php
// Source: https://github.com/pmjones/adr-example/blob/master/src/Domain/Blog/BlogService.php
namespace Pmjones\Adr\Domain\Blog;
use Pmjones\Adr\DataSource\Blog\BlogMapper;
use Pmjones\Adr\Domain\ApplicationService;
use Pmjones\Adr\Domain\Payload;
use Exception;
class BlogService extends ApplicationService
{
protected $mapper;
protected $filter;
public function __construct(
BlogMapper $mapper,
BlogFilter $filter
) {
$this->mapper = $mapper;
$this->filter = $filter;
}
protected function fetchPage(int $page = 1, int $paging = 10) : Payload
{
$blogs = $this->mapper->selectAllByPage($page, $paging);
return $this->newPayload(Payload::STATUS_FOUND, [
'blogs' => $blogs,
]);
}
protected function fetchPost(int $id) : Payload
{
// ...
}
protected function newPost(array $data = []) : Payload
{
// ...
}
protected function addPost(array $data) : Payload
{
// ...
}
protected function editPost(int $id, array $data) : Payload
{
// ...
}
protected function deletePost(int $id) : Payload
{
// ...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment