Passed
Push — master ( 783d3e...cfd8c6 )
by Martin
02:33
created

TaskValidationService   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 50
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A validateName() 0 18 3
1
<?php
2
/**
3
 * File: TaskValidationService.php - todo
4
 * zzz - 04/02/17 19:12
5
 * PHP Version 7
6
 *
7
 * @category None
8
 * @package  Todo
9
 * @author   Martin Pham <[email protected]>
10
 * @license  None http://
11
 * @link     None
12
 */
13
14
namespace Todo\Domain\Service;
15
use Todo\Domain\Exception\TaskNameIsAlreadyExistedException;
16
use Todo\Domain\Exception\TaskNameIsEmptyException;
17
use Todo\Domain\Repository\TaskRepositoryInterface;
18
use Todo\Domain\Specification\TaskNameIsNotEmptySpecification;
19
use Todo\Domain\Specification\TaskNameIsUniqueSpecification;
20
21
/**
22
 * Class TaskValidationService
23
 *
24
 * @category None
25
 * @package  Todo\Domain\Service
26
 * @author   Martin Pham <[email protected]>
27
 * @license  None http://
28
 * @link     None
29
 */
30
class TaskValidationService
31
{
32
    /**
33
     * TaskRepository
34
     *
35
     * @var TaskRepositoryInterface
36
     */
37
    protected $taskRepository;
38
39
    /**
40
     * TaskValidationService constructor
41
     *
42
     * @param TaskRepositoryInterface $taskRepository
43
     *
44
     */
45
    public function __construct(TaskRepositoryInterface $taskRepository)
46
    {
47
        $this->taskRepository = $taskRepository;
48
    }
49
50
    /**
51
     * ValidateName
52
     *
53
     * @param string $name Name
54
     * @param mixed  $id   ID
55
     *
56
     * @return bool
57
     * @throws TaskNameIsEmptyException
58
     * @throws TaskNameIsAlreadyExistedException
59
     */
60
    public function validateName(string $name, $id = null): bool
61
    {
62
        $emptyNameValidator = new TaskNameIsNotEmptySpecification();
63
        if (!$emptyNameValidator->isSatisfiedBy($name)) {
64
            throw new TaskNameIsEmptyException("Task's name should not be empty.");
65
        }
66
67
        $uniqueNameValidator = new TaskNameIsUniqueSpecification(
68
            $this->taskRepository
69
        );
70
        if (!$uniqueNameValidator->isSatisfiedBy($name, $id)) {
71
            throw new TaskNameIsAlreadyExistedException(
72
                "Task's name $name is already existed"
73
            );
74
        }
75
76
        return false;
77
    }
78
79
}