TaskNameIsUniqueSpecification   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 2
dl 0
loc 47
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A isSatisfiedBy() 0 16 2
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