|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Todo\Domain\Specification; |
|
4
|
|
|
|
|
5
|
|
|
use Todo\Domain\Exception\TaskNotFoundException; |
|
6
|
|
|
use Todo\Domain\Repository\TaskRepositoryInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class TaskNameIsUniqueSpecification |
|
10
|
|
|
* |
|
11
|
|
|
* A specification describes that Task's name should be unique |
|
12
|
|
|
* |
|
13
|
|
|
* @category None |
|
14
|
|
|
* @package Todo\Domain\Specification |
|
15
|
|
|
* @author Martin Pham <[email protected]> |
|
16
|
|
|
* @license None http:// |
|
17
|
|
|
* @link None |
|
18
|
|
|
*/ |
|
19
|
|
|
class TaskNameIsUniqueSpecification |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* TaskRepository |
|
23
|
|
|
* |
|
24
|
|
|
* @var TaskRepositoryInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $taskRepository; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* TaskNameIsUniqueSpecification constructor |
|
30
|
|
|
* |
|
31
|
|
|
* @param TaskRepositoryInterface $taskRepository Task Repository |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct(TaskRepositoryInterface $taskRepository) |
|
34
|
|
|
{ |
|
35
|
|
|
// Inject repository, since we need to check tasks on repository |
|
36
|
|
|
$this->taskRepository = $taskRepository; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Check the Task's name is already used or not |
|
41
|
|
|
* If it's already used by a Task which is the same Task we are trying to check |
|
42
|
|
|
* then it will be fine |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $name Name |
|
45
|
|
|
* @param mixed $id ID |
|
46
|
|
|
* |
|
47
|
|
|
* @return bool |
|
48
|
|
|
*/ |
|
49
|
|
|
public function isSatisfiedBy(string $name, $id = null) |
|
50
|
|
|
{ |
|
51
|
|
|
// Find the Task with given name |
|
52
|
|
|
try { |
|
53
|
|
|
$task = $this->taskRepository->findByName($name); |
|
54
|
|
|
} catch (TaskNotFoundException $e) { |
|
55
|
|
|
return true; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
// there is task with same name |
|
59
|
|
|
// but if this task's id === given id |
|
60
|
|
|
// then it's OK |
|
61
|
|
|
|
|
62
|
|
|
/** @noinspection TypeUnsafeComparisonInspection */ |
|
63
|
|
|
return $task->getId() == $id; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|