Skip to content

Commit 2113ae2

Browse files
committed
refactor: post messages.
1 parent 2ffac0a commit 2113ae2

17 files changed

+545
-236
lines changed

src/Comment.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use TeamWorkPm\Exception;
88
use TeamWorkPm\Factory;
9+
use TeamWorkPm\Rest\Resource\MarkAsReadTrait;
910
use TeamWorkPm\Rest\Resource\Model;
1011
use TeamWorkPm\Rest\Response\Model as Response;
1112

@@ -22,6 +23,8 @@ class Comment extends Model
2223

2324
protected string|array $fields = 'resource_comments';
2425

26+
use MarkAsReadTrait;
27+
2528
/**
2629
* Create a comment related to a task/message/notebook etc.
2730
*
@@ -53,17 +56,6 @@ public function create(array|object $data): int
5356
);
5457
}
5558

56-
/**
57-
* Mark a Comment as Read
58-
*
59-
* @param integer $id
60-
* @return boolean
61-
*/
62-
public function markAsRead(int $id): bool
63-
{
64-
return $this->put("$this->action/$id/markread");
65-
}
66-
6759
/**
6860
* Retrieving Comments across all types
6961
*

src/Message.php

Lines changed: 58 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -4,125 +4,92 @@
44

55
namespace TeamWorkPm;
66

7+
use TeamWorkPm\Rest\Resource\ArchiveTrait;
8+
use TeamWorkPm\Rest\Resource\MarkAsReadTrait;
79
use TeamWorkPm\Rest\Resource\Model;
10+
use TeamWorkPm\Rest\Response\Model as Response;
11+
use TeamWorkPm\Rest\Resource\Project\CreateTrait;
812

13+
/**
14+
* @see https://apidocs.teamwork.com/docs/teamwork/v1/messages/get-posts-json
15+
*/
916
class Message extends Model
1017
{
11-
protected function init()
12-
{
13-
$this->fields = [
14-
'title' => true,
15-
'category_id' => [
16-
'required' => true,
17-
'type' => 'integer'
18-
],
19-
'notify' => [
20-
'type' => 'array',
21-
'element' => 'person',
22-
],
23-
'private' => [
24-
'type' => 'boolean'
25-
],
26-
'body' => true,
27-
'attachments' => false,
28-
'pending_file_attachments' => false,
29-
];
30-
$this->parent = 'post';
31-
$this->action = 'posts';
32-
}
18+
protected ?string $parent = 'post';
19+
20+
protected ?string $action = 'posts';
21+
22+
protected string|array $fields = 'messages';
23+
24+
use CreateTrait, MarkAsReadTrait;
3325

3426
/**
35-
* Retrieve Multiple Messages
36-
*
37-
* GET /projects/#{project_id}/posts.xml
38-
* For the project ID supplied, will return the 25 most recent messages
39-
*
40-
* Get archived messages
41-
*
42-
* GET /projects/#{project_id}/posts/archive.xml
43-
*
44-
* Rather than the full message, this returns a summary record for each message in the specified project.
45-
*
46-
* @param $project_id
47-
* @param bool $archive
27+
* Get all Resource on a given Project
4828
*
49-
* @return \TeamWorkPm\Response\Model
29+
* @param int $id
30+
* @param boolean $archived
31+
* @return Response
5032
* @throws Exception
5133
*/
52-
public function getByProject($project_id, $archive = false)
34+
public function getByProject(int $id, bool $archived = false): Response
5335
{
54-
$project_id = (int)$project_id;
55-
if ($project_id <= 0) {
56-
throw new Exception('Invalid param project_id');
57-
}
58-
$action = "projects/$project_id/$this->action";
59-
if ($archive) {
36+
$action = "projects/$id/$this->action";
37+
if ($archived) {
6038
$action .= '/archive';
6139
}
6240
return $this->fetch($action);
6341
}
6442

6543
/**
66-
* Retrieve Messages by Category
67-
*
68-
* GET /projects/#{project_id}/cat/#{category_id}/posts.xml
69-
*
70-
* As before, will return you the most recent 25 messages, this time limited by project and category.
71-
*
72-
* Get archived messages by category
73-
*
74-
* GET /projects/#{project_id}/cat/#{category_id}/posts/archive.xml
44+
* Retrieve Messages by Category | Get Archived Messages by Category
7545
*
76-
* As above, but returns only the posts in the given category
77-
*
78-
* @param int $project_id
79-
* @param int $category_id
80-
* @param bool $archive
81-
*
82-
* @return \TeamWorkPm\Response\Model
83-
* @throws Exception
46+
* @param integer $projectId
47+
* @param integer $categoryId
48+
* @param boolean $archived
49+
* @return Response
8450
*/
85-
public function getByProjectAndCategory($project_id, $category_id, $archive = false)
51+
public function getByProjectAndCategory(int $projectId, int $categoryId, bool $archived = false): Response
8652
{
87-
$project_id = (int)$project_id;
88-
if ($project_id <= 0) {
89-
throw new Exception('Invalid param project_id');
90-
}
91-
$category_id = (int)$category_id;
92-
if ($category_id <= 0) {
93-
throw new Exception('Invalid param category_id');
94-
}
95-
$action = "projects/$project_id/cat/$category_id/$this->action";
96-
if ($archive) {
53+
$action = "projects/$projectId/cat/$categoryId/$this->action";
54+
if ($archived) {
9755
$action .= '/archive';
9856
}
9957
return $this->fetch($action);
10058
}
10159

102-
/**
103-
* Create a message
104-
*
105-
* POST /projects/#{project_id}/posts.xml
60+
/**
61+
* Mark a Resource as archive
10662
*
107-
* This will create a new message.
108-
* Also, you have the option of sending a notification to a list of people you specify.
109-
*
110-
* @param array $data
63+
* @param int $id
64+
* @return bool
65+
* @throws Exception
66+
*/
67+
public function archive(int $id): bool
68+
{
69+
return $this->put("messages/$id/archive");
70+
}
71+
72+
/**
73+
* Mark a Resource as unarchive
11174
*
112-
* @return int
75+
* @param int $id
76+
* @return bool
11377
* @throws Exception
11478
*/
115-
public function create(array $data)
79+
public function unArchive(int $id): bool
11680
{
117-
$project_id = empty($data['project_id']) ? 0 : (int)$data['project_id'];
118-
if ($project_id <= 0) {
119-
throw new Exception('Required field project_id');
120-
}
121-
if (!empty($data['files'])) {
122-
$file = Factory::build('file');
123-
$data['pending_file_attachments'] = $file->upload($data['files']);
124-
unset($data['files']);
125-
}
126-
return $this->post("projects/$project_id/$this->action", $data);
81+
return $this->put("messages/$id/unarchive");
82+
}
83+
84+
/**
85+
* Mark a Resource as Read
86+
*
87+
* @param integer $id
88+
* @return boolean
89+
*/
90+
public function markAsRead(int $id): bool
91+
{
92+
return $this->put("messages/$id/markread");
12793
}
12894
}
95+

src/Rest/Resource/ArchiveTrait.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace TeamWorkPm\Rest\Resource;
6+
7+
trait ArchiveTrait
8+
{
9+
/**
10+
* Mark a Resource as archive
11+
*
12+
* @param int $id
13+
* @return bool
14+
* @throws Exception
15+
*/
16+
public function archive(int $id): bool
17+
{
18+
return $this->put("$this->action/$id/archive");
19+
}
20+
21+
/**
22+
* Mark a Resource as unarchive
23+
*
24+
* @param int $id
25+
* @return bool
26+
* @throws Exception
27+
*/
28+
public function unArchive(int $id): bool
29+
{
30+
return $this->put("$this->action/$id/unarchive");
31+
}
32+
}

src/Rest/Resource/MarkAsReadTrait.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace TeamWorkPm\Rest\Resource;
4+
5+
trait MarkAsReadTrait
6+
{
7+
/**
8+
* Mark a Resource as Read
9+
*
10+
* @param integer $id
11+
* @return boolean
12+
*/
13+
public function markAsRead(int $id): bool
14+
{
15+
return $this->put("$this->action/$id/markread");
16+
}
17+
}

src/Rest/Resource/Project/CreateTrait.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44

55
namespace TeamWorkPm\Rest\Resource\Project;
66

7+
use TeamWorkPm\Rest\Resource\UploadTrait;
8+
79
trait CreateTrait
810
{
11+
use UploadTrait;
12+
913
/**
1014
* Create a Resource on a Project
1115
*
@@ -22,6 +26,8 @@ public function create(array|object $data): int
2226
'project_id' => $projectId
2327
], true);
2428

29+
$this->uploadFiles($data);
30+
2531
return $this->post("projects/$projectId/$this->action", $data);
2632
}
2733
}

src/Rest/Resource/UploadTrait.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace TeamWorkPm\Rest\Resource;
6+
7+
use TeamWorkPm\Factory;
8+
use TeamWorkPm\ArrayObject;
9+
10+
trait UploadTrait
11+
{
12+
private function uploadFiles(ArrayObject $data)
13+
{
14+
$files = $data->pull('files');
15+
if ($files !== null) {
16+
$data['pending_file_attachments'] = Factory::file()
17+
->upload($files);
18+
}
19+
}
20+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"title": {
3+
"type": "string",
4+
"required": true
5+
},
6+
"body": {
7+
"type": "string",
8+
"required": true
9+
},
10+
"category_id": {
11+
"type": "string",
12+
"transform": "dash"
13+
},
14+
"notify": {
15+
"type": "string|array<integer>",
16+
"validate": [
17+
"ALL"
18+
]
19+
},
20+
"private": {
21+
"type": "string"
22+
},
23+
"attachments": {
24+
"type": "string"
25+
},
26+
"pending_file_attachments": {
27+
"type": "string",
28+
"transform": "camel"
29+
},
30+
"tags": {
31+
"type": "string"
32+
}
33+
}

0 commit comments

Comments
 (0)