This commit is contained in:
Maxime 2024-12-13 11:52:01 +01:00
commit 6de775c8b7
9 changed files with 605 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.idea/
/vendor/

92
README.md Normal file
View File

@ -0,0 +1,92 @@
# laravel-push
The Bluesquare Push package allows you to send Push templates using Laravel notification system.
## Installation
First prepare your composer.json:
```
composer config repositories.push vcs https://git.bluesquare.io/bluesquare/laravel-push.git -n
```
Then install the package:
```bash
composer require bluesquare/laravel-push
```
If you want to customize the config system:
```bash
php artisan vendor:publish
```
Finally, copy your API key from Bluesquare Push (senders section), and add it in your `.env`:
```bash
PUSH_API_KEY=your_api_key
```
You may also want to use your own API:
```bash
PUSH_API_URL=https://push.bluesquare.io/api/send
```
## Usage
First, create a new notification or use an existing one:
```bash
php artisan make:notification MyAwesomeNotification
```
Add BluescaleMailChannel in the `via()` method:
```php
return [PushChannel::class];
```
Then, create a 'toTemplate()' method, in which you specify your template fields :
```php
public function toTemplate($notifiable)
{
return (new PushTemplate("my-template-slug"))
->sender("john.doe@gmail.com", "John OPTIONAL") // OPTIONAL
->replyTo("jean.grey@gmail.com", "Jean OPTIONAL") // OPTIONAL
->recipients([
[
"email" => "elvis@gmail.com",
"name": "Elvis", // OPTIONAL
"parameters": ["key" => "value"] // OPTIONAL
"unsubscribe_url": "https://my-unsubscribe-url.com" // OPTIONAL
],
// ...
])
->addRecipient("edward@gmail.com") // OPTIONAL
->addRecipient(
email: "edward@gmail.com",
name: "Edward", // OPTIONAL
parameters: ["key" => "value"], // OPTIONAL
unsubscribe_url: "https://my-unsubscribe-url.com" // OPTIONAL
)
->parameters(["key" => "value", "key" => "value"]) // OPTIONAL
->addParameter("key", "value") // OPTIONAL
->bulkMode() // OPTIONAL
->testMode() // OPTIONAL
->unsubscribeUrl("https://group-unsubscribe-url.com") // OPTIONAL
;
}
```
The $notifiable is automatically added as recipient.
In your controller, you can now send your template:
```php
User::first()->notify(new MyAwesomeNotification);
```

41
composer.json Normal file
View File

@ -0,0 +1,41 @@
{
"name": "bluesquare/laravel-push",
"description": "Send Bluesquare Push templates from your Laravel notifications.",
"keywords": [
"package",
"bluesquare",
"push",
"mail",
"api",
"template",
"email"
],
"homepage": "https://git.bluesquare.io/bluesquare/laravel-push",
"license": "proprietary",
"authors": [
{
"name": "Maxime Renou",
"email": "maxime@bluesquare.io",
"homepage": "https://bluesquare.io/",
"role": "Developer"
}
],
"minimum-stability": "dev",
"autoload": {
"psr-4": {
"Bluesquare\\Push\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"PushServiceProvider"
]
}
},
"require": {
"guzzlehttp/guzzle": "^7.0.1",
"php": "^7.3 | ^8.0"
},
"prefer-stable": true
}

6
config/push.php Normal file
View File

@ -0,0 +1,6 @@
<?php
return [
'api_key' => env('PUSH_API_KEY', null),
'api_url' => env('PUSH_API_URL', null)
];

97
src/PushApi.php Normal file
View File

