|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* File: TaskRepository.php - ltodo |
|
4
|
|
|
* zzz - 04/02/17 23:37 |
|
5
|
|
|
* PHP Version 7 |
|
6
|
|
|
* |
|
7
|
|
|
* @category None |
|
8
|
|
|
* @package Ltodo |
|
9
|
|
|
* @author Martin Pham <[email protected]> |
|
10
|
|
|
* @license None http:// |
|
11
|
|
|
* @link None |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Todo\Infrastructure\Persistence\Eloquent\Repository; |
|
15
|
|
|
|
|
16
|
|
|
use Illuminate\Support\Facades\DB; |
|
17
|
|
|
use Todo\Domain\Exception\TaskNotFoundException; |
|
18
|
|
|
use Todo\Domain\Repository\TaskRepositoryInterface; |
|
19
|
|
|
use Todo\Domain\Task; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class TaskRepository |
|
23
|
|
|
* |
|
24
|
|
|
* @category None |
|
25
|
|
|
* @package Todo\Infrastructure\Persistence\Eloquent\Repository |
|
26
|
|
|
* @author Martin Pham <[email protected]> |
|
27
|
|
|
* @license None http:// |
|
28
|
|
|
* @link None |
|
29
|
|
|
*/ |
|
30
|
|
|
class TaskRepository implements TaskRepositoryInterface |
|
31
|
|
|
{ |
|
32
|
|
|
private function hydrateTask(\stdClass $object) { |
|
33
|
|
|
$task = new Task(); |
|
34
|
|
|
$task->setId($object->id); |
|
35
|
|
|
$task->setName($object->name); |
|
36
|
|
|
$task->setStatus($object->status); |
|
37
|
|
|
$task->setCreatedAt(new \DateTime($object->created_at)); |
|
38
|
|
|
$task->setUpdatedAt(new \DateTime($object->updated_at)); |
|
39
|
|
|
|
|
40
|
|
|
return $task; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @inheritDoc |
|
45
|
|
|
*/ |
|
46
|
|
|
public function findAll(): array |
|
47
|
|
|
{ |
|
48
|
|
|
$return = []; |
|
49
|
|
|
$results = DB::select('SELECT * FROM tasks'); |
|
50
|
|
|
|
|
51
|
|
|
foreach($results as $taskObject) { |
|
52
|
|
|
$return[] = $this->hydrateTask($taskObject); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
return $return; |
|
57
|
|
|
}/** @noinspection PhpSignatureMismatchDuringInheritanceInspection */ |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @inheritDoc |
|
61
|
|
|
*/ |
|
62
|
|
View Code Duplication |
public function find($id): Task |
|
|
|
|
|
|
63
|
|
|
{ |
|
64
|
|
|
$results = DB::table('tasks')->where('id', $id)->get(); |
|
65
|
|
|
if (count($results) === 0) { |
|
66
|
|
|
throw new TaskNotFoundException("Cannot find task with id $id"); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $this->hydrateTask($results[0]); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @inheritDoc |
|
74
|
|
|
*/ |
|
75
|
|
|
public function findAllByStatus($status): array |
|
76
|
|
|
{ |
|
77
|
|
|
$return = []; |
|
78
|
|
|
$results = DB::table('tasks')->where('status', $status)->get(); |
|
79
|
|
|
|
|
80
|
|
|
foreach($results as $taskObject) { |
|
81
|
|
|
$return[] = $this->hydrateTask($taskObject); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
return $return; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @inheritDoc |
|
90
|
|
|
*/ |
|
91
|
|
View Code Duplication |
public function findByName(string $name): Task |
|
|
|
|
|
|
92
|
|
|
{ |
|
93
|
|
|
$results = DB::table('tasks')->where('name', $name)->get(); |
|
94
|
|
|
|
|
95
|
|
|
if (count($results) === 0) { |
|
96
|
|
|
throw new TaskNotFoundException("Cannot find task with name $name"); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $this->hydrateTask($results[0]); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @inheritDoc |
|
104
|
|
|
*/ |
|
105
|
|
|
public function save(Task $task): bool |
|
106
|
|
|
{ |
|
107
|
|
|
if ($task->getCreatedAt() === null) { |
|
108
|
|
|
$task->setCreatedAt(new \DateTime()); |
|
109
|
|
|
} |
|
110
|
|
|
$task->setUpdatedAt(new \DateTime()); |
|
111
|
|
|
|
|
112
|
|
|
if ($task->getId() === null) { |
|
113
|
|
|
DB::table('tasks')->insert([ |
|
114
|
|
|
'name' => $task->getName(), |
|
115
|
|
|
'status' => $task->getStatus(), |
|
116
|
|
|
'created_at' => $task->getCreatedAt(), |
|
117
|
|
|
'updated_at' => $task->getUpdatedAt(), |
|
118
|
|
|
]); |
|
119
|
|
|
$task->setId(DB::getPdo()->lastInsertId()); |
|
120
|
|
|
} else { |
|
121
|
|
|
DB::table('tasks')->where('id', $task->getId())->update([ |
|
122
|
|
|
'name' => $task->getName(), |
|
123
|
|
|
'status' => $task->getStatus(), |
|
124
|
|
|
'created_at' => $task->getCreatedAt(), |
|
125
|
|
|
'updated_at' => $task->getUpdatedAt(), |
|
126
|
|
|
]); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
return true; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @inheritDoc |
|
137
|
|
|
*/ |
|
138
|
|
|
public function remove(Task $task): bool |
|
139
|
|
|
{ |
|
140
|
|
|
return DB::table('tasks')->where('id', $task->getId())->delete(); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @inheritDoc |
|
145
|
|
|
*/ |
|
146
|
|
|
public function removeByStatus($status): bool |
|
147
|
|
|
{ |
|
148
|
|
|
return DB::table('tasks')->where('status', 'status')->delete(); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
} |
|
152
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.