for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Todo\Domain\Factory;
use Todo\Domain\Exception\TaskNameIsAlreadyExistedException;
use Todo\Domain\Exception\TaskNameIsEmptyException;
use Todo\Domain\Repository\TaskRepositoryInterface;
use Todo\Domain\Specification\TaskNameIsNotEmptySpecification;
use Todo\Domain\Specification\TaskNameIsUniqueSpecification;
use Todo\Domain\Task;
/**
* Class TaskFactory
*
* @category None
* @package Domain\Factory
* @author Martin Pham <[email protected]>
* @license None http://
* @link None
*/
class TaskFactory
{
* TaskRepository
* @var TaskRepositoryInterface
protected $taskRepository;
* TaskFactory constructor
* @param TaskRepositoryInterface $taskRepository
public function __construct(TaskRepositoryInterface $taskRepository)
$this->taskRepository = $taskRepository;
}
* Create Task From Name
* @param string $name Name
* @return Task
* @throws TaskNameIsAlreadyExistedException
* @throws TaskNameIsEmptyException
public function createFromName(string $name) : Task
$task = new Task();
$emptyNameValidator = new TaskNameIsNotEmptySpecification();
if (!$emptyNameValidator->isSatisfiedBy($name)) {
throw new TaskNameIsEmptyException("Task's name should not be empty.");
$uniqueNameValidator = new TaskNameIsUniqueSpecification(
$this->taskRepository
);
if (!$uniqueNameValidator->isSatisfiedBy($name)) {
throw new TaskNameIsAlreadyExistedException(
"Task's name $name is already existed"
$task->setName($name);
return $task;