Skip to content

Commit

Permalink
refactor: portfolio, message reply and others.
Browse files Browse the repository at this point in the history
  • Loading branch information
loduis committed Jan 5, 2025
1 parent 8d1a4d9 commit 6b1b5ef
Show file tree
Hide file tree
Showing 55 changed files with 1,561 additions and 955 deletions.
2 changes: 1 addition & 1 deletion src/Me/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use TeamWorkPm\Exception;
use TeamWorkPm\Rest\Resource;
use TeamWorkPm\Rest\Resource\DestroyTrait;
use TeamworkPm\Rest\Resource\StoreTrait;
use TeamWorkPm\Rest\Resource\StoreTrait;
use TeamWorkPm\Rest\Response\Model as Response;

class Status extends Resource
Expand Down
89 changes: 33 additions & 56 deletions src/Message/Reply.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,78 +4,55 @@

namespace TeamWorkPm\Message;

use TeamWorkPm\Exception;
use TeamWorkPm\Rest\Resource\Model;
use TeamWorkPm\Rest\Resource;
use TeamWorkPm\Rest\Resource\DestroyTrait;
use TeamWorkPm\Rest\Resource\GetTrait;
use TeamWorkPm\Rest\Response\Model as Response;
use TeamWorkPm\Rest\Resource\MarkAsReadTrait;
use TeamWorkPm\Rest\Resource\SaveTrait;
use TeamWorkPm\Rest\Resource\UpdateTrait;

class Reply extends Model
/**
* @see https://apidocs.teamwork.com/docs/teamwork/v1/message-replies/get-message-replies-id-json
*/
class Reply extends Resource
{
public function init()
{
$this->fields = [
'body' => true,
'notify' => [
'type' => 'array',
'element' => 'person',
],
];
$this->parent = 'messagereply';
$this->action = 'messageReplies';
}
use MarkAsReadTrait, UpdateTrait, SaveTrait, DestroyTrait, GetTrait;

protected ?string $parent = 'messagereply';

protected ?string $action = 'messageReplies';

protected string|array $fields = "messages.replies";

/**
* Retrieve Replies to a Message
*
* GET /messages/#{id}/replies.xml
*
* Uses the given messsage ID to retrieve a all replies to a message specified in the url.
* By default 20 records are returned at a time. You can pass "page" and "pageSize" to change this:
* eg. GET /messages/54/replies.xml?page=2&pageSize=50.
*
* The following headers are returned:
* "X-Records" - The total number of replies
* "X-Pages" - The total number of pages
* "X-Page" - The page you requested
*
* @param <type> $id
* @param array $params
*
* @return \TeamWorkPm\Response\Model
* @param int $id
* @param array|object $params
* @return Response
* @throws Exception
*/
public function getByMessage($message_id, array $params = [])
public function getByMessage(int $id, array|object $params = []): Response
{
$message_id = (int)$message_id;
if ($message_id <= 0) {
throw new Exception('Invalid param message_id');
}
$validate = ['page', 'pagesize'];
foreach ($params as $name => $value) {
if (!in_array(strtolower($name), $validate)) {
unset($params[$name]);
}
}
return $this->fetch("messages/$message_id/replies", $params);
return $this->fetch("messages/$id/replies", $params);
}

/**
* Create a Message Reply
*
* POST /messages/#{message_id}/messageReplies.xml
*
* This will create a new message.
* Also, you have the option of sending a notification to a list of people you select.people.
*
* @param array $data
*
* @param array|object $data
* @return int
* @throws Exception
*/
public function create(array $data)
public function create(array|object $data): int
{
$message_id = empty($data['message_id']) ? 0 : (int)$data['message_id'];
if ($message_id <= 0) {
throw new Exception('Required field message_id');
}
return $this->post("messages/$message_id/messageReplies", $data);
$data = arr_obj($data);
$messageId = $data->pull('message_id');

$this->validates([
'message_id' => $messageId
], true);

return $this->post("messages/$messageId/$this->action", $data);
}
}
2 changes: 1 addition & 1 deletion src/People/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use TeamWorkPm\Exception;
use TeamWorkPm\Rest\Resource;
use TeamworkPm\Rest\Resource\SaveTrait;
use TeamWorkPm\Rest\Resource\SaveTrait;
use TeamWorkPm\Rest\Response\Model as Response;

class Status extends Resource
Expand Down
55 changes: 11 additions & 44 deletions src/Portfolio/Board.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,23 @@

namespace TeamWorkPm\Portfolio;

use TeamWorkPm\Exception;
use TeamWorkPm\Factory;
use TeamWorkPm\Rest\Response\Model as Response;
use TeamWorkPm\Rest\Resource\Model;

