Skip to content

Instantly share code, notes, and snippets.

@andrewshell
Last active August 29, 2015 14:02
Show Gist options
  • Save andrewshell/06beb8e764a0bf472853 to your computer and use it in GitHub Desktop.
Save andrewshell/06beb8e764a0bf472853 to your computer and use it in GitHub Desktop.
Idea on how to use specifications
<?php
class QueryLocationsAction
{
protected $repository;
// Probably need to break up repositories by "select, insert, update and delete"
// Extra bonus, we can choose to populate them with a read or write connection.
public function __construct(PdoLocationSelectRepository $repository)
{
$this->repository = $repository;
}
public function respond(Request $request)
{
$companyId = $request->get('id');
$currentPage = $request->get('page');
$itemsPerPage = $request->get('items');
// This may not be exactly the way specs will work, but it's an example of how the specifications
// are taylored to the repository.
$companyFilter = new PdoSpec\FilterByCompanyId($companyId);
$paginatedCompanyFilter = new PdoSpec\Paginate($companyFilter, $currentPage, $itemsPerPage);
// Assign the active specification.
$repository->assignSpec($paginatedCompanyFilter);
// The interactor is now simpler, it just knows how to call getLocations and getLocationCount on
// the repository and populate the QueryLocationsResponse object.
// I can't pass in the interactor as a dependency, but it lets the repository
// define how it's configured.
$interactor = new QueryLocations($repository);
$response = $interactor->run();
foreach ($response->getLocations() as $location) {
// do whatever
}
// Probably get rid of "responders"
return JsonResponse($response);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment