Test Failed
Push — master ( faf208...51c6fd )
by Martin
14:25
created

TaskFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

2 Methods

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