Last active
April 22, 2023 11:57
-
-
Save adelf/a53ce49b22b32914879801113cf79043 to your computer and use it in GitHub Desktop.
This file contains 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 | |
/** | |
* Example of useless Repository abstraction for Eloquent. | |
* From "Architecture of complex web applications" book. | |
* https://adelf.tech/2019/architecture-of-complex-web-applications | |
*/ | |
interface PollRepository | |
{ | |
//... some other actions | |
public function save(Poll $poll); | |
public function saveOption(PollOption $pollOption); | |
} | |
class EloquentPollRepository implements PollRepository | |
{ | |
//... some other actions | |
public function save(Poll $poll) | |
{ | |
$poll->save(); | |
} | |
public function saveOption(PollOption $pollOption) | |
{ | |
$pollOption->save(); | |
} | |
} | |
class PollService | |
{ | |
/** @var \Illuminate\Database\ConnectionInterface */ | |
private $connection; | |
/** @var PollRepository */ | |
private $repository; | |
/** @var \Illuminate\Contracts\Events\Dispatcher */ | |
private $dispatcher; | |
public function __construct( | |
ConnectionInterface $connection, | |
PollRepository $repository, | |
Dispatcher $dispatcher) | |
{ | |
$this->connection = $connection; | |
$this->repository = $repository; | |
$this->dispatcher = $dispatcher; | |
} | |
public function create(PollCreateDto $dto) | |
{ | |
if(count($dto->options) < 2) { | |
throw new BusinessException( | |
"Please provide at least 2 options"); | |
} | |
$poll = new Poll(); | |
$this->connection->transaction(function() use ($dto, $poll) { | |
$poll->title = $dto->title; | |
$this->repository->save($poll); | |
foreach ($dto->options as $optionText) { | |
$pollOption = new PollOption(); | |
$pollOption->poll_id = $poll->id; | |
$pollOption->text = $optionText; | |
$this->repository->saveOption($pollOption); | |
} | |
}); | |
$this->dispatcher->dispatch(new PollCreated($poll->id)); | |
} | |
} | |
class PollServiceTest extends \PHPUnit\Framework\TestCase | |
{ | |
public function testCreatePoll() | |
{ | |
$eventFake = new EventFake( | |
$this->createMock(Dispatcher::class)); | |
$repositoryMock = $this->createMock(PollRepository::class); | |
$repositoryMock->method('save') | |
->with($this->callback(function(Poll $poll) { | |
return $poll->title == 'test title'; | |
})); | |
$repositoryMock->expects($this->at(2)) | |
->method('saveOption'); | |
$postService = new PollService( | |
new FakeConnection(), $repositoryMock, $eventFake); | |
$postService->create(new PollCreateDto( | |
'test title', | |
['option1', 'option2'])); | |
$eventFake->assertDispatched(PollCreated::class); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Eloquent Model
s already provide you benefits of aRepository
Pattern.Working with an
Eloquent Model
- you should not think where the data is stored.Using Eloquent
Query Builder
- you should not thing what database driver is used under hood.So why you need a additional abstraction level?
If you want to call methods in more "object" style instead of static one - just import your
Eloquent Model
as a dependency.Laravel will create an empty instance of that
Eloquent Model
and you will have access to allQuery Builder
methods using that object.So technically saying -
Eloquent Model
is anEntity
andRepository
at the same time.It stores a representation of a
row
, and allows you to work with thatrow
orcollection of rows
in programatic style.