8000 Add LICENSE, PHPUnit Tests · symfony/symfony@1d7a63c · GitHub
[go: up one dir, main page]

Skip to content

Commit 1d7a63c

Browse files
committed
Add LICENSE, PHPUnit Tests
Changes as fabbot.io suggested
1 parent 38bcc0b commit 1d7a63c

File tree

10 files changed

+323
-57
lines changed

10 files changed

+323
-57
lines changed

src/Symfony/Component/Notifier/Bridge/Matrix/Exception/UnsupportedRecipiantTypeException.php renamed to src/Symfony/Component/Notifier/Bridge/Matrix/Exception/UnsupportedRecipientTypeException.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
<?php
22

3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
312
namespace Symfony\Component\Notifier\Bridge\Matrix\Exception;
413

514
use Symfony\Component\Notifier\Exception\LogicException;
615

7-
class UnsupportedRecipiantTypeException extends LogicException
16+
class UnsupportedRecipientTypeException extends LogicException
817
{
918
public function __construct(string $transport, string $given)
1019
{
1120
$message = \sprintf(
12-
'The "%s" transport only supports recipiants starting with "!","@","#" ("%s" given).',
21+
'The "%s" transport only supports recipients starting with "!","@","#" ("%s" given).',
1322
$transport,
1423
$given
1524
);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2022-present Fabien Potencier
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

src/Symfony/Component/Notifier/Bridge/Matrix/MatrixOptions.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
11
<?php
22

3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
312
namespace Symfony\Component\Notifier\Bridge\Matrix;
413

14+
use JsonSerializable;
515
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
616

7-
final class MatrixOptions implements MessageOptionsInterface
17+
/**
18+
* @author Frank Schulze <frank@akiber.de>
19+
*/
20+
final class MatrixOptions implements MessageOptionsInterface, JsonSerializable
821
{
922
public function __construct(
1023
private array $options = [],
11-
){
24+
) {
1225
}
1326

1427
public function toArray(): array
@@ -25,9 +38,14 @@ public function getMsgType(): string
2538
{
2639
return $this->options['msgtype'] ?? 'm.text';
2740
}
41+
2842
public function getFormat(): ?string
2943
{
3044
return $this->options['format'] ?? null;
3145
}
3246

47+
public function jsonSerialize(): mixed
48+
{
49+
return $this->options;
50+
}
3351
}
Lines changed: 55 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,46 @@
11
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
212
namespace Symfony\Component\Notifier\Bridge\Matrix;
313

4-
use Symfony\Component\Notifier\Bridge\Matrix\Exception\UnsupportedRecipiantTypeException;
14+
use Symfony\Component\Notifier\Bridge\Matrix\Exception\UnsupportedRecipientTypeException;
515
use Symfony\Component\Notifier\Exception\LogicException;
616
use Symfony\Component\Notifier\Exception\TransportException;
717
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
818
use Symfony\Component\Notifier\Message\ChatMessage;
919
use Symfony\Component\Notifier\Message\MessageInterface;
1020
use Symfony\Component\Notifier\Message\SentMessage;
1121
use Symfony\Component\Notifier\Transport\AbstractTransport;
22+
use Symfony\Component\Uid\Uuid;
1223
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
1324
use Symfony\Contracts\HttpClient\HttpClientInterface;
14-
use Symfony\Component\Uid\Uuid;
1525
use Symfony\Contracts\HttpClient\ResponseInterface;
1626

27+
/**
28+
* @author Frank Schulze <frank@akiber.de>
29+
*/
1730
final class MatrixTransport extends AbstractTransport
1831
{
1932
public function __construct(
2033
#[\SensitiveParameter] private string $accessToken,
2134
private bool $ssl,
2235
?HttpClientInterface $client = null,
2336
?EventDispatcherInterface $dispatcher = null
24-
){
37+
) {
2538
parent::__construct($client, $dispatcher);
2639
}
2740

2841
public function __toString(): string
2942
{
30-
return \sprintf( 'matrix://%s', $this->getEndpoint(false));
43+
return \sprintf('matrix://%s', $this->getEndpoint(false));
3144
}
3245

3346
public function supports(MessageInterface $message): bool
@@ -38,41 +51,40 @@ public function supports(MessageInterface $message): bool
3851
protected function doSend(MessageInterface $message): SentMessage
3952
{
4053
if(!$message instanceof ChatMessage) {
41-
throw new UnsupportedMessageTypeException(__CLASS__, ChatMessage::class, $message);
54+
throw new UnsupportedMessageTypeException(__CLASS__, ChatMessage::class, $message);
4255
}
4356

44-
if($message->getRecipientId() === null){
57+
if(null === $message->getRecipientId()){
4558
throw new LogicException('Recipient id is required.');
4659
}
4760

48-
$recipient = match(mb_substr($message->getRecipientId(),0,1)){
61+
$recipient = match (mb_substr($message->getRecipientId(), 0, 1)){
4962
"@" => $this->getDirectMessageChannel($message->getRecipientId()),
5063
"!" => $message->getRecipientId(),
5164
"#" => $this->getRoomFromAlias($message->getRecipientId()),
52-
default => throw new UnsupportedRecipiantTypeException(__CLASS__, mb_substr($message->getRecipientId(),0,1)),
65+
default => throw new UnsupportedRecipientTypeException(__CLASS__, mb_substr($message->getRecipientId(), 0, 1)),
5366
};
67+
5468
return $this->sendMessage($recipient, $message);
5569
}
5670

5771
protected function sendMessage(
5872
string $recipientId,
5973
MessageInterface $message,
60-
): SentMessage
61-
{
74+
): SentMessage {
6275
/** @var MatrixOptions $options */
6376
$options = $message->getOptions();
6477
$uri = '/_matrix/client/v3/rooms/%s/send/%s/%s';
6578

6679
$content['msgtype'] = $options->getMsgType();
67-
if($options->getFormat() === 'org.matrix.custom.html') {
80+
if('org.matrix.custom.html' === $options->getFormat()) {
6881
$content['format'] = $options->getFormat();
6982
$content['formatted_body'] = $message->getSubject();
7083
$content['body'] = strip_tags($message->getSubject());
7184
} else {
7285
$content['body'] = $message->getSubject();
7386
}
7487

75-
7688
$response = $this->connect(
7789
method: 'PUT',
7890
uri: \sprintf($uri, $recipientId, 'm.room.message', Uuid::v4()),
@@ -83,49 +95,50 @@ protected function sendMessage(
8395
$success = $response->toArray(false);
8496
$sentMessage = new SentMessage($message, (string) $this);
8597
$sentMessage->setMessageId($success['event_id']);
98+
8699
return $sentMessage;
87100
}
88101

89102
protected function getRoomFromAlias(
90103
string $alias,
91-
): string
92-
{
104+
): string {
93105
$uri = '/_matrix/client/v3/directory/room/%s';
94106
$response = $this->connect('GET', \sprintf($uri, urlencode($alias)));
107+
95108
return $response->toArray()['room_id'];
96109
}
97110

98111
protected function createPrivateChannel(
99112
string $recipientId,
100-
): array| null
101-
{
113+
): array| null {
102114
$uri = '/_matrix/client/v3/createRoom';
103115
$invites[] = $recipientId;
104116
$response = $this->connect('POST', $uri, [
105117
'json' => [
106-
"creation_content" => [
107-
"m.federate" => false
118+
'creation_content' => [
119+
'm.federate' => false
108120
],
109-
"is_direct" => true,
110-
"preset" => "trusted_private_chat",
111-
"invite" => $invites
112-
]
121+
'is_direct' => true,
122+
'preset' => 'trusted_private_chat',
123+
'invite' => $invites
124+
],
113125
]);
126+
114127
return $response->toArray();
115128
}
116129

117130
protected function getDirectMessageChannel(
118131
string $recipientId
119-
): string| null
120-
{
132+
): string| null {
121133
$response = $this->getAccountData(
122134
userId: $this->getWhoami()['user_id'],
123-
type:'m.direct'
135+
type: 'm.direct'
124136
);
125-
if(!isset($response[$recipientId])){
137+
if (!isset($response[$recipientId])) {
126138
$roomid = $this->createPrivateChannel($recipientId)['room_id'];
127139
$response[$recipientId] = [$roomid];
128140
$this->updateAccountData('m.direct', $response);
141+
129142
return $roomid;
130143
}
131144

@@ -135,23 +148,22 @@ protected function getDirectMessageChannel(
135148
protected function updateAccountData(
136149
string $type,
137150
array $option
138-
): array | null
139-
{
151+
): ?array {
140152
$uri = '/_matrix/client/v3/user/%s/account_data/%s';
141153
$response = $this->connect(
142154
method: 'PUT',
143-
uri: \sprintf($uri,urlencode($this->getWhoami()['user_id']), $type),
155+
uri: \sprintf($uri, urlencode($this->getWhoami()['user_id']), $type),
144156
options: [
145-
'json' => $option,
157+
'json' => $option,
146158
]);
159+
147160
return $response->toArray();
148161
}
149162

150163
protected function getAccountData(
151164
string $userId,
152165
string $type,
153-
): array|null
154-
{
166+
): ?array {
155167
$uri = '/_matrix/client/v3/user/%s/account_data/%s';
156168
$response = $this->connect(
157169
method: 'GET',
@@ -160,7 +172,7 @@ protected function getAccountData(
160172
return $response->toArray();
161173
}
162174

163-
protected function getWhoami(): array|null
175+
protected function getWhoami(): ?array
164176
{
165177
$uri = '/_matrix/client/v3/account/whoami';
166178
$response = $this->connect(
@@ -172,24 +184,23 @@ protected function getWhoami(): array|null
172184
}
173185

174186
protected function getEndpoint(
175-
bool $full=false
176-
): string
177-
{
187+
bool $full = false
188+
): string {
178189
return rtrim(
179-
($full?$this->getScheme().'://':'').$this->host.($this->port ? ':'.$this->port : ''),
190+
($full ? $this->getScheme().'://' : '').$this->host.($this->port ? ':'.$this->port : ''),
180191
'/');
181192
}
193+
182194
protected function getScheme(): string
183195
{
184-
return $this->ssl? 'https': 'http';
196+
return $this->ssl ? 'https' : 'http';
185197
}
186198

187199
protected function connect(
188200
string $method,
189201
string $uri,
190202
?array $options = [],
191-
): ResponseInterface
192-
{
203+
): ResponseInterface {
193204
$options += [
194205
'auth_bearer' => $this->accessToken,
195206
];
@@ -204,17 +215,12 @@ protected function connect(
204215
if (400 == $statusCode) {
205216
$result = $response->toArray(false);
206217

207-
throw new TransportException(
208-
\sprintf(
209-
'Error: Matrix responded with "%s (%s)"',
210-
$result['error'],
211- $result['errcode']
212-
),
213-
$response);
218+
throw new TransportException(\sprintf('Error: Matrix responded with "%s (%s)"', $result['error'], $result['errcode']), $response);
214219
}
215-
if(!$response instanceof ResponseInterface) {
216-
throw new LogicException('Expected response to be an instance of ResponseInterface');
220+
if (!$response instanceof ResponseInterface) {
221+
throw new LogicException('Expected response to be an instance of ResponseInterface.');
217222
}
223+
218224
return $response;
219225
}
220226
}

src/Symfony/Component/Notifier/Bridge/Matrix/MatrixTransportFactory.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,39 @@
11
<?php
22

3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
312
namespace Symfony\Component\Notifier\Bridge\Matrix;
413

514
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
615
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
716
use Symfony\Component\Notifier\Transport\Dsn;
817

18+
/**
19+
* @author Frank Schulze <frank@akiber.de>
20+
*/
921
class MatrixTransportFactory extends AbstractTransportFactory
1022
{
1123

1224
public function create(Dsn $dsn): MatrixTransport
1325
{
1426
$scheme = $dsn->getScheme();
15-
if('matrix' !== $scheme){
27+
if ('matrix' !== $scheme){
1628
throw new UnsupportedSchemeException($dsn, 'matrix', $this->getSupportedSchemes());
1729
}
1830

1931
$token = $dsn->getRequiredOption('accessToken');
2032
$host = $dsn->getHost();
2133
$port = $dsn->getPort();
22-
$ssl = filter_var($dsn->getOption('ssl', false), FILTER_VALIDATE_BOOLEAN);
34+
$ssl = filter_var($dsn->getOption('ssl', false), \FILTER_VALIDATE_BOOLEAN);
2335

24-
return (new MatrixTransport($token, $ssl, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
36+
return (new MatrixTransport($token, $ssl, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
2537
}
2638

2739
protected function getSupportedSchemes(): array

0 commit comments

Comments
 (0)
0