1616use Symfony \Component \Notifier \Exception \LogicException ;
1717use Symfony \Component \Notifier \Exception \TransportException ;
1818use Symfony \Component \Notifier \Exception \UnsupportedMessageTypeException ;
19+ use Symfony \Component \Notifier \Exception \UnsupportedOptionsException ;
1920use Symfony \Component \Notifier \Message \ChatMessage ;
2021use Symfony \Component \Notifier \Message \MessageInterface ;
2122use Symfony \Component \Notifier \Message \SentMessage ;
@@ -86,25 +87,26 @@ protected function doSend(MessageInterface $message): SentMessage
8687 $ options ['formatted_body ' ] = $ message ->getSubject ();
8788 $ options ['body ' ] = strip_tags ($ message ->getSubject ());
8889 } else {
89- $ content ['body ' ] = $ message ->getSubject ();
90+ $ options ['body ' ] = $ message ->getSubject ();
9091 }
9192
9293 $ uri = '/_matrix/client/v3/rooms/%s/send/%s/%s ' ;
9394 $ response = $ this ->connect (
9495 method: 'PUT ' ,
95- uri: \sprintf ($ uri , $ recipientId , 'm.room.message ' , Uuid::v4 ()),
96+ uri: \sprintf ($ uri , $ recipient , 'm.room.message ' , Uuid::v4 ()),
9697 options: [
97- 'json ' => $ content ,
98+ 'json ' => $ options ,
9899 ]
99100 );
101+
100102 $ success = $ response ->toArray (false );
101103 $ sentMessage = new SentMessage ($ message , (string ) $ this );
102104 $ sentMessage ->setMessageId ($ success ['event_id ' ]);
103105
104106 return $ sentMessage ;
105107 }
106108
107- protected function getRoomFromAlias (
109+ private function getRoomFromAlias (
108110 string $ alias ,
109111 ): string {
110112 $ uri = '/_matrix/client/v3/directory/room/%s ' ;
@@ -113,7 +115,7 @@ protected function getRoomFromAlias(
113115 return $ response ->toArray ()['room_id ' ];
114116 }
115117
116- protected function createPrivateChannel (
118+ private function createPrivateChannel (
117119 string $ recipientId ,
118120 ): ?array {
119121 $ uri = '/_matrix/client/v3/createRoom ' ;
@@ -132,40 +134,39 @@ protected function createPrivateChannel(
132134 return $ response ->toArray ();
133135 }
134136
135- protected function getDirectMessageChannel (
137+ private function getDirectMessageChannel (
136138 string $ recipientId ,
137139 ): ?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 ' );
142141 if (!isset ($ response [$ recipientId ])) {
143142 $ roomid = $ this ->createPrivateChannel ($ recipientId )['room_id ' ];
144143 $ response [$ recipientId ] = [$ roomid ];
145- $ this ->updateAccountData ('m.direct ' , $ response );
144+ $ this ->updateAccountData ($ this -> getWhoami (), 'm.direct ' , $ response );
146145
147146 return $ roomid ;
148147 }
149148
150149 return $ response [$ recipientId ][0 ];
151150 }
152151
153- protected function updateAccountData (
152+ private function updateAccountData (
153+ string $ userId ,
154154 string $ type ,
155- array $ option ,
156- ): ? array {
155+ array $ data ,
156+ ): void {
157157 $ uri = '/_matrix/client/v3/user/%s/account_data/%s ' ;
158158 $ response = $ this ->connect (
159159 method: 'PUT ' ,
160- uri: \sprintf ($ uri , urlencode ($ this -> getWhoami ()[ ' user_id ' ] ), $ type ),
160+ uri: \sprintf ($ uri , urlencode ($ userId ), $ type ),
161161 options: [
162- 'json ' => $ option ,
162+ 'json ' => $ data ,
163163 ]);
164-
165- return $ response ->toArray ();
164+ if ($ response ->toArray ()!== []) {
165+ throw new TransportException ('Unable to update account data. ' , $ response );
166+ }
166167 }
167168
168- protected function getAccountData (
169+ private function getAccountData (
169170 string $ userId ,
170171 string $ type ,
171172 ): ?array {
@@ -211,20 +212,17 @@ protected function connect(
211212 ];
212213 $ url = $ this ->getEndpoint (true ).$ uri ;
213214 $ response = $ this ->client ->request ($ method , $ url , $ options );
215+
214216 try {
215217 $ statusCode = $ response ->getStatusCode ();
216218 } catch (TransportException $ e ) {
217219 throw new TransportException ('Could not reach the Matrix server. ' , $ response , 0 , $ e );
218220 }
219221
220- if (400 == $ statusCode ) {
222+ if (in_array ( $ statusCode,[ 400 , 403 , 405 ]) ) {
221223 $ result = $ response ->toArray (false );
222-
223224 throw new TransportException (\sprintf ('Error: Matrix responded with "%s (%s)" ' , $ result ['error ' ], $ result ['errcode ' ]), $ response );
224225 }
225- if (!$ response instanceof ResponseInterface) {
226- throw new LogicException ('Expected response to be an instance of ResponseInterface. ' );
227- }
228226
229227 return $ response ;
230228 }
0 commit comments