DI and actions
The facade is the recommended default, not the only way in. Three equivalent entry points run the same code:
- The Campaigns facade — the shortest form, used throughout these docs.
- The manager, RoundlyConsulting\Campaigns\CampaignManager — the facade root, a container singleton, injected through the constructor. Same API, an explicit dependency and no static calls.
- Actions — single-purpose classes with execute(), for composing into your own actions, jobs and commands.
Inject the manager
use RoundlyConsulting\Campaigns\CampaignManager;
final class LaunchSpringSale
{
public function __construct(private CampaignManager $campaigns) {}
public function __invoke(iterable $subscribers): void
{
$campaign = $this->campaigns->create('Spring sale', '<p>50% off</p>')->to($subscribers)->prepare();
$this->campaigns->start($campaign);
}
}Call an action
Every write is an action. Build the Campaign and its CampaignRecipient objects yourself and run them:
use Illuminate\Support\Str;
use RoundlyConsulting\Campaigns\Actions\CancelCampaignAction;
use RoundlyConsulting\Campaigns\Actions\PrepareCampaignAction;
use RoundlyConsulting\Campaigns\Actions\StartCampaignAction;
use RoundlyConsulting\Campaigns\Campaign;
use RoundlyConsulting\Campaigns\CampaignRecipient;
$campaign = app(PrepareCampaignAction::class)->execute(
new Campaign(uuid: (string) Str::uuid(), subject: 'Spring sale', content: '<p>50% off</p>', fromName: 'Shop', fromAddress: '[email protected]'),
[new CampaignRecipient(uuid: (string) Str::uuid(), name: 'John Doe', reachableAt: '[email protected]')],
);
app(StartCampaignAction::class)->execute($campaign);
app(CancelCampaignAction::class)->execute($campaign->uuid);Facade method → action
| Action | Facade |
|---|---|
PrepareCampaignAction | Campaigns::prepare(), create()->prepare() |
StartCampaignAction | Campaigns::start(), campaign()->start(), create()->dispatch() |
CancelCampaignAction | Campaigns::cancel(), campaign()->cancel(), campaigns:cancel |
MarkRecipientProcessedAction | Campaigns::campaign()->markProcessed() |
MarkRecipientFailedAction | Campaigns::campaign()->markFailed() |
The actions take the plain value objects: PrepareCampaignAction::execute(Campaign, iterable $recipients), StartCampaignAction and CancelCampaignAction::execute(Campaign|string), MarkRecipientProcessedAction::execute(Campaign, CampaignRecipient) and MarkRecipientFailedAction::execute(Campaign, CampaignRecipient, string $error).
Campaigns::fake() replaces the manager in the container as well as behind the facade, so injected managers, the campaigns:cancel command and delivery jobs all record into it. An action you resolve and call yourself bypasses the fake and runs for real. ChangeCampaignStatusAction — the one status transition the lifecycle actions and the batch callback share — and FinishCampaignAction — what the batch runs once every delivery job ran — are @internal, so don’t call them from your code.
Show your open-source love
This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.
More ways to support, including cryptoBy donating, you agree to our donation terms.
Want this built into your product?
We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.