Pre-1.0. Not intended for public use yet. The API is subject to undocumented change, and these pages may already be wrong about it.

Skip to content

Publishing Story Classes ​

Introduction ​

A Story class that extends Storyfeed\Stories\Story builds one activity with its data and publishes it.

Defining the Activity ​

shell
php artisan make:story OrderWasPlaced --verb=place --object=Order
app/Stories/OrderWasPlaced.php
php
<?php

namespace App\Stories;

use App\Models\Order;
use App\Models\User;
use Storyfeed\PendingActivity;
use Storyfeed\Stories\Story;

class OrderWasPlaced extends Story
{
    public function __construct(
        public Order $order,
        public User $customer,
    ) {}

    public function toFeedActivity(): ?PendingActivity
    {
        return $this->activity($this->order)
            ->by($this->customer)
            ->to($this->order->shop);
    }

    public function headline(): string
    {
        // Presentation methods cannot read constructor data.
        return ':actor placed :object with :target';
    }

    public function icon(): ?string
    {
        return 'shopping-bag';
    }
}
Erica Sinclair

The toFeedActivity method builds the activity. The inherited activity method sets the verb registered for this class. Return null to skip publishing.

An event implementing PublishesToFeed uses the same toFeedActivity method and publishes when dispatched. A Story can be published independently, like a notification, so use it when the activity has no corresponding application event.

Declaring Casts on a Story Class ​

A Story class declares its casts in a casts method, as a model does. This alternative order-placement class records a channel and promised time:

app/Stories/OrderPlaced.php
php
<?php

namespace App\Stories;

use App\Enums\Channel;
use App\Models\Order;
use Storyfeed\PendingActivity;
use Storyfeed\Stories\Story;

class OrderPlaced extends Story
{
    public string|array|null $objectType = Order::class;

    public function __construct(public Order $order) {}

    public function toFeedActivity(): ?PendingActivity
    {
        return $this->activity($this->order)->data([
            'channel' => $this->order->channel,
            'promised_at' => $this->order->promised_at,
        ]);
    }

    public function headline(): string
    {
        return ':actor placed :object';
    }

    public function casts(): array
    {
        return [
            'channel' => Channel::class,
            'promised_at' => 'immutable_datetime',
        ];
    }
}

To use this class instead of OrderWasPlaced, bind it to the order's verb:

routes/feed.php
php
use App\Models\Order;
use App\Stories\OrderPlaced;
use Storyfeed\Facades\Story;

Story::for(Order::class)->verb('place', OrderPlaced::class);

Registering the Story ​

Register the class for its object type and verb in the feed file:

routes/feed.php
php
use App\Models\Order;
use App\Stories\OrderWasPlaced;
use Storyfeed\Facades\Story;

Story::for(Order::class)->verb('place', OrderWasPlaced::class);

Publishing the Story ​

Construct the Story with its data and publish it:

Where the order is placed: a controller, an action, a listener
php
use App\Stories\OrderWasPlaced;
use Storyfeed\Facades\Storyfeed;

Storyfeed::publish(new OrderWasPlaced($order, $request->user()));
Erica Sinclair

Storyfeed::publish() returns the activity, or null when toFeedActivity() returns null or the Story is queued.

Queueing Stories ​

Implement ShouldQueue and use Queueable, as a queued notification does:

app/Stories/OrderWasPlaced.php
php
<?php

namespace App\Stories;

use App\Models\Order;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Storyfeed\PendingActivity;
use Storyfeed\Stories\Story;

class OrderWasPlaced extends Story implements ShouldQueue
{
    use Queueable;

    // ...
}

The Storyfeed::publish method queues a Story that implements ShouldQueue. Use the Queueable trait's methods to select the connection and queue:

app/Http/Controllers/PlaceOrderController.php__invoke()
php
use App\Stories\OrderWasPlaced;
use Storyfeed\Facades\Storyfeed;

Storyfeed::publish(
    (new OrderWasPlaced($order, $request->user()))->onQueue('feed'),
);

After the worker calls toFeedActivity() and publishes its result:

Erica Sinclair

Queued publishing returns null. Use Storyfeed::publishNow() to publish synchronously, as you would use sendNow() for a notification. Model properties are serialized by their identifiers. The publication time is captured when Storyfeed::publish() is called, unless toFeedActivity() sets it explicitly.

A queued Story may implement ShouldBeUnique and define a uniqueId method. Its middleware method declares story middleware, while the Queueable trait's through method sets job middleware. See Queued Publishing for connections, transactions, and missing models.

Presentation Methods ​

Storyfeed calls headline and icon without running the constructor, so these methods cannot use constructor data. Use that data in toFeedActivity instead. The same restriction applies to all definition methods:

MethodDeclares
intent()the icon's intent
groups()group headlines
missing()the roles that make it redundant once deleted
keepFor(), keepForever()its retention
keepLatest()keeping the latest activity
bursts()its burst window
period()the calendar period for custom axes whose keys include d
middleware()its story middleware

Released under the MIT License.