Skip to content

Commit 6b1b5ef

Browse files
committed
refactor: portfolio, message reply and others.
1 parent 8d1a4d9 commit 6b1b5ef

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1561
-955
lines changed

src/Me/Status.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use TeamWorkPm\Exception;
88
use TeamWorkPm\Rest\Resource;
99
use TeamWorkPm\Rest\Resource\DestroyTrait;
10-
use TeamworkPm\Rest\Resource\StoreTrait;
10+
use TeamWorkPm\Rest\Resource\StoreTrait;
1111
use TeamWorkPm\Rest\Response\Model as Response;
1212

1313
class Status extends Resource

src/Message/Reply.php

Lines changed: 33 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,78 +4,55 @@
44

55
namespace TeamWorkPm\Message;
66

7-
use TeamWorkPm\Exception;
8-
use TeamWorkPm\Rest\Resource\Model;
7+
use TeamWorkPm\Rest\Resource;
8+
use TeamWorkPm\Rest\Resource\DestroyTrait;
9+
use TeamWorkPm\Rest\Resource\GetTrait;
10+
use TeamWorkPm\Rest\Response\Model as Response;
11+
use TeamWorkPm\Rest\Resource\MarkAsReadTrait;
12+
use TeamWorkPm\Rest\Resource\SaveTrait;
13+
use TeamWorkPm\Rest\Resource\UpdateTrait;
914

10-
class Reply extends Model
15+
/**
16+
* @see https://apidocs.teamwork.com/docs/teamwork/v1/message-replies/get-message-replies-id-json
17+
*/
18+
class Reply extends Resource
1119
{
12-
public function init()
13-
{
14-
$this->fields = [
15-
'body' => true,
16-
'notify' => [
17-
'type' => 'array',
18-
'element' => 'person',
19-
],
20-
];
21-
$this->parent = 'messagereply';
22-
$this->action = 'messageReplies';
23-
}
20+
use MarkAsReadTrait, UpdateTrait, SaveTrait, DestroyTrait, GetTrait;
21+
22+
protected ?string $parent = 'messagereply';
23+
24+
protected ?string $action = 'messageReplies';
25+
26+
protected string|array $fields = "messages.replies";
2427

2528
/**
2629
* Retrieve Replies to a Message
2730
*
28-
* GET /messages/#{id}/replies.xml
29-
*
30-
* Uses the given messsage ID to retrieve a all replies to a message specified in the url.
31-
* By default 20 records are returned at a time. You can pass "page" and "pageSize" to change this:
32-
* eg. GET /messages/54/replies.xml?page=2&pageSize=50.
33-
*
34-
* The following headers are returned:
35-
* "X-Records" - The total number of replies
36-
* "X-Pages" - The total number of pages
37-
* "X-Page" - The page you requested
38-
*
39-
* @param <type> $id
40-
* @param array $params
41-
*
42-
* @return \TeamWorkPm\Response\Model
31+
* @param int $id
32+
* @param array|object $params
33+
* @return Response
4334
* @throws Exception
4435
*/
45-
public function getByMessage($message_id, array $params = [])
36+
public function getByMessage(int $id, array|object $params = []): Response
4637
{
47-
$message_id = (int)$message_id;
48-
if ($message_id <= 0) {
49-
throw new Exception('Invalid param message_id');
50-
}
51-
$validate = ['page', 'pagesize'];
52-
foreach ($params as $name => $value) {
53-
if (!in_array(strtolower($name), $validate)) {
54-
unset($params[$name]);
55-
}
56-
}
57-
return $this->fetch("messages/$message_id/replies", $params);
38+
return $this->fetch("messages/$id/replies", $params);
5839
}
5940

6041
/**
6142
* Create a Message Reply
6243
*
63-
* POST /messages/#{message_id}/messageReplies.xml
64-
*
65-
* This will create a new message.
66-
* Also, you have the option of sending a notification to a list of people you select.people.
67-
*
68-
* @param array $data
69-
*
44+
* @param array|object $data
7045
* @return int
71-
* @throws Exception
7246
*/
73-
public function create(array $data)
47+
public function create(array|object $data): int
7448
{
75-
$message_id = empty($data['message_id']) ? 0 : (int)$data['message_id'];
76-
if ($message_id <= 0) {
77-
throw new Exception('Required field message_id');
78-
}
79-
return $this->post("messages/$message_id/messageReplies", $data);
49+
$data = arr_obj($data);
50+
$messageId = $data->pull('message_id');
51+
52+
$this->validates([
53+
'message_id' => $messageId
54+
], true);
55+
56+
return $this->post("messages/$messageId/$this->action", $data);
8057
}
8158
}

