Skip to content

Commit

Permalink
docs: update messaging docs
Browse files Browse the repository at this point in the history
  • Loading branch information
papac committed Jan 26, 2025
1 parent 462e51f commit 0df24d9
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 84 deletions.
33 changes: 33 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Mail Configuration
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=[email protected]
MAIL_FROM_NAME=Example
MAIL_SSL=false
MAIL_TLS=true

# DKIM Configuration
MAIL_DKIM_ENABLED=false
MAIL_DKIM_DOMAIN=example.com
MAIL_DKIM_SELECTOR=default
MAIL_DKIM_PRIVATE_KEY=/path/to/private.key
MAIL_DKIM_PASSPHRASE=null
MAIL_DKIM_IDENTITY=null

# SPF Configuration
MAIL_SPF_ENABLED=false
MAIL_SPF_STRICT=true

# Queue Configuration
MAIL_QUEUE_ENABLED=false
MAIL_QUEUE_CONNECTION=sync
MAIL_QUEUE_NAME=default

# AWS Configuration (for SES)
AWS_ACCESS_KEY_ID=null
AWS_SECRET_ACCESS_KEY=null
AWS_DEFAULT_REGION=us-east-1
127 changes: 43 additions & 84 deletions docs/messaging.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ use Bow\Database\Barry\Model;
class User extends Model
{
use CanSendMessage;

// Votre modèle peut avoir d'autres propriétés et méthodes
protected $fillable = ['name', 'email'];
}
```

Expand All @@ -38,7 +35,7 @@ Une notification est une classe qui étend `Messaging`. Voici un exemple complet

```php
use Bow\Messaging\Messaging;
use Bow\Mail\Message;
use Bow\Mail\Envelop;
use Bow\Database\Barry\Model;

class WelcomeMessage extends Messaging
Expand All @@ -54,26 +51,26 @@ class WelcomeMessage extends Messaging
/**
* Configuration du message email
*/
public function toMail(Model $notifiable): Message
public function toMail(Model $context): ?Envelop
{
return (new Message())
->to($notifiable->email)
return (new Envelop())
->to($context->email)
->subject('Bienvenue sur notre plateforme!')
->view('emails.welcome', [
'user' => $notifiable,
'user' => $context,
'message' => $this->customMessage
]);
}

