16
16
use Symfony \Component \Notifier \Exception \LogicException ;
17
17
use Symfony \Component \Notifier \Exception \TransportException ;
18
18
use Symfony \Component \Notifier \Exception \UnsupportedMessageTypeException ;
19
+ use Symfony \Component \Notifier \Exception \UnsupportedOptionsException ;
19
20
use Symfony \Component \Notifier \Message \ChatMessage ;
20
21
use Symfony \Component \Notifier \Message \MessageInterface ;
21
22
use Symfony \Component \Notifier \Message \SentMessage ;
@@ -86,25 +87,26 @@ protected function doSend(MessageInterface $message): SentMessage
86
87
$ options ['formatted_body ' ] = $ message ->getSubject ();
87
88
$ options ['body ' ] = strip_tags ($ message ->getSubject ());
88
89
} else {
89
- $ content ['body ' ] = $ message ->getSubject ();
90
+ $ options ['body ' ] = $ message ->getSubject ();
90
91
}
91
92
92
93
$ uri = '/_matrix/client/v3/rooms/%s/send/%s/%s ' ;
93
94
$ response = $ this ->connect (
94
95
method: 'PUT ' ,
95
- uri: \sprintf ($ uri , $ recipientId , 'm.room.message ' , Uuid::v4 ()),
96
+ uri: \sprintf ($ uri , $ recipient , 'm.room.message ' , Uuid::v4 ()),
96
97
options: [
97
- 'json ' => $ content ,
98
+ 'json ' => $ options ,
98
99
]
99
100
);
101
+
100
102
$ success = $ response ->toArray (false );
101
103
$ sentMessage = new SentMessage ($ message , (string ) $ this );
102
104
$ sentMessage ->setMessageId ($ success ['event_id ' ]);
103
105
104
106
return $ sentMessage ;
105
107
}
106
108
107
- protected function getRoomFromAlias (
109
+ private function getRoomFromAlias (
108
110
string $ alias ,
109
111
): string {
110
112
$ uri = '/_matrix/client/v3/directory/room/%s ' ;
@@ -113,7 +115,7 @@ protected function getRoomFromAlias(
113
115
return $ response ->toArray ()['room_id ' ];
114
116
}
115
117
116
- protected function createPrivateChannel (
118
+ private function createPrivateChannel (
117
119
string $ recipientId ,
118
120
): ?array {
119
121
$ uri = '/_matrix/client/v3/createRoom ' ;
@@ -132,40 +134,39 @@ protected function createPrivateChannel(
132
134
return $ response ->toArray ();
133
135
}
134
136
135
- protected function getDirectMessageChannel (
137
+ private function getDirectMessageChannel (
136
138
string $ recipientId ,
137
139
): ?string {
138
- $ response = $ this ->getAccountData (
139
- userId: $ this ->getWhoami ()['user_id ' ],
140
- type: 'm.direct '
141
- );
140
+ $ response = $ this ->getAccountData ($ this ->getWhoami ()['user_id ' ],'m.direct ' );
142
141
if (!isset ($ response [$ recipientId ])) {
143
142
$ roomid = $ this ->createPrivateChannel ($ recipientId )['room_id ' ];
144
143
$ response [$ recipientId ] = [$ roomid ];
145
- $ this ->updateAccountData ('m.direct ' , $ response );
144
+ $ this ->updateAccountData ($ this -> getWhoami (), 'm.direct ' , $ response );
146
145
147
146
return $ roomid ;
148
147
}
149
148
150
149
return $ response [$ recipientId ][0 ];
151
150
}
152
151
153
- protected function updateAccountData (
152
+ private function updateAccountData (
153
+ string $ userId ,
154
154
string $ type ,
155
- array $ option ,
156
- ): ? array {
155
+ array $ data ,
156
+ ): void {
157
157
$ uri = '/_matrix/client/v3/user/%s/account_data/%s ' ;
158
158
$ response = $ this ->connect (
159
159
method: 'PUT ' ,
160
- uri: \sprintf ($ uri , urlencode ($ this -> getWhoami ()[ ' user_id ' ] ), $ type ),
160
+ uri: \sprintf ($ uri , urlencode ($ userId ), $ type ),
161
161
options: [
162
- 'json ' => $ option ,
162
+ 'json ' => $ data ,
163
163
]);
164
-
165
- return $ response ->toArray ();
164
+ if ($ response ->toArray ()!== []) {
165
+ throw new TransportException ('Unable to update account data. ' , $ response );
166
+ }
166
167
}
167
168
168
- protected function getAccountData (
169
+ private function getAccountData (
169
170
string $ userId ,
170
171
string $ type ,
171
172
): ?array {
@@ -211,20 +212,17 @@ protected function connect(
211
212
];
212
213
$ url = $ this ->getEndpoint (true ).$ uri ;
213
214
$ response = $ this ->client ->request ($ method , $ url , $ options );
215
+
214
216
try {
215
217
$ statusCode = $ response ->getStatusCode ();
216
218
} catch (TransportException $ e ) {
217
219
throw new TransportException ('Could not reach the Matrix server. ' , $ response , 0 , $ e );
218
220
}
219
221
220
- if (400 == $ statusCode ) {
222
+ if (in_array ( $ statusCode,[ 400 , 403 , 405 ]) ) {
221
223
$ result = $ response ->toArray (false );
222
-
223
224
throw new TransportException (\sprintf ('Error: Matrix responded with "%s (%s)" ' , $ result ['error ' ], $ result ['errcode ' ]), $ response );
224
225
}
225
- if (!$ response instanceof ResponseInterface) {
226
- throw new LogicException ('Expected response to be an instance of ResponseInterface. ' );
227
- }
228
226
229
227
return $ response ;
230
228
}
0 commit comments