Permalink
Browse files

Task creation and storing were separated

  • Loading branch information...
AlexKupreev committed Jun 24, 2014
1 parent 03068ec commit 5ea7e0744bcfb5915d3f7031a6b768cc64b8e3ce
Showing with 35 additions and 15 deletions.
  1. +9 −0 app/entity/Task.php
  2. +4 −1 app/interactor/Task/Creation.php
  3. +13 −12 app/mock/repository/Task.php
  4. +9 −2 app/repository/TaskInterface.php
View
@@ -86,4 +86,13 @@ public function getNotes()
{
return $this->notes;
}
/**
* Sets unique ID of task
* @param int $id
*/
public function setId($id)
{
$this->id = $id;
}
}
@@ -44,14 +44,17 @@ public function __construct(
public function execute(Request\Task\Creation $request, Response\Task\Creation $response)
{
$userId = $this->session->getLoggedInUserId();
$this->taskRepo->create(
$task = new Entity\Task(
null,
$userId,
$request->getTitle(),
$request->getDescription(),
$request->getNotes()
);
$this->taskRepo->add($task);
$response->setStatusOk();
}
}
@@ -4,7 +4,6 @@
use app\entity as Entity;
use app\repository as Repo;
use app\request as Request;
class Task implements Repo\TaskInterface
{
@@ -15,33 +14,35 @@ class Task implements Repo\TaskInterface
protected $root = [];
/**
* Array of occupied task IDs
* @var array
* {@inheritDoc}
*/
protected $taskIds = [];
public function create($id, $userId, $title, $description, $notes)
{
return new Entity\Task($id, $userId, $title, $description, $notes);
}
/**
* {@inheritDoc}
*/
public function create($id, $userId, $title, $description, $notes)
public function add(Entity\Task $task)
{
if (empty($id) or ! $this->isFreeId($id)) {
$id = $this->getUniqId();
if ( ! $task->getId() or ! $this->isFreeId($task->getId())) {
$task->setId($this->getUniqId());
}
$task = new Entity\Task($id, $userId, $title, $description, $notes);
$this->taskIds[] = $task->getId();
return $task;
$this->root[$task->getId()] = $task;
return true;
}
protected function isFreeId($id)
{
return array_key_exists($id, $this->taskIds);
return array_key_exists($id, $this->root);
}
protected function getUniqId()
{
$keys = array_keys($this->taskIds);
$keys = array_keys($this->root);
if (empty($keys)) {
return 1;
}
@@ -2,12 +2,12 @@
namespace app\repository;
use app\request as Request;
use app\entity as Entity;
interface TaskInterface
{
/**
* Creates a task from a data array
* Creates a task object from a data array
* @param int $id
* @param int $userId
* @param string $title
@@ -17,4 +17,11 @@
* @return Entity\Task
*/
public function create($id, $userId, $title, $description, $notes);
/**
* Adds a task to the storage
* @param Entity\Task $task
*
*/
public function add(Entity\Task $task);
}

0 comments on commit 5ea7e07

Please sign in to comment.