@ -0,0 +1,97 @@
<?php
namespace Bluesquare\Push;
use Bluesquare\Push\PushTemplate;
use GuzzleHttp\Client;
use Psr\Http\Message\StreamInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class PushApi
{
/**
* The API key
*
* @var string
*/
private $api_key;
public function __construct(string $api_key)
{
$this->api_key = $api_key;
}
/**
* @param PushTemplate $template
* @param $notifiable
* @return \Psr\Http\Message\StreamInterface
* @throws PushException
*/
public function send($template, $notifiable): StreamInterface
{
$url = config('push.api_url') ?? 'https://push.bluesquare.io/api/send';
$client = new Client();
if (is_object($notifiable) && isset($notifiable->email)) {
$notifiable_recipient = false;
$notifiable_email = $notifiable->email;
$recipients = $template->recipients;
if (count($recipients) > 0)
foreach ($recipients as $recipient)
if (! empty($recipient['email']))
if ($notifiable_email == $recipient['email'])
$notifiable_recipient = true;
if (! $notifiable_recipient)
$template->recipients[] = [
'email' => $notifiable->email
];
}
$multipart_form = [];
foreach ($template as $key => $value) {
if ($key == 'attachments') {
foreach ($value as $i => $attachment) {
$multipart_form[] = [
'name' => "{$key}[$i]",
'contents' => file_get_contents($attachment['path']),
'filename' => $attachment['filename']
];
}
continue;
}
if (! is_array($value)) {
$multipart_form[] = [
'name' => $key,
'contents' => $value
];
continue;
}
foreach ($value as $subkey => $subvalue) {
$multipart_form[] = [
'name' => "{$key}[$subkey]" . (is_array($subvalue) ? '[' . key($subvalue) . ']' : '' ),
'contents' => (is_array($subvalue) ? reset($subvalue) : $subvalue)
];
}
}
try {
return $client->request('post', $url, [
'multipart' => $multipart_form,
'headers' => [
'Authorization' => 'Bearer ' . $this->api_key,
'Accept' => 'application/json'
]
])->getBody();
} catch(\Exception $e) {
throw new PushException($e->getMessage());
}
}
}

29
src/PushChannel.php Normal file
View File

@ -0,0 +1,29 @@
<?php
namespace Bluesquare\Push;
use Illuminate\Notifications\Notification;
use Psr\Http\Message\StreamInterface;
class PushChannel
{
protected $api;
public function __construct(PushApi $api)
{
$this->api = $api;
}
/**
* @param $notifiable
* @param Notification $notification
* @return \Psr\Http\Message\StreamInterface
* @throws PushException
*/
public function send($notifiable, Notification $notification): StreamInterface
{
$template = $notification->toTemplate($notifiable);
return $this->api->send($template, $notifiable);
}
}

5
src/PushException.php Normal file
View File

@ -0,0 +1,5 @@
<?php
namespace Bluesquare\Push;
class PushException extends \Exception {}

View File

@ -0,0 +1,37 @@
<?php
namespace Bluesquare\Push;
use Illuminate\Support\ServiceProvider;
class PushServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->mergeConfigFrom(
__DIR__ . '/../config/push.php',
'push'
);
$this->app->singleton(PushApi::class, function ($app) {
return new PushApi($app['config']['push']['api_key']);
});
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
$this->publishes([
__DIR__ . '/../config/push.php' => config_path('push.php')
], 'config');
}
}

296
src/PushTemplate.php Normal file
View File

@ -0,0 +1,296 @@
<?php
namespace Bluesquare\Push;
class PushTemplate implements \JsonSerializable
{
/**
* The template for the message
*
* @var string
*/
public $template_id;
/**
* The sender for the message
*
* @var array
*/
public $sender = [];
/**
* The recipients for the message
*
* @var array
*/
public $recipients = [];
/**
* The attachments for the message
*
* @var array
*/
public $attachments = [];
/**
* The reply address for the message
*
* @var array
*/
public $replyTo = [];
/**
* The parameters for the message
*
* @var array
*/
public $parameters = [];
/**
* @var bool Whether the message should not be sent
*/
public $test_mode = false;
/**
* @var bool Mass mailing
*/
public $bulk_mode = false;
/**
* @var string|null Template's locale
*/
public $locale = null;
/**
* @var string|null Group unsubscribe URL
*/
public $unsubscribe_url = null;
/**
* @param string $template_id
* @return PushTemplate
*/
public static function make(string $template_id)
{
return new self($template_id);
}
public function __construct(string $template_id)
{
$this->template_id = $template_id;
}
/**
* @param string $email
* @param string|null $name
* @return $this
*/
public function sender(string $email, string $name = null): PushTemplate
{
$this->sender['email'] = $email;
if ($name)
$this->sender['name'] = $name;
return $this;
}
/**
* @param string $email
* @param string|null $name
* @return $this
*/
public function replyTo(string $email, string $name = null): PushTemplate
{
$this->replyTo['email'] = $email;
if ($name)
$this->replyTo['name'] = $name;
return $this;
}
/**
* @param array $recipients
* @return $this
*/
public function recipients(array $recipients): PushTemplate
{
if (! empty($recipients)) {
foreach ($recipients as $recipient) {
if (empty($recipient['email'])) {
continue;
}
$data = [
'email' => $recipient['email']
];
if (! empty($recipient['name'])) {
$data['name'] = $recipient['name'];
}
if (! empty($recipient['parameters']) && is_array($recipient['parameters'])) {
$data['parameters'] = $recipient['parameters'];
}
if (! empty($recipient['unsubscribe_url'])) {
$data['unsubscribe_url'] = $recipient['unsubscribe_url'];
}
$this->recipients[] = $data;
}
}
return $this;
}
/**
* @param string $email
* @param string|null $name
* @param array $parameters
* @param string|null $unsubscribe_url
* @return $this
*/
public function addRecipient(string $email, string $name = null, array $parameters = [], string $unsubscribe_url = null): PushTemplate
{
$data = [
'email' => $email
];
if (! is_null($name)) {
$data['name'] = $name;
}
if (! empty($parameters)) {
$data['parameters'] = $parameters;
}
if (! is_null($unsubscribe_url)) {
$data['unsubscribe_url'] = $unsubscribe_url;
}
$this->recipients[] = $data;
return $this;
}
/**
* @param array $parameters
* @return $this
*/
public function parameters(array $parameters): PushTemplate
{
$this->parameters = $parameters;
return $this;
}
/**
* @param array $attachments
* @return $this
*/
public function attachments(array $attachments): PushTemplate
{
foreach ($attachments as $attachment) {
$this->addAttachment($attachment['path'] ?? $attachment['file'], $attachment['filename'] ?? null);
}
return $this;
}
/**
* @param $file
* @param $filename
* @return $this
*/
public function addAttachment($file, $filename = null)
{
$file = is_object($file) && method_exists($file, 'getRealPath') ? $file->getRealPath() : $file;
$this->attachments[] = [
'path' => $file,
'filename' => $filename ?? basename($file)
];
return $this;
}
/**
* @param string $key
* @param string $value
* @return $this
*/
public function addParameter(string $key, string $value): PushTemplate
{
$this->parameters[$key] = $value;
return $this;
}
/**
* @param $bulk_mode
* @return $this
*/
public function bulkMode($bulk_mode = true)
{
$this->bulk_mode = $bulk_mode;
return $this;
}
/**
* @param $test_mode
* @return $this
*/
public function testMode($test_mode = true)
{
$this->test_mode = $test_mode;
return $this;
}
/**
* @param string|null $locale
* @return $this
*/
public function locale(string $locale = null)
{
$this->locale = $locale;
return $this;
}
public function unsubscribeUrl($url = null)
{
$this->unsubscribe_url = $url;
return $this;
}
/**
* Get an array representation of the message.
*
* @return array
*/
public function toArray(): array
{
return [
'template' => $this->template_id,
'locale' => $this->locale,
'recipients' => $this->recipients,
'parameters' => $this->parameters,
'reply_to' => $this->replyTo,
'sender' => $this->sender,
'bulk' => $this->bulk,
'test' => $this->test_mode,
'unsubscribe_url' => $this->unsubscribe_url,
'attachments' => $this->attachments,
];
}
public function jsonSerialize()
{
return $this->toArray();
}
}