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

TaskFactory::createFromName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
cc 3
eloc 12
nc 3
nop 1
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