Skip to content

Commit

Permalink
Task creation and storing were separated
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Kupreev committed Jun 24, 2014
1 parent 03068ec commit 5ea7e07
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
9 changes: 9 additions & 0 deletions app/entity/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
5 changes: 4 additions & 1 deletion app/interactor/Task/Creation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
25 changes: 13 additions & 12 deletions app/mock/repository/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use app\entity as Entity;
use app\repository as Repo;
use app\request as Request;

class Task implements Repo\TaskInterface
{
Expand All @@ -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;
}
Expand Down
11 changes: 9 additions & 2 deletions app/repository/TaskInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,4 +17,11 @@ interface TaskInterface
* @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.