src/People/Status.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use TeamWorkPm\Exception;
88
use TeamWorkPm\Rest\Resource;
9-
use TeamworkPm\Rest\Resource\SaveTrait;
9+
use TeamWorkPm\Rest\Resource\SaveTrait;
1010
use TeamWorkPm\Rest\Response\Model as Response;
1111

1212
class Status extends Resource

src/Portfolio/Board.php

Lines changed: 11 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,56 +4,23 @@
44

55
namespace TeamWorkPm\Portfolio;
66

7-
use TeamWorkPm\Exception;
7+
use TeamWorkPm\Factory;
8+
use TeamWorkPm\Rest\Response\Model as Response;
89
use TeamWorkPm\Rest\Resource\Model;
910

11+
/**
12+
* @see https://apidocs.teamwork.com/docs/teamwork/v1/portfolio-boards/get-portfolio-boards-json
13+
*/
1014
class Board extends Model
1115
{
12-
public function init()
13-
{
14-
$this->parent = 'board';
15-
$this->action = 'portfolio/boards';
16+
protected ?string $parent = 'board';
1617

17-
$this->fields = [
18-
'canEdit' => [
19-
'type' => 'boolean'
20-
],
21-
'name' => [
22-
'type' => 'string'
23-
],
24-
'displayOrder' => [
25-
'type' => 'string'
26-
],
27-
'description' => [
28-
'type' => 'string'
29-
],
30-
'deletedDate' => [
31-
'type' => 'string'
32-
],
33-
'id' => [
34-
'type' => 'string'
35-
],
36-
'dateCreated' => [
37-
'type' => 'string'
38-
],
39-
'color' => [
40-
'type' => 'string'
41-
],
42-
'deleted' => [
43-
'type' => 'boolean'
44-
],
45-
];
46-
}
18+
protected ?string $action = 'portfolio/boards';
19+
20+
protected string|array $fields = "portfolio.boards";
4721

48-
/**
49-
* Get all the Portfolio Boards
50-
* GET /portfolio/boards
51-
*
52-
* @return \TeamWorkPm\Response\Model
53-
* @throws Exception
54-
*/
55-
public function all()
22+
public function getColumns(int $id): Response
5623
{
57-
return $this->fetch("$this->action");
24+
return Factory::portfolioColumn()->getByBoard($id);
5825
}
5926
}

src/Portfolio/Card.php

Lines changed: 46 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -4,110 +4,72 @@
44

55
namespace TeamWorkPm\Portfolio;
66

