Skip to content

Instantly share code, notes, and snippets.

@coreymcmahon
Created July 18, 2014 04:57
Show Gist options
  • Save coreymcmahon/ffe69cdeb3d9b2f5b759 to your computer and use it in GitHub Desktop.
Save coreymcmahon/ffe69cdeb3d9b2f5b759 to your computer and use it in GitHub Desktop.
Example of using a service layer w/ the repository pattern - www.slashnode.com
<?php
/* UserRepository.php */
namespace Acme\Repositories;
class UserRepository extends AbstractRepository implements UserRepositoryInterface
{
// etc...
}
/* UserManager.php */
namespace Acme\Services;
use Acme\Repositories\UserRepository;
use Illuminate\Events\Dispatcher as EventDispatcher;
class UserManager
{
protected $userRepository;
protected $eventDispatcher;
public function __construct(UserRepository $userRepository, EventDispatcher $eventDispatcher)
{
$this->userRepository = $userRepository;
$this->eventDispatcher = $eventDispatcher;
}
public function register($data)
{
// create the user
$user = $this->userRepository->create($data);
if (!$user) return false;
// fire a domain event before returning the user
$this->eventDispatcher->fire('user.registered', $user);
return $user;
}
/** etc... **/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment