TaskValidationService   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A validateName() 0 20 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
 * Validates Task object to make sure we have valid Task before working
25
 *
26
 * @category None
27
 * @package  Todo\Domain\Service
28
 * @author   Martin Pham <[email protected]>
29
 * @license  None http://
30
 * @link     None
31
 */
32
class TaskValidationService
33
{
34
    /**
35
     * TaskRepository
36
     *
37
     * @var TaskRepositoryInterface
38
     */
39
    protected $taskRepository;
40
41
    /**
42
     * TaskValidationService constructor
43
     *
44
     * @param TaskRepositoryInterface $taskRepository
45
     *
46
     */
47
    public function __construct(TaskRepositoryInterface $taskRepository)
48
    {
49
        // Inject Repository object
50
        $this->taskRepository = $taskRepository;
51
    }
52
53
    /**
54
     * Validate a Task object by name
55
     *
56
     * @param string $name Name
57
     * @param mixed  $id   ID
58
     *
59
     * @return bool
60
     * @throws TaskNameIsEmptyException
61
     * @throws TaskNameIsAlreadyExistedException
62
     */
63
    public function validateName(string $name, $id = null): bool
64
    {
65
        // Task's name should not be empty
66
        $emptyNameValidator = new TaskNameIsNotEmptySpecification();
67
        if (!$emptyNameValidator->isSatisfiedBy($name)) {
68
            throw new TaskNameIsEmptyException("Task's name should not be empty.");
69
        }
70
71
        // Task's name should be unique
72
        $uniqueNameValidator = new TaskNameIsUniqueSpecification(
73
            $this->taskRepository
74
        );
75
        if (!$uniqueNameValidator->isSatisfiedBy($name, $id)) {
76
            throw new TaskNameIsAlreadyExistedException(
77
                "Task's name $name is already existed"
78
            );
79
        }
80
81
        return true;
82
    }
83
84
}