8000 [Notifier] Add Line bridge · symfony/symfony@e96524a · GitHub
[go: up one dir, main page]

Skip to content

Commit e96524a

Browse files
kurozumiOskarStark
authored andcommitted
[Notifier] Add Line bridge
1 parent 7cf5a0f commit e96524a

File tree

15 files changed

+382
-0
lines changed

15 files changed

+382
-0
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@
149149
use Symfony\Component\Notifier\Bridge\Isendpro\IsendproTransportFactory;
150150
use Symfony\Component\Notifier\Bridge\KazInfoTeh\KazInfoTehTransportFactory;
151151
use Symfony\Component\Notifier\Bridge\LightSms\LightSmsTransportFactory;
152+
use Symfony\Component\Notifier\Bridge\LineNotify\LineNotifyTransportFactory;
152153
use Symfony\Component\Notifier\Bridge\LinkedIn\LinkedInTransportFactory;
153154
use Symfony\Component\Notifier\Bridge\Mailjet\MailjetTransportFactory as MailjetNotifierTransportFactory;
154155
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
@@ -2576,6 +2577,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
25762577
IsendproTransportFactory::class => 'notifier.transport_factory.isendpro',
25772578
KazInfoTehTransportFactory::class => 'notifier.transport_factory.kaz-info-teh',
25782579
LightSmsTransportFactory::class => 'notifier.transport_factory.light-sms',
2580+
LineNotifyTransportFactory::class => 'notifier.transport_factory.line-notify',
25792581
LinkedInTransportFactory::class => 'notifier.transport_factory.linked-in',
25802582
MailjetNotifierTransportFactory::class => 'notifier.transport_factory.mailjet',
25812583
MattermostTransportFactory::class => 'notifier.transport_factory.mattermost',

src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier_transports.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use Symfony\Component\Notifier\Bridge\Isendpro\IsendproTransportFactory;
3535
use Symfony\Component\Notifier\Bridge\KazInfoTeh\KazInfoTehTransportFactory;
3636
use Symfony\Component\Notifier\Bridge\LightSms\LightSmsTransportFactory;
37+
use Symfony\Component\Notifier\Bridge\LineNotify\LineNotifyTransportFactory;
3738
use Symfony\Component\Notifier\Bridge\LinkedIn\LinkedInTransportFactory;
3839
use Symfony\Component\Notifier\Bridge\Mailjet\MailjetTransportFactory;
3940
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
@@ -317,5 +318,8 @@
317318
->parent('notifier.transport_factory.abstract')
318319
->tag('texter.transport_factory')
319320

