think.studio/laravel-periodic-notice

Send your periodical series to user as batch using notifications.

1.4.0 2023-07-11 15:17 UTC

This package is auto-updated.

Last update: 2024-10-11 18:46:16 UTC


README

Packagist License Packagist Version Total Downloads Build Status Code Coverage Scrutinizer Code Quality

Send your periodical series to user as batch using notifications.

Installation

Install the package via composer:

composer require think.studio/laravel-periodic-notice

You can publish the assets file with:

php artisan vendor:publish --provider="PeriodicNotice\ServiceProvider" --tag="config"
php artisan vendor:publish --provider="PeriodicNotice\ServiceProvider" --tag="lang"

To disable default migrations add this code to app service provider:

\PeriodicNotice\PeriodicNoticeManager::ignoreMigrations()

Usage

use PeriodicNotice\Concerns\HasPeriodicNotice;
use PeriodicNotice\Contracts\NotificationReceiver;
use PeriodicNotice\PeriodicNoticeDirector;

class User extends \Illuminate\Foundation\Auth\User implements NotificationReceiver
{
    use Notifiable;
    use HasPeriodicNotice;

    public function periodicNoticeDirector(string $group = 'default'): PeriodicNoticeDirector
    {
        $dayInSeconds = 60 * 60 * 24;

        return PeriodicNoticeDirector::defaults($group)
                                     ->usePeriodType($this->periodic_notification_type)
                                     ->usePeriodTypesMap([
                                         'every_day'  => round($dayInSeconds),
                                         'every_week' => round($dayInSeconds * 7),
                                     ])
                                     ->useQueryToGetEntries(Post::class);
    }

    public function scopeWithNotificationPeriodType(Builder $query, string $type, string $group = 'default')
    {
        $query->where('periodic_notification_type', '=', $type);
    }
}
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use PeriodicNotice\Concerns\InPeriodicNotice;
use PeriodicNotice\Contracts\SendableEntity;
use PeriodicNotice\Tests\Fixtures\Factories\PostFactory;

class Post extends Model implements SendableEntity
{
    use InPeriodicNotice;

    public function scopeReleasedAfter(Builder $query, \DateTimeInterface|string $dateTime, string $group)
    {
        $query->where('published_at', '>=', $dateTime);
    }
}

Manual call

php artisan periodic-notice:send:batch every_day \\App\\Models\\User
# or use morph alias
php artisan periodic-notice:send:batch every_day user
# use custom group
php artisan periodic-notice:send:batch every_day user -G custom_group

More appropriate way is using cron schedule

$schedule->command('periodic-notice:send:batch every_day user')
          ->dailyAt('18:00');

Credits

  • Think Studio