/**
* @see https://apidocs.teamwork.com/docs/teamwork/v1/portfolio-boards/get-portfolio-boards-json
*/
class Board extends Model
{
public function init()
{
$this->parent = 'board';
$this->action = 'portfolio/boards';
protected ?string $parent = 'board';

$this->fields = [
'canEdit' => [
'type' => 'boolean'
],
'name' => [
'type' => 'string'
],
'displayOrder' => [
'type' => 'string'
],
'description' => [
'type' => 'string'
],
'deletedDate' => [
'type' => 'string'
],
'id' => [
'type' => 'string'
],
'dateCreated' => [
'type' => 'string'
],
'color' => [
'type' => 'string'
],
'deleted' => [
'type' => 'boolean'
],
];
}
protected ?string $action = 'portfolio/boards';

protected string|array $fields = "portfolio.boards";

/**
* Get all the Portfolio Boards
* GET /portfolio/boards
*
* @return \TeamWorkPm\Response\Model
* @throws Exception
*/
public function all()
public function getColumns(int $id): Response
{
return $this->fetch("$this->action");
return Factory::portfolioColumn()->getByBoard($id);
}
}
130 changes: 46 additions & 84 deletions src/Portfolio/Card.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,110 +4,72 @@

namespace TeamWorkPm\Portfolio;

use TeamWorkPm\Exception;
use TeamWorkPm\Rest\Resource\Model;

class Card extends Model
use TeamWorkPm\Rest\Resource;
use TeamWorkPm\Rest\Response\Model as Response;
use TeamWorkPm\Rest\Resource\DestroyTrait;
use TeamWorkPm\Rest\Resource\GetTrait;

/**
* @see https://apidocs.teamwork.com/docs/teamwork/v1/portfolio-boards/get-portfolio-boards-json
*/
class Card extends Resource
{
public function init()
{
$this->parent = 'card';
$this->action = 'portfolio/cards';
use DestroyTrait, GetTrait;

$this->fields = [
'projectId' => [
'type' => 'string'
],
protected ?string $parent = 'card';

// These are only used by the update method
'cardId' => [
'type' => 'string',
'sibling' => true,
],
protected ?string $action = 'portfolio/cards';

'columnId' => [
'type' => 'string',
'sibling' => true,
],

'oldColumnId' => [
'type' => 'string',
'sibling' => true,
],

'positionAfterId' => [
'type' => 'integer',
'sibling' => true,
],
];
}
protected string|array $fields = "portfolio.cards";

/**
* Get all the Columns for a Portfolio Column
* GET /portfolio/columns/{columnId}/cards
*
* @param int $columnId
* Undocumented function
*
* @return \TeamWorkPm\Response\Model
* @throws Exception
* @param integer $columnId
* @param integer $projectId
* @return integer
*/
public function getAllForColumn($columnId)
public function create(int $columnId, int $projectId): int
{
$columnId = (int)$columnId;
if ($columnId <= 0) {
throw new Exception('Invalid param columnId');
}

return $this->fetch("portfolio/columns/$columnId/cards");
$this->validates([
'column_id' => $columnId,
'project_id' => $projectId
], true);

return $this->post(
"portfolio/columns/$columnId/cards", compact('projectId')
);
}

/**
* Adds a project to the given board
* Get Cards inside a Portfolio Column
*
* @param array $data
*
* @return int
* @param integer $id
* @return Response
*/
public function create(array $data)
public function getByColumn(int $id): Response
{
$columnId = empty($data['columnId']) ? 0 : (int)$data['columnId'];
if ($columnId <= 0) {
throw new Exception('Required field columnId');
}
unset($data['columnId']);

if (empty($data['projectId'])) {
throw new Exception('Required field projectId');
}

return $this->post("portfolio/columns/$columnId/cards", $data);
return $this->fetch("portfolio/columns/$id/cards");
}

/**
* Moves the given card from one board to another
*
* @param array $data
* Undocumented function
*
* @return bool
* @throws Exception
* @param integer $id
* @param integer $oldColumnId
* @param integer $columnId
* @param integer $positionAfterId
* @return boolean
*/
public function update(array $data)
public function move(int $id, int $oldColumnId, int $columnId, int $positionAfterId): bool
{
$cardId = empty($data['id']) ? 0 : (int)$data['id'];
if ($cardId <= 0) {
throw new Exception('Required field id');
}
$data['cardId'] = $data['id'];
unset($data['id']);

if (empty($data['columnId'])) {
throw new Exception('Required field columnId');
}

if (empty($data['oldColumnId'])) {
throw new Exception('Required field oldColumnId');
}

return $this->put("$this->action/$cardId/move", $data);
return $this
->notUseFields()
->put("$this->action/$id/move", compact(
'oldColumnId',
'columnId',
'positionAfterId'
)
);
}
}
Loading

0 comments on commit 6b1b5ef

Please sign in to comment.