321+
->set('notifier.transport_factory.line-notify', LineNotifyTransportFactory::class)
322+
->parent('notifier.transport_factory.abstract')
323+
->tag('chatter.transport_factory')
320324
;
321325
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/Tests export-ignore
2+
/phpunit.xml.dist export-ignore
3+
/.gitattributes export-ignore
4+
/.gitignore export-ignore
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor/
2+
composer.lock
3+
phpunit.xml
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
6.3
5+
---
6+
7+
* Add the bridge
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2022 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.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?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+
12+
namespace Symfony\Component\Notifier\Bridge\LineNotify;
13+
14+
use Symfony\Component\Notifier\Exception\TransportException;
15+
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
16+
use Symfony\Component\Notifier\Message\ChatMessage;
17+
use Symfony\Component\Notifier\Message\MessageInterface;
18+
use Symfony\Component\Notifier\Message\SentMessage;
19+
use Symfony\Component\Notifier\Transport\AbstractTransport;
20+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
21+
use Symfony\Contracts\HttpClient\HttpClientInterface;
22+
23+
/**
24+
* @author Akira Kurozumi <info@a-zumi.net>
25+
*/
26+
final class LineNotifyTransport extends AbstractTransport
27+
{
28+
protected const HOST = 'notify-api.line.me';
29+
30+
private string $token;
31+
32+
public function __construct(#[\SensitiveParameter] string $token, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
33+
{
34+
$this->token = $token;
35+
parent::__construct($client, $dispatcher);
36+
}
37+
38+
protected function doSend(MessageInterface $message): SentMessage
39+
{
40+
if (!$message instanceof ChatMessage) {
41+
throw new UnsupportedMessageTypeException(__CLASS__, ChatMessage::class, $message);
42+
}
43+
44+
$content = $message->getSubject();
45+
46+
$endpoint = sprintf('https://%s/api/notify', $this->getEndpoint());
47+
$response = $this->client->request('POST', $endpoint, [
48+
'auth_bearer' => $this->token,
49+
'query' => [
50+
'message' => $content,
51+
],
52+
]);
53+
54+
try {
55+
$statusCode = $response->getStatusCode();
56+
} catch (TransportException $e) {
57+
throw new TransportException('Could not reach the remote Line server.', $response, 0, $e);
58+
}
59+
60+
if (200 !== $statusCode) {
61+
$result = $response->toArray(false);
62+
63+
$originalContent = $message->getSubject();
64+
$errorCode = $result['status'];
65+
$errorMessage = trim($result['message']);
66+
throw new TransportException(sprintf('Unable to post the Line message: "%s" (%d: "%s").', $originalContent, $errorCode, $errorMessage), $response);
67+
}
68+
69+
return new SentMessage($message, (string) $this);
70+
}
71+
72+
public function supports(MessageInterface $message): bool
73+
{
74+
return $message instanceof ChatMessage;
75+
}
76+
77+
public function __toString(): string
78+
{
79+
return sprintf('linenotify://%s', $this->getEndpoint());
80+
}
81+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?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+
12+
namespace Symfony\Component\Notifier\Bridge\LineNotify;
13+
14+
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
15+
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
16+
use Symfony\Component\Notifier\Transport\Dsn;
17+
18+
/**
19+
* @author Akira Kurozumi <info@a-zumi.net>
20+
*/
21+
final class LineNotifyTransportFactory extends AbstractTransportFactory
22+
{
23+
private const SCHEME = 'linenotify';
24+
25+
protected function getSupportedSchemes(): array
26+
{
27+
return [self::SCHEME];
28+
}
29+
30+
public function create(Dsn $dsn): LineNotifyTransport
31+
{
32+
if (self::SCHEME !== $dsn->getScheme()) {
33+
throw new UnsupportedSchemeException($dsn, self::SCHEME, $this->getSupportedSchemes());
34+
}
35+
36+
$token = $this->getUser($dsn);
37+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
38+
$port = $dsn->getPort();
39+
40+
return (new LineNotifyTransport($token, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
41+
}
42+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
LINE Notifier
2+
=============
3+
4+
Provides [LINE Notify](https://notify-bot.line.me/) integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
LINE_NOTIFY_DSN=linenotify://TOKEN@default
11+
```
12+
13+
Resources
14+
---------
15+
16+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
17+
* [Report issues](https://github.com/symfony/symfony/issues) and
18+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
19+
in the [main Symfony repository](https://github.com/symfony/symfony)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?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+
12+
use Symfony\Component\Notifier\Bridge\LineNotify\LineNotifyTransportFactory;
13+
use Symfony\Component\Notifier\Test\TransportFactoryTestCase;
14+
15+
/**
16+
* @author Akira Kurozumi <info@a-zumi.net>
17+
*/
18+
final class LineNotifyTransportFactoryTest extends TransportFactoryTestCase
19+
{
20+
public function createFactory(): LineNotifyTransportFactory
21+
{
22+
return new LineNotifyTransportFactory();
23+
}
24+
25+
public function supportsProvider(): iterable
26+
{
27+
yield [true, 'linenotify://host'];
28+
yield [false, 'somethingElse://host'];
29+
}
30+
31+
public function createProvider(): iterable
32+
{
33+
yield [
34+
'linenotify://host.test',
35+
'linenotify://token@host.test',
36+
];
37+
}
38+
39+
public function incompleteDsnProvider(): iterable
40+
{
41+
yield 'missing token' => ['linenotify://host.test'];
42+
}
43+
44+
public function unsupportedSchemeProvider(): iterable
45+
{
46+
yield ['somethingElse://token@host'];
47+
}
48+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?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+
12+
namespace Symfony\Component\Notifier\Bridge\LineNotify\Tests;
13+
14+
use Symfony\Component\HttpClient\MockHttpClient;
15+
use Symfony\Component\Notifier\Bridge\LineNotify\LineNotifyTransport;
16+
use Symfony\Component\Notifier\Exception\LengthException;
17+
use Symfony\Component\Notifier\Exception\TransportException;
18+
use Symfony\Component\Notifier\Message\ChatMessage;
19+
use Symfony\Component\Notifier\Message\MessageInterface;
20+
use Symfony\Component\Notifier\Message\SmsMessage;
21+
use Symfony\Component\Notifier\Test\TransportTestCase;
22+
use Symfony\Contracts\HttpClient\HttpClientInterface;
23+
use Symfony\Contracts\HttpClient\ResponseInterface;
24+
25+
/**
26+
* @author Akira Kurozumi <info@a-zumi.net>
27+
*/
28+
final class LineNotifyTransportTest extends TransportTestCase
29+
{
30+
public function createTransport(HttpClientInterface $client = null): LineNotifyTransport
31+
{
32+
return (new LineNotifyTransport('testToken', $client ?? $this->createMock(HttpClientInterface::class)))->setHost('host.test');
33+
}
34+
35+
public function toStringProvider(): iterable
36+
{
37+
yield ['linenotify://host.test', $this->createTransport()];
38+
}
39+
40+
public function supportedMessagesProvider(): iterable
41+
{
42+
yield [new ChatMessage('Hello!')];
43+
}
44+
45+
public function unsupportedMessagesProvider(): iterable
46+
{
47+
yield [new SmsMessage('0611223344', 'Hello!')];
48+
yield [$this->createMock(MessageInterface::class)];
49+
}
50+
51+
public function testSendChatMessageWithMoreThan2000CharsThrowsLogicException()
52+
{
53+
$transport = $this->createTransport();
54+
55+
$this->expectException(LengthException::class);
56+
$this->expectExceptionMessage('The subject length of a Line message must not exceed 1000 characters.');
57+
58+
$transport->send(new ChatMessage(str_repeat('', 1001)));
59+
}
60+
61+
public function testSendWithErrorResponseThrows()
62+
{
63+
$response = $this->createMock(ResponseInterface::class);
64+
$response->expects($this->exactly(2))
65+
->method('getStatusCode')
< D7AE /code>66+
->willReturn(400);
67+
$response->expects($this->once())
68+
->method('getContent')
69+
->willReturn(json_encode(['message' => 'testDescription', 'code' => 'testErrorCode']));
70+
71+
$client = new MockHttpClient(static function () use ($response): ResponseInterface {
72+
return $response;
73+
});
74+
75+
$transport = $this->createTransport($client);
76+
77+
$this->expectException(TransportException::class);
78+
$this->expectExceptionMessageMatches('/testDescription.+testErrorCode/');
79+
80+
$transport->send(new ChatMessage('testMessage'));
81+
}
82+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "symfony/line-notifier",
3+
"type": "symfony-notifier-bridge",
4+
"description": "Provides LINE Notify integration for Symfony Notifier.",
5+
"keywords": ["line", "notifier", "chat"],
6+
"homepage": "https://symfony.com",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Akira Kurozumi",
11+
"email": "info@a-zumi.net"
12+
},
13+
{
14+
"name": "Symfony Community",
15+
"homepage": "https://symfony.com/contributors"
16+
}
17+
],
18+
"require": {
19+
"php": ">=8.1",
20+
"symfony/http-client": "^5.4|^6.0",
21+
"symfony/notifier": "^6.3"
22+
},
23+
"require-dev": {
24+
"symfony/event-dispatcher": "^5.4|^6.0"
25+
},
26+
"autoload": {
27+
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\LineNotify\\": "" },
28+
"exclude-from-classmap": [
29+
"/Tests/"
30+
]
31+
},
32+
"minimum-stability": "dev"
33+
}

0 commit comments

Comments
 (0)
0