/**
* Configuration de la notification en base de données
*/
public function toDatabase(Model $notifiable): array
public function toDatabase(Model $context): array
{
return [
'type' => 'welcome_notification',
'data' => [
'user_id' => $notifiable->id,
'user_id' => $context->id,
'message' => $this->customMessage,
'created_at' => now()
]
Expand All @@ -83,10 +80,10 @@ class WelcomeMessage extends Messaging
/**
* Définition des canaux à utiliser
*/
public function channels(Model $notifiable): array
public function channels(Model $context): array
{
// Vous pouvez ajouter une logique conditionnelle
if ($notifiable->preferences['email_notifications']) {
if ($context->preferences['email_notifications']) {
return ['mail', 'database'];
}

Expand All @@ -107,21 +104,21 @@ class PasswordResetMessage extends Messaging
) {
}

public function toMail(Model $notifiable): Message
public function toMail(Model $context): Envelop
{
$resetUrl = url("/password/reset/{$this->token}");

return (new Message())
->to($notifiable->email)
return (new Envelop())
->to($context->email)
->subject('Réinitialisation de votre mot de passe')
->view('emails.password-reset', [
'user' => $notifiable,
'user' => $context,
'resetUrl' => $resetUrl,
'expiresIn' => '60 minutes'
]);
}

public function channels(Model $notifiable): array
public function channels(Model $context): array
{
return ['mail'];
}
Expand All @@ -138,18 +135,18 @@ class NewCommentMessage extends Messaging
) {
}

public function toMail(Model $notifiable): Message
public function toMail(Model $context): Envelop
{
return (new Message())
->to($notifiable->email)
return (new Envelop())
->to($context->email)
->subject('Nouveau commentaire sur votre post')
->view('emails.new-comment', [
'user' => $notifiable,
'user' => $context,
'comment' => $this->commentData
]);
}

public function toDatabase(Model $notifiable): array
public function toDatabase(Model $context): array
{
return [
'type' => 'new_comment',
Expand All @@ -162,7 +159,7 @@ class NewCommentMessage extends Messaging
];
}

public function channels(Model $notifiable): array
public function channels(Model $context): array
{
return ['mail', 'database'];
}
Expand Down Expand Up @@ -226,15 +223,15 @@ $user->sendMessageLaterOn(
Le canal email est parfait pour les communications importantes. Exemple complet d'une notification par email :

```php
public function toMail(Model $notifiable): Message
public function toMail(Model $context): Envelop
{
return (new Message())
->to($notifiable->email)
return (new Envelop())
->to($context->email)
->cc('[email protected]')
->bcc('[email protected]')
->subject('Sujet Important')
->view('emails.template', [
'user' => $notifiable,
'user' => $context,
'data' => $this->data
])
->attach('/chemin/vers/fichier.pdf')
Expand All @@ -261,7 +258,7 @@ CREATE TABLE notifications (
Exemple d'utilisation avancée :

```php
public function toDatabase(Model $notifiable): array
public function toDatabase(Model $context): array
{
return [
'type' => 'system_notification',
Expand All @@ -281,81 +278,43 @@ public function toDatabase(Model $notifiable): array
}
```

### SMS

Pour implémenter le canal SMS, créez une classe dédiée :

```php
class SmsChannel implements ChannelInterface
{
public function __construct(
private SmsService $smsService
) {
}

public function send(Model $notifiable): void
{
$this->smsService->send(
$notifiable->phone_number,
$this->formatMessage()
);
}
}
```

## Extension du Système

### Création d'un Nouveau Canal

Exemple de création d'un canal pour les notifications Slack :
Exemple de création d'un canal pour les notifications Log :

```php
namespace App\Messaging\Channels;

use Bow\Messaging\Contracts\ChannelInterface;
use Bow\Database\Barry\Model;

class SlackChannel implements ChannelInterface
class LogChannel implements ChannelInterface
{
public function __construct(
private array $slackConfig
) {
}

public function send(Model $notifiable): void
public function send(Model $context, Messaging $message): void
{
$client = new SlackClient($this->slackConfig['webhook_url']);

$client->send([
'channel' => $this->slackConfig['channel'],
'text' => $this->slackConfig['message'],
'attachments' => $this->slackConfig['attachments'] ?? []
]);
if (!method_exists($message, 'toLog')) {
throw new \InvalidArgumentException('The message must have a toLog method.');
}

$log = $message->toLog($context);

logger()->log($log);
}
}
```

### Enregistrement du Nouveau Canal

```php
// Dans votre classe de notification
protected array $channels = [
'slack' => SlackChannel::class
];
Pour enregistrer votre nouveau canal, vous devez l'ajouter dans la configuration de l'application. Dans votre fichier `App\Configurations\ApplicationConfiguration.php`, ajoutez le code suivant dans la méthode `create()` :

public function toSlack(Model $notifiable): array
```php title="app/Configurations/ApplicationConfiguration.php"
public function create(Loader $config): void
{
return [
'webhook_url' => config('services.slack.webhook_url'),
'channel' => '#notifications',
'message' => 'Nouvelle notification!',
'attachments' => [
[
'title' => 'Détails',
'text' => 'Contenu de la notification'
]
]
];
Messaging::pushChannels([
'log' => LogChannel::class
]);
}
```

Expand All @@ -375,8 +334,8 @@ Pour déboguer vos notifications, vous pouvez :
// Journaliser les envois
Log::info('Envoi de notification', [
'type' => get_class($notification),
'user' => $notifiable->id,
'channels' => $notification->channels($notifiable)
'user' => $context->id,
'channels' => $notification->channels($context)
]);

// Tester en environnement local
Expand Down

0 comments on commit 0df24d9

Please sign in to comment.