-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Add Engagespot bridge #44303
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add Engagespot bridge #44303
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
src/Symfony/Component/Notifier/Bridge/Engagespot/.gitattributes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/Tests export-ignore | ||
/phpunit.xml.dist export-ignore | ||
/.gitattributes export-ignore | ||
/.gitignore export-ignore |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
vendor/ | ||
composer.lock | ||
phpunit.xml |
7 changes: 7 additions & 0 deletions
7
src/Symfony/Component/Notifier/Bridge/Engagespot/CHANGELOG.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
CHANGELOG | ||
========= | ||
|
||
6.1 | ||
--- | ||
|
||
* Add the bridge |
99 changes: 99 additions & 0 deletions
99
src/Symfony/Component/Notifier/Bridge/Engagespot/EngagespotOptions.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Notifier\Bridge\Engagespot; | ||
|
||
use Symfony\Component\Notifier\Message\MessageOptionsInterface; | ||
|
||
/** | ||
* @author Daniel GORGAN <https://github.com/danut007ro> | ||
* | ||
* @see https://docs.engagespot.co/how-to-send-notifications-via-engagespot-api/how-to-send-notifications-using-engagespot-rest-api | ||
*/ | ||
final class EngagespotOptions implements MessageOptionsInterface | ||
{ | ||
protected $options; | ||
|
||
public function __construct(array $options = []) | ||
{ | ||
$this->options = $options; | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
return $this->options; | ||
} | ||
|
||
public function getRecipientId(): ?string | ||
{ | ||
return $this->options['to']; | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function campaignName(string $campaignName): static | ||
{ | ||
$this->options['campaign_name'] = $campaignName; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function to(string $to): static | ||
{ | ||
$this->options['to'] = $to; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function identifiers(array $identifiers): static | ||
{ | ||
$this->options['identifiers'] = $identifiers; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function everyone(bool $everyone): static | ||
{ | ||
$this->options['everyone'] = $everyone; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function icon(string $icon): static | ||
{ | ||
$this->options['icon'] = $icon; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function url(string $url): static | ||
{ | ||
$this->options['url'] = $url; | ||
|
||
return $this; | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
src/Symfony/Component/Notifier/Bridge/Engagespot/EngagespotTransport.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Notifier\Bridge\Engagespot; | ||
|
||
use Symfony\Component\Notifier\Exception\InvalidArgumentException; | ||
use Symfony\Component\Notifier\Exception\TransportException; | ||
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException; | ||
use Symfony\Component\Notifier\Message\MessageInterface; | ||
use Symfony\Component\Notifier\Message\PushMessage; | ||
use Symfony\Component\Notifier\Message\SentMessage; | ||
use Symfony\Component\Notifier\Transport\AbstractTransport; | ||
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; | ||
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
|
||
/** | ||
* @author Daniel GORGAN <https://github.com/danut007ro> | ||
*/ | ||
final class EngagespotTransport extends AbstractTransport | ||
{ | ||
protected const HOST = 'api.engagespot.co/2/campaigns'; | ||
|
||
private $apiKey; | ||
private $campaignName; | ||
|
||
public function __construct(string $apiKey, string $campaignName, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null) | ||
{ | ||
$this->apiKey = $apiKey; | ||
$this->campaignName = $campaignName; | ||
$this->client = $client; | ||
|
||
parent::__construct($client, $dispatcher); | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return sprintf('engagespot://%s?campaign_name=%s', $this->getEndpoint(), $this->campaignName); | ||
} | ||
|
||
public function supports(MessageInterface $message): bool | ||
{ | ||
return $message instanceof PushMessage; | ||
} | ||
|
||
protected function doSend(MessageInterface $message): SentMessage | ||
{ | ||
if (!$message instanceof PushMessage) { | ||
throw new UnsupportedMessageTypeException(__CLASS__, PushMessage::class, $message); | ||
} | ||
|
||
$endpoint = sprintf('https://%s', $this->getEndpoint()); | ||
$options = ($opts = $message->getOptions()) ? $opts->toArray() : []; | ||
if (!isset($options['to'])) { | ||
$options['to'] = $message->getRecipientId(); | ||
} | ||
|
||
$sendToEveryone = $options['everyone'] ?? false; | ||
if (!$sendToEveryone) { | ||
// Use either "to" or "identifiers" as recipient list. | ||
if (null !== $options['to']) { | ||
$identifiers = [$options['to']]; | ||
} elseif (!\is_array($options['identifiers'] ?? null)) { | ||
throw new InvalidArgumentException(sprintf('The "%s" transport required the "to" or "identifiers" option to be set when not sending to everyone.', __CLASS__)); | ||
} else { | ||
$identifiers = $options['identifiers']; | ||
} | ||
} | ||
|
||
$response = $this->client->request('POST', $endpoint, [ | ||
'headers' => [ | ||
'API-Key' => $this->apiKey, | ||
], | ||
'json' => [ | ||
'campaign_name' => $options['campaign_name'] ?? $this->campaignName, | ||
'notification' => [ | ||
'title' => $message->getSubject(), | ||
'message' => $message->getContent(), | ||
'icon' => $options['icon'] ?? '', | ||
'url' => $options['url'] ?? '#', | ||
], | ||
'send_to' => $sendToEveryone ? 'everyone' : 'identifiers', | ||
'identifiers' => $identifiers ?? null, | ||
], | ||
]); | ||
|
||
try { | ||
$statusCode = $response->getStatusCode(); | ||
if (200 !== $statusCode) { | ||
throw new TransportException('Invalid status code received from Engagespot server: '.$statusCode, $response); | ||
} | ||
} catch (TransportExceptionInterface $e) { | ||
throw new TransportException('Could not reach the remote Engagespot server.', $response, 0, $e); | ||
} | ||
|
||
$jsonContents = $response->toArray(false); | ||
if ('ok' !== $jsonContents['status'] ?? null) { | ||
$errorMessage = $jsonContents['message'] ?? $response->getContent(false); | ||
|
||
throw new TransportException('Unable to post the Engagespot message: '.$errorMessage, $response); | ||
} | ||
|
||
return new SentMessage($message, (string) $this); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/Symfony/Component/Notifier/Bridge/Engagespot/EngagespotTransportFactory.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Notifier\Bridge\Engagespot; | ||
|
||
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException; | ||
use Symfony\Component\Notifier\Transport\AbstractTransportFactory; | ||
use Symfony\Component\Notifier\Transport\Dsn; | ||
use Symfony\Component\Notifier\Transport\TransportInterface; | ||
|
||
/** | ||
* @author Daniel GORGAN <https://github.com/danut007ro> | ||
*/ | ||
final class EngagespotTransportFactory extends AbstractTransportFactory | ||
{ | ||
public function create(Dsn $dsn): EngagespotTransport | ||
{ | ||
$scheme = $dsn->getScheme(); | ||
|
||
if ('engagespot' !== $scheme) { | ||
throw new UnsupportedSchemeException($dsn, 'engagespot', $this->getSupportedSchemes()); | ||
} | ||
|
||
$apiKey = $dsn->getUser(); | ||
$campaignName = $dsn->getRequiredOption('campaign_name'); | ||
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost(); | ||
$port = $dsn->getPort(); | ||
|
||
return (new EngagespotTransport($apiKey, $campaignName, $this->client, $this->dispatcher))->setHost($host)->setPort($port); | ||
} | ||
|
||
protected function getSupportedSchemes(): array | ||
{ | ||
return ['engagespot']; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (c) 2022 Fabien Potencier | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is furnished | ||
to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
23 changes: 23 additions & 0 deletions
23
src/Symfony/Component/Notifier/Bridge/Engagespot/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
Engagespot Notifier | ||
=================== | ||
|
||
Provides [Engagespot](https://docs.engagespot.co/how-to-send-notifications-via-engagespot-api/how-to-send-notifications-using-engagespot-rest-api) integration for Symfony Notifier. | ||
|
||
DSN example | ||
----------- | ||
|
||
``` | ||
ENGAGESPOT_DSN=engagespot://API_KEY@default?campaign_name=CAMPAIGN_NAME | ||
``` | ||
|
||
where: | ||
danut007ro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- `API_KEY` is your Engagespot API Key | ||
- `CAMPAIGN_NAME` is your default campaign name | ||
|
||
Resources | ||
--------- | ||
|
||
* [Contributing](https://symfony.com/doc/current/contributing/index.html) | ||
* [Report issues](https://github.com/symfony/symfony/issues) and | ||
[send Pull Requests](https://github.com/symfony/symfony/pulls) | ||
in the [main Symfony repository](https://github.com/symfony/symfony) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.