Last active
August 29, 2015 14:21
-
-
Save NigelGreenway/f7815e83c5cbe43908a6 to your computer and use it in GitHub Desktop.
Simple web to domain/application interface
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
final class EmployeeProfileAction | |
{ | |
private $domain; | |
private $responseHandler; | |
public function __construct( | |
EmployeeService $domain, | |
ActiveEmployeeListingResponseHandler $responseHandler | |
) { | |
$this->domain = $domain; | |
$this->responseHandler = $responseHandler; | |
} | |
public function handler( | |
$employeeID, | |
Request $request | |
) { | |
$payload = $domain->getEmployeeProfile(new EmployeeID($employeeID)); | |
return $responseHandler->handle($request, $payload); | |
} | |
} | |
// --- | |
final class EmployeeProfileResponseHandler | |
{ | |
private $response; | |
public function __construct( | |
EmployeeProfileResponse $response | |
) { | |
$this->response = $response; | |
} | |
public function handle( | |
Request $request, | |
array $payload = [] | |
) { | |
switch ($request->get('Accept')) { | |
case 'application/json': | |
return $this->response->json($payload, $request); | |
break; | |
case 'application/xml': | |
return $this->response->xml($payload, $request); | |
break; | |
default: | |
return $this->response->html($payload, $request); | |
break; | |
} | |
} | |
} | |
// --- | |
final class EmployeeProfileResponse extends AbstractResponse implements ResponseInterface | |
{ | |
public function html( | |
array $payload =[], | |
Request $request | |
) { | |
return $this | |
->template | |
->render( | |
'@path/to/my_view.twig.html', | |
[ | |
'payload' => $payload, | |
], | |
[ | |
'Content-Type' => 'text/html', | |
] | |
); | |
} | |
public function json( | |
array $payload =[], | |
Request $request | |
) { | |
return $this | |
->template | |
->render( | |
'@path/to/my_view.twig.json', | |
[ | |
'payload' => $payload, | |
], | |
[ | |
'Content-Type' => 'application/json', | |
] | |
); | |
} | |
public function xml( | |
array $payload =[], | |
Request $request | |
) { | |
return $this | |
->template | |
->render( | |
'@path/to/my_view.xml.html', | |
[ | |
'payload' => $payload, | |
], | |
[ | |
'Content-Type' => 'application/xml', | |
] | |
); | |
} | |
} | |
// --- | |
abstract class AbstractResponse | |
{ | |
private $template; | |
public function __construct( | |
TemplateAdaptor $template | |
) { | |
$this->template = $template; | |
} | |
} | |
// --- | |
interface ResponseInterface | |
{ | |
public function json(array $payload = [], Request $request); | |
public function xml(array $payload = [], Request $request); | |
public function html(array $payload = [], Request $request); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment