|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Todo\Infrastructure\Persistence\DoctrineORM\Repository; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityRepository; |
|
6
|
|
|
use Doctrine\ORM\OptimisticLockException; |
|
7
|
|
|
use Doctrine\ORM\ORMInvalidArgumentException; |
|
8
|
|
|
use Todo\Domain\Exception\TaskNotFoundException; |
|
9
|
|
|
use Todo\Domain\Repository\TaskRepositoryInterface; |
|
10
|
|
|
use Todo\Domain\Task; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class TaskRepository |
|
14
|
|
|
* |
|
15
|
|
|
* @category None |
|
16
|
|
|
* @package Infrastructure\Persistence\DoctrineORM\Repository |
|
17
|
|
|
* @author Martin Pham <[email protected]> |
|
18
|
|
|
* @license None http:// |
|
19
|
|
|
* @link None |
|
20
|
|
|
*/ |
|
21
|
|
|
class TaskRepository extends EntityRepository implements TaskRepositoryInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @inheritDoc |
|
25
|
|
|
*/ |
|
26
|
|
|
public function findAll(): array |
|
27
|
|
|
{ |
|
28
|
|
|
return parent::findAll(); |
|
29
|
|
|
}/** @noinspection PhpSignatureMismatchDuringInheritanceInspection */ |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @inheritDoc |
|
33
|
|
|
*/ |
|
34
|
|
|
public function find($id): Task |
|
35
|
|
|
{ |
|
36
|
|
|
/** @var Task $task */ |
|
37
|
|
|
$task = parent::find($id); |
|
38
|
|
|
|
|
39
|
|
|
if ($task === null) { |
|
40
|
|
|
throw new TaskNotFoundException("Cannot find task with id $id"); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return $task; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @inheritDoc |
|
48
|
|
|
*/ |
|
49
|
|
|
public function findAllByStatus($status): array |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->findBy( |
|
52
|
|
|
[ |
|
53
|
|
|
'status' => $status |
|
54
|
|
|
] |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @inheritDoc |
|
60
|
|
|
*/ |
|
61
|
|
|
public function findByName(string $name): Task |
|
62
|
|
|
{ |
|
63
|
|
|
/** @var Task $task */ |
|
64
|
|
|
$task = $this->findOneBy( |
|
65
|
|
|
[ |
|
66
|
|
|
'name' => $name |
|
67
|
|
|
] |
|
68
|
|
|
); |
|
69
|
|
|
|
|
70
|
|
|
if ($task === null) { |
|
71
|
|
|
throw new TaskNotFoundException("Cannot find task with name $name"); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $task; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @inheritDoc |
|
79
|
|
|
*/ |
|
80
|
|
View Code Duplication |
public function save(Task $task): bool |
|
|
|
|
|
|
81
|
|
|
{ |
|
82
|
|
|
try { |
|
83
|
|
|
$this->getEntityManager()->persist($task); |
|
84
|
|
|
} catch (ORMInvalidArgumentException $e) { |
|
85
|
|
|
return false; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
try { |
|
89
|
|
|
$this->getEntityManager()->flush(); |
|
90
|
|
|
} catch (OptimisticLockException $e) { |
|
91
|
|
|
return false; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return true; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @inheritDoc |
|
99
|
|
|
*/ |
|
100
|
|
View Code Duplication |
public function remove(Task $task): bool |
|
|
|
|
|
|
101
|
|
|
{ |
|
102
|
|
|
try { |
|
103
|
|
|
$this->getEntityManager()->remove($task); |
|
104
|
|
|
} catch (ORMInvalidArgumentException $e) { |
|
105
|
|
|
return false; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
try { |
|
109
|
|
|
$this->getEntityManager()->flush(); |
|
110
|
|
|
} catch (OptimisticLockException $e) { |
|
111
|
|
|
return false; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return true; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @inheritDoc |
|
119
|
|
|
*/ |
|
120
|
|
|
public function removeByStatus($status): bool |
|
121
|
|
|
{ |
|
122
|
|
|
$query = $this->getEntityManager() |
|
123
|
|
|
->createQuery('DELETE FROM Domain\Task t WHERE t.status = :status'); |
|
124
|
|
|
$query->setParameter('status', $status); |
|
125
|
|
|
$query->execute(); |
|
126
|
|
|
|
|
127
|
|
|
return true; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
} |
|
131
|
|
|
|
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.