7-
use TeamWorkPm\Exception;
8-
use TeamWorkPm\Rest\Resource\Model;
9-
10-
class Card extends Model
7+
use TeamWorkPm\Rest\Resource;
8+
use TeamWorkPm\Rest\Response\Model as Response;
9+
use TeamWorkPm\Rest\Resource\DestroyTrait;
10+
use TeamWorkPm\Rest\Resource\GetTrait;
11+
12+
/**
13+
* @see https://apidocs.teamwork.com/docs/teamwork/v1/portfolio-boards/get-portfolio-boards-json
14+
*/
15+
class Card extends Resource
1116
{
12-
public function init()
13-
{
14-
$this->parent = 'card';
15-
$this->action = 'portfolio/cards';
17+
use DestroyTrait, GetTrait;
1618

17-
$this->fields = [
18-
'projectId' => [
19-
'type' => 'string'
20-
],
19+
protected ?string $parent = 'card';
2120

22-
// These are only used by the update method
23-
'cardId' => [
24-
'type' => 'string',
25-
'sibling' => true,
26-
],
21+
protected ?string $action = 'portfolio/cards';
2722

28-
'columnId' => [
29-
'type' => 'string',
30-
'sibling' => true,
31-
],
32-
33-
'oldColumnId' => [
34-
'type' => 'string',
35-
'sibling' => true,
36-
],
37-
38-
'positionAfterId' => [
39-
'type' => 'integer',
40-
'sibling' => true,
41-
],
42-
];
43-
}
23+
protected string|array $fields = "portfolio.cards";
4424

4525
/**
46-
* Get all the Columns for a Portfolio Column
47-
* GET /portfolio/columns/{columnId}/cards
48-
*
49-
* @param int $columnId
26+
* Undocumented function
5027
*
51-
* @return \TeamWorkPm\Response\Model
52-
* @throws Exception
28+
* @param integer $columnId
29+
* @param integer $projectId
30+
* @return integer
5331
*/
54-
public function getAllForColumn($columnId)
32+
public function create(int $columnId, int $projectId): int
5533
{
56-
$columnId = (int)$columnId;
57-
if ($columnId <= 0) {
58-
throw new Exception('Invalid param columnId');
59-
}
60-
61-
return $this->fetch("portfolio/columns/$columnId/cards");
34+
$this->validates([
35+
'column_id' => $columnId,
36+
'project_id' => $projectId
37+
], true);
38+
39+
return $this->post(
40+
"portfolio/columns/$columnId/cards", compact('projectId')
41+
);
6242
}
6343

6444
/**
65-
* Adds a project to the given board
45+
* Get Cards inside a Portfolio Column
6646
*
67-
* @param array $data
68-
*
69-
* @return int
47+
* @param integer $id
48+
* @return Response
7049
*/
71-
public function create(array $data)
50+
public function getByColumn(int $id): Response
7251
{
73-
$columnId = empty($data['columnId']) ? 0 : (int)$data['columnId'];
74-
if ($columnId <= 0) {
75-
throw new Exception('Required field columnId');
76-
}
77-
unset($data['columnId']);
78-
79-
if (empty($data['projectId'])) {
80-
throw new Exception('Required field projectId');
81-
}
82-
83-
return $this->post("portfolio/columns/$columnId/cards", $data);
52+
return $this->fetch("portfolio/columns/$id/cards");
8453
}
8554

8655
/**
87-
* Moves the given card from one board to another
88-
*
89-
* @param array $data
56+
* Undocumented function
9057
*
91-
* @return bool
92-
* @throws Exception
58+
* @param integer $id
59+
* @param integer $oldColumnId
60+
* @param integer $columnId
61+
* @param integer $positionAfterId
62+
* @return boolean
9363
*/
94-
public function update(array $data)
64+
public function move(int $id, int $oldColumnId, int $columnId, int $positionAfterId): bool
9565
{
96-
$cardId = empty($data['id']) ? 0 : (int)$data['id'];
97-
if ($cardId <= 0) {
98-
throw new Exception('Required field id');
99-
}
100-
$data['cardId'] = $data['id'];
101-
unset($data['id']);
102-
103-
if (empty($data['columnId'])) {
104-
throw new Exception('Required field columnId');
105-
}
106-
107-
if (empty($data['oldColumnId'])) {
108-
throw new Exception('Required field oldColumnId');
109-
}
110-
111-
return $this->put("$this->action/$cardId/move", $data);
66+
return $this
67+
->notUseFields()
68+
->put("$this->action/$id/move", compact(
69+
'oldColumnId',
70+
'columnId',
71+
'positionAfterId'
72+
)
73+
);
11274
}
11375
}

0 commit comments

Comments
 (0)