VOOZH about

URL: https://deepwiki.com/hypervel/notifications/6-message-construction

⇱ Message Construction | hypervel/notifications | DeepWiki


Loading...
Menu

Message Construction

This page provides an overview of the message construction layer in the hypervel/notifications package. Message classes define the content and structure of notifications for specific delivery channels, but do not handle the actual delivery logic.

For detailed guides on constructing specific message types, see:

For information about how messages are delivered through channels, see Notification Channels.

Message Types and Responsibilities

The package provides three primary message classes, each designed for a specific delivery channel:

Message ClassChannelBase ClassPurpose
MailMessagemailSimpleMessageEmail notifications with views, attachments, and recipients
SlackMessageslackNoneSlack notifications with Block Kit support
BroadcastMessagebroadcastNoneReal-time event data for broadcasting

Diagram: Message Class Hierarchy


Sources: src/Messages/SimpleMessage.php1-231 src/Messages/MailMessage.php1-333 src/Slack/SlackMessage.php1-336

SimpleMessage Base Class

The SimpleMessage class provides common structure for notification content, primarily used by MailMessage. It defines a standard notification format with greeting, intro lines, an optional action button, and outro lines.

Properties:

PropertyTypePurpose
levelstringNotification level: info, success, or error
subjectstring|nullSubject line for the notification
greetingstring|nullOpening greeting text
salutationstring|nullClosing salutation text
introLinesarrayLines of text before the action button
outroLinesarrayLines of text after the action button
actionTextstring|nullCall-to-action button text
actionUrlstring|nullURL for the action button

Key Methods:

The with() method src/Messages/SimpleMessage.php164-175 intelligently places content: lines added before action() go into introLines, and lines added after go into outroLines.

Sources: src/Messages/SimpleMessage.php10-231

Message Builder Pattern

All message classes implement a fluent interface pattern, allowing method chaining to construct messages declaratively:


Diagram: Message Construction Flow


Sources: src/Messages/SimpleMessage.php60-202 src/Messages/MailMessage.php93-332 src/Slack/SlackMessage.php90-291

MailMessage Extension

MailMessage extends SimpleMessage to add email-specific functionality while inheriting the base structure. This extension pattern allows emails to use the standard greeting/lines/action format or provide custom views.

Extended Properties:

View Selection Logic:

The render() method src/Messages/MailMessage.php306-322 implements this logic:

  1. If view is set, render using the mailer's template engine
  2. Otherwise, use the Markdown service to render the markdown template with the base message structure

Sources: src/Messages/MailMessage.php16-333

SlackMessage Independence

Unlike MailMessage, SlackMessage does not extend SimpleMessage. It uses a completely different structure optimized for Slack's Block Kit API.

Core Properties:

Block Kit Methods:

Slack messages are constructed using Block Kit builder methods:

For detailed information on Block Kit components, see Slack Block Kit.

Sources: src/Slack/SlackMessage.php21-336

Message-to-Channel Mapping

Notification classes define which channels to use via the via() method, and provide channel-specific messages through methods like toMail(), toSlack(), or toBroadcast():

Diagram: Message Construction and Channel Delivery


Sources: src/Messages/MailMessage.php1-333 src/Slack/SlackMessage.php1-336

Conditionable Interface

Both MailMessage and SlackMessage use the Conditionable trait src/Messages/MailMessage.php18 src/Slack/SlackMessage.php23 which provides conditional method chaining:


This allows dynamic message construction based on runtime conditions without breaking the fluent interface chain.

Sources: src/Messages/MailMessage.php18 src/Slack/SlackMessage.php23

Message Validation

Messages implement validation to ensure they can be successfully delivered:

MailMessage: No explicit validation; relies on underlying mailer to validate addresses

SlackMessage: Validates before conversion to array src/Slack/SlackMessage.php296-304:

  • Must contain at least text or blocks
  • Maximum of 50 blocks allowed (Slack API limit)
  • Throws LogicException if validation fails

BroadcastMessage: Minimal structure; primarily wraps arbitrary data

Sources: src/Slack/SlackMessage.php296-304

Array Conversion

Message classes implement toArray() to convert their structure into formats expected by delivery channels:

SimpleMessage src/Messages/SimpleMessage.php217-230: Returns all properties including computed displayableActionUrl

MailMessage src/Messages/MailMessage.php280-283: Merges base array with viewData for template rendering

SlackMessage src/Slack/SlackMessage.php296-323: Converts blocks to arrays, filters null values, formats for Slack API

The array representation is consumed by channels during the send operation.

Sources: src/Messages/SimpleMessage.php217-230 src/Messages/MailMessage.php280-283 src/Slack/SlackMessage.php296-323

Debugging Support

SlackMessage provides a dd() method src/Slack/SlackMessage.php328-335 for debugging:

  • dd(true): Dumps raw array structure
  • dd(): Generates URL to Slack Block Kit Builder with message payload

This allows developers to preview Slack messages in Slack's official builder tool before sending.

Sources: src/Slack/SlackMessage.php328-335