-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTodoItemRepository.php
61 lines (52 loc) · 1.56 KB
/
TodoItemRepository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
declare(strict_types=1);
namespace N1215\LaraTodo\Impls\EloquentAsEntity;
use Illuminate\Support\Collection;
use InvalidArgumentException;
use N1215\LaraTodo\Common\TodoItemId;
use N1215\LaraTodo\Common\TodoItemInterface;
use N1215\LaraTodo\Common\TodoItemRepositoryInterface;
use N1215\LaraTodo\Exceptions\PersistenceException;
/**
* Eloquent ModelによるTodoItemのリポジトリ実装
* @package N1215\LaraTodo\Impls\EloquentAsEntity
*/
class TodoItemRepository implements TodoItemRepositoryInterface
{
/**
* @inheritDoc
*/
public function find(TodoItemId $id): ?TodoItemInterface
{
/** @var TodoItem|null $todoItem */
$todoItem = TodoItem::query()->find($id->getValue());
return $todoItem;
}
/**
* @inheritDoc
*/
public function list(): Collection
{
return collect(TodoItem::all()->all());
}
/**
* @inheritDoc
*/
public function persist(TodoItemInterface $todoItem): TodoItemInterface
{
if (!$todoItem instanceof TodoItem) {
throw new InvalidArgumentException('このリポジトリで永続化できるエンティティは' . TodoItem::class . 'のみです');
}
if (!$todoItem->save()) {
throw new PersistenceException('Todo項目の永続化に失敗しました。title=' . $todoItem->getTitle()->getValue());
}
return $todoItem;
}
/**
* @inheritDoc
*/
public function new(array $record): TodoItemInterface
{
return (new TodoItem())->forceFill($record);
}
}