You've already forked laravel-push
init
This commit is contained in:
97
src/PushApi.php
Normal file
97
src/PushApi.php
Normal 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
29
src/PushChannel.php
Normal 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
5
src/PushException.php
Normal file
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
|
||||
namespace Bluesquare\Push;
|
||||
|
||||
class PushException extends \Exception {}
|
||||
37
src/PushServiceProvider.php
Normal file
37
src/PushServiceProvider.php
Normal 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
296
src/PushTemplate.php
Normal 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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user