1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Polidog\Chatwork\Api; |
6
|
|
|
|
7
|
|
|
use Polidog\Chatwork\Client\ClientInterface; |
8
|
|
|
use Polidog\Chatwork\Entity\Collection\CollectionInterface; |
9
|
|
|
use Polidog\Chatwork\Entity\Collection\MemberCollection; |
10
|
|
|
use Polidog\Chatwork\Entity\Factory\FileFactory; |
11
|
|
|
use Polidog\Chatwork\Entity\Factory\MemberFactory; |
12
|
|
|
use Polidog\Chatwork\Entity\Factory\MessageFactory; |
13
|
|
|
use Polidog\Chatwork\Entity\Factory\RoomFactory; |
14
|
|
|
use Polidog\Chatwork\Entity\Factory\TaskFactory; |
15
|
|
|
use Polidog\Chatwork\Entity\Room; |
16
|
|
|
use Polidog\Chatwork\Exception\InvalidArgumentException; |
17
|
|
|
|
18
|
|
|
class Rooms |
19
|
|
|
{ |
20
|
|
|
const ACTION_TYPE_LEAVE = 'leave'; |
21
|
|
|
const ACTION_TYPE_DELETE = 'delete'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var ClientInterface |
25
|
|
|
*/ |
26
|
|
|
private $client; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var RoomFactory |
30
|
|
|
*/ |
31
|
|
|
private $factory; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Rooms constructor. |
35
|
|
|
* |
36
|
|
|
* @param ClientInterface $client |
37
|
|
|
* @param RoomFactory $factory |
38
|
|
|
*/ |
39
|
|
|
public function __construct(ClientInterface $client, RoomFactory $factory) |
40
|
|
|
{ |
41
|
|
|
$this->client = $client; |
42
|
|
|
$this->factory = $factory; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* 自分のチャット一覧の取得. |
47
|
|
|
* |
48
|
|
|
* @return CollectionInterface |
49
|
|
|
*/ |
50
|
|
|
public function show() |
51
|
|
|
{ |
52
|
|
|
return $this->factory->collection( |
53
|
|
|
$this->client->get('rooms') |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param $id |
59
|
|
|
* |
60
|
|
|
* @return Room |
61
|
|
|
*/ |
62
|
|
|
public function detail($id) |
63
|
|
|
{ |
64
|
|
|
return $this->factory->entity( |
65
|
|
|
$this->client->get("rooms/{$id}") |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* グループチャットを新規作成. |
71
|
|
|
* |
72
|
|
|
* @param Room $room |
73
|
|
|
* @param MemberCollection $members |
74
|
|
|
* |
75
|
|
|
* @return Room |
76
|
|
|
*/ |
77
|
|
|
public function create(Room $room, MemberCollection $members) |
78
|
|
|
{ |
79
|
|
|
$result = $this->client->post('rooms', [ |
80
|
|
|
'name' => $room->name, |
81
|
|
|
'description' => $room->description, |
82
|
|
|
'members_admin_ids' => implode(',', $members->getAdminIds()), |
83
|
|
|
'members_member_ids' => implode(',', $members->getMemberIds()), |
84
|
|
|
'members_readonly_ids' => implode(',', $members->getReadonlyIds()), |
85
|
|
|
]); |
86
|
|
|
$room->roomId = $result['room_id']; |
87
|
|
|
|
88
|
|
|
return $room; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* チャットの名前、アイコンをアップデート. |
93
|
|
|
* |
94
|
|
|
* @param Room $room |
95
|
|
|
*/ |
96
|
|
|
public function update(Room $room) |
97
|
|
|
{ |
98
|
|
|
$this->client->put( |
99
|
|
|
"rooms/{$room->roomId}", |
100
|
|
|
[$room->toArray()] |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* グループチャットを退席/削除する. |
106
|
|
|
* |
107
|
|
|
* @param Room $room |
108
|
|
|
* @param string $actionType leave or delete |
109
|
|
|
* |
110
|
|
|
* @throws InvalidArgumentException |
111
|
|
|
*/ |
112
|
|
|
public function remove(Room $room, $actionType) |
113
|
|
|
{ |
114
|
|
|
if ('leave' !== $actionType && 'delete' !== $actionType) { |
115
|
|
|
throw new InvalidArgumentException('ActionType is only leave or delete'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$this->client->delete( |
119
|
|
|
"rooms/{$room->roomId}", |
120
|
|
|
[ |
121
|
|
|
'action_type' => $actionType, |
122
|
|
|
] |
123
|
|
|
); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param $roomId |
128
|
|
|
* |
129
|
|
|
* @return Rooms\Members |
130
|
|
|
*/ |
131
|
|
|
public function members(int $roomId) |
132
|
|
|
{ |
133
|
|
|
return new Rooms\Members($this->client, new MemberFactory(), $roomId); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param $roomId |
138
|
|
|
* |
139
|
|
|
* @return Rooms\Messages |
140
|
|
|
*/ |
141
|
|
|
public function messages(int $roomId) |
142
|
|
|
{ |
143
|
|
|
return new Rooms\Messages($this->client, new MessageFactory(), $roomId); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param $roomId |
148
|
|
|
* |
149
|
|
|
* @return Rooms\Tasks |
150
|
|
|
*/ |
151
|
|
|
public function tasks(int $roomId) |
152
|
|
|
{ |
153
|
|
|
return new Rooms\Tasks($this->client, new TaskFactory(), $roomId); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param int $roomId |
158
|
|
|
* |
159
|
|
|
* @return Rooms\Files |
160
|
|
|
*/ |
161
|
|
|
public function files(int $roomId) |
162
|
|
|
{ |
163
|
|
|
return new Rooms\Files($this->client, new FileFactory(), $roomId); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|