bibsdb/repeating-campaign-bundle
Installation
composer require bibsdb/repeating-campaign-bundle
Add to config/app.php under providers:
Bibsdb\RepeatingCampaignBundle\RepeatingCampaignServiceProvider::class,
Publish Config
php artisan vendor:publish --provider="Bibsdb\RepeatingCampaignBundle\RepeatingCampaignServiceProvider"
Edit config/repeating_campaign.php to define:
workdays (e.g., ['mon', 'tue', 'wed', 'thu', 'fri'])weekend_days (e.g., ['sat', 'sun'])['09:00', '17:00'] for 9 AM–5 PM).First Use Case Trigger a campaign via the facade:
use Bibsdb\RepeatingCampaignBundle\Facades\RepeatingCampaign;
// Run a campaign for today (respects workdays/weekends)
RepeatingCampaign::run('campaign_name');
// Force a weekend run (bypasses workday checks)
RepeatingCampaign::run('campaign_name', true);
Scheduling Campaigns
Use the RepeatingCampaign facade to queue campaigns with day-specific logic:
// Run only on workdays (default)
RepeatingCampaign::schedule('daily_newsletter', '2024-01-01');
// Run on weekends only
RepeatingCampaign::schedule('weekend_promo', '2024-01-06', false, true);
campaign_name: Key for your campaign logic.date: Optional override (defaults to today).force_workday: Run even on weekends (default: true).force_weekend: Run only on weekends (default: false).Customizing Day Logic
Extend the bundle’s DayChecker trait in a service:
namespace App\Services;
use Bibsdb\RepeatingCampaignBundle\Traits\DayChecker;
class CustomCampaignChecker {
use DayChecker;
protected function isWorkday($date): bool {
// Override logic (e.g., exclude holidays)
return parent::isWorkday($date) && !in_array($date->format('Y-m-d'), config('holidays'));
}
}
Integrating with Queues Chain campaigns to Laravel’s queue system:
RepeatingCampaign::run('campaign_name')
->then(function () {
dispatch(new SendEmailJob());
});
Event-Based Triggers
Listen for campaign events in EventServiceProvider:
protected $listen = [
'Bibsdb\RepeatingCampaignBundle\Events\CampaignScheduled' => [
'App\Listeners\LogCampaignSchedule',
],
];
Time Zone Mismatches
config/app.php:
'timezone' => 'America/New_York',
\Carbon\Carbon::now()->timezone(config('app.timezone'))->format('Y-m-d H:i');
Weekend Logic Overrides
force_weekend flag excludes workdays entirely. Use sparingly to avoid conflicts:
// ❌ Runs only on weekends (never on workdays)
RepeatingCampaign::schedule('campaign', null, false, true);
// ✅ Runs on weekends + optionally on workdays
if ($this->isWeekend()) {
RepeatingCampaign::run('campaign', true); // Force workday mode
}
Config Caching
config/repeating_campaign.php, clear the config cache:
php artisan config:clear
Date Handling Quirks
Carbon internally. Ensure your input dates are Carbon instances:
$date = Carbon::parse('2024-01-01');
RepeatingCampaign::schedule('campaign', $date);
Debugging Day Checks Inspect the current day’s classification:
dd(
RepeatingCampaign::isWorkday(), // bool
RepeatingCampaign::isWeekend() // bool
);
Bulk Scheduling
Use Laravel’s collect() to schedule multiple campaigns:
collect(['newsletter', 'promo'])
->each(fn ($campaign) => RepeatingCampaign::schedule($campaign));
Extending Campaign Logic Create a custom campaign runner:
namespace App\Services;
use Bibsdb\RepeatingCampaignBundle\RepeatingCampaign;
class CustomCampaignRunner {
public function run($campaign, $forceWorkday = true) {
if ($this->shouldRun($campaign, $forceWorkday)) {
// Custom logic here
RepeatingCampaign::trigger($campaign);
}
}
}
Testing
Mock the DayChecker trait in PHPUnit:
$mockChecker = $this->getMockBuilder(StdClass::class)
->setMethods(['isWorkday', 'isWeekend'])
->getMock();
$mockChecker->method('isWorkday')->willReturn(true);
RepeatingCampaign::setDayChecker($mockChecker);
How can I help you explore Laravel packages today?