Skip to content

Commit

Permalink
feat: User->getRecentTasks(); refactor: Task data as constuct option
Browse files Browse the repository at this point in the history
  • Loading branch information
dehenne committed Mar 23, 2019
1 parent 2d3a1ff commit 631516b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 10 deletions.
17 changes: 11 additions & 6 deletions src/PCSG/Makerlog/Api/Tasks/Task.php
Expand Up @@ -37,11 +37,16 @@ class Task
*
* @param integer $taskId
* @param Makerlog $Makerlog - main makerlog instance
* @param array $data - optional, data to build a task object yourself. this is only intended if data from a task already exists
*/
public function __construct($taskId, Makerlog $Makerlog)
public function __construct($taskId, Makerlog $Makerlog, $data = null)
{
$this->Makerlog = $Makerlog;
$this->taskId = $taskId;

if (is_object($data) && $data !== null) {
$this->data = $data;
}
}

//region data
Expand Down Expand Up @@ -92,7 +97,7 @@ public function refresh()
*/
public function delete()
{
$this->Makerlog->getRequest()->delete('/tasks/'.$this->taskId.'/');
$this->Makerlog->getRequest()->delete('/tasks/' . $this->taskId . '/');
}

/**
Expand Down Expand Up @@ -128,7 +133,7 @@ public function update($options = [])
);
}

$this->Makerlog->getRequest()->patch('/tasks/'.$this->taskId.'/', [
$this->Makerlog->getRequest()->patch('/tasks/' . $this->taskId . '/', [
'form_params' => $params
]);
}
Expand Down Expand Up @@ -182,7 +187,7 @@ public function undone()
*/
public function canPraise()
{
$Request = $this->Makerlog->getRequest()->get('/tasks/'.$this->taskId.'/can_praise/');
$Request = $this->Makerlog->getRequest()->get('/tasks/' . $this->taskId . '/can_praise/');
$Response = json_decode($Request->getBody());

return (bool)$Response->can_praise;
Expand All @@ -197,7 +202,7 @@ public function canPraise()
*/
public function praise($amount)
{
$this->Makerlog->getRequest()->post('/tasks/'.$this->taskId.'/praise/', [
$this->Makerlog->getRequest()->post('/tasks/' . $this->taskId . '/praise/', [
'amount' => (int)$amount
]);
}
Expand All @@ -216,7 +221,7 @@ public function praise($amount)
public function getEmbed()
{
$Request = $this->Makerlog->getRequest();
$Reply = $Request->get('/tasks/'.$this->taskId.'/embed/');
$Reply = $Request->get('/tasks/' . $this->taskId . '/embed/');

return $Reply->getBody()->getContents();
}
Expand Down
47 changes: 43 additions & 4 deletions src/PCSG/Makerlog/Api/Users/User.php
Expand Up @@ -8,6 +8,7 @@

use PCSG\Makerlog\Api\Products\Product;
use PCSG\Makerlog\Api\Projects\Project;
use PCSG\Makerlog\Api\Tasks\Task;
use PCSG\Makerlog\Exception;
use PCSG\Makerlog\Makerlog;

Expand Down Expand Up @@ -402,15 +403,24 @@ public function getEmbed()
}

/**
* Return the users products
* Return products of the user
*
* @return Project[]
* @throws Exception
* @throws Exception - if debug is true
*/
public function getProducts()
{
$Request = $this->Makerlog->getRequest();
$Reply = $Request->get('/users/' . $this->username . '/products/');
try {
$Request = $this->Makerlog->getRequest();
$Reply = $Request->get('/users/' . $this->username . '/products/');
} catch (Exception $Exception) {
if ($this->Makerlog->getOption('debug')) {
throw $Exception;
}

return [];
}

$products = json_decode($Reply->getBody());

$result = [];
Expand All @@ -422,6 +432,35 @@ public function getProducts()
return $result;
}

/**
* Return the recent tasks of the user
*
* @return Task[]
* @throws Exception - if debug is true
*/
public function getRecentTasks()
{
try {
$Request = $this->Makerlog->getRequest();
$Reply = $Request->get('/users/' . $this->username . '/recent_tasks/');
} catch (Exception $Exception) {
if ($this->Makerlog->getOption('debug')) {
throw $Exception;
}

return [];
}

$tasks = json_decode($Reply->getBody());
$result = [];

foreach ($tasks as $task) {
$result[] = new Task($task->id, $this->Makerlog, $task);
}

return $result;
}

//endregion

//region action
Expand Down

0 comments on commit 631516b

Please sign in to comment.