Code Monkey home page Code Monkey logo

cake-notifications's Introduction

CakePHP 3 Notifications Plugin

Build Status Code Coverage v2-dev License

A CakePHP 3.8 notification plugin which can send out emails asynchronously through to the cakephp-queuesadilla job queue.

Requirements

Installation

1. Install the plugin via composer

composer require codekanzlei/cake-notifications

2. Load the plugin in your src/Application.php

$this->addPlugin('Notifications');

3. Configure config/app.php

Set your default locale in a config file, for example in app.php. This config is mandatory and will cause an exception if not set.

'Notifications' => [
    'defaultLocale' => 'en_US'
]

You can also override the queue options like attempts, attempts_delay, delay, expires_in and queue.

'Notifications' => [
    'queueOptions' => [
        'queue' => 'notification'
    ]
]

This doesn't affect the use of queueOptions() later. You can still override the options there.

Also, be sure to set up the the cakephp-queuesadilla plugin config. You can find an example config here: https://cakephp-queuesadilla.readthedocs.io/en/latest/.

Or you can find available config options inside your used Engine file (vendor/josegonzalez/queuesadilla/src/josegonzalez/Queuesadilla/Engine/*Engine.php) inside the $baseConfig property.

IMPORTANT: Set "date.timezone" in your cli/php.ini to an appropriate value, else notifications with a delay_until could be sent out at the wrong time.

Usage

Email

The EmailNotification is completely compatible with the CakePHP Email.

Add the following to your class where you want to send an email:

use Notifications\Notification\EmailNotification;

Then simply create a new EmailNotification object.

$email = new EmailNotification();
$email->to('[email protected]')
    ->setSubject('Send with cake-notifications v2')
    ->send('Hello :)');

You can chain all methods provided by the CakePHP Email Class http://book.cakephp.org/3.0/en/core-libraries/email.html

Additional, following functions are available:

send( array|string|null $content null )

Send out the email immediately. before- and afterSend callbacks are still available

setLocale( string|null $locale null )

Set the locale for the notification. If null, Configure::read('Notifications.defaultLocale') is used.

push()

Push the email into the queue to send it asynchronous

setQueueOptions( array $options null )

You can change some of the default options from the cakephp-queuesadilla plugin.

Supported options:

  • attempts how often the notification will be executed again after a failure
  • attempts_delay how long it takes in seconds until the notification will be executed again
  • delay how long it takes until the notification will be executed for the first time in seconds
  • expires_in how long the notification will stay in the queue in seconds
  • queue name of the queue

setBeforeSendCallback( array|string|null $class null, array $args [] )

Pass a callable as the $class parameter. Static and none-static functions are supported.

$email->beforeSendCallback(['Foo', 'bar'], ['first_param', 'second_param'])

This will call the bar method inside the Foo class with two parameters before the email is send.

To manipulate the EmailNotification instance before sending, the beforeSendCallback may return a function taking the notification instance reference and for example changing the profile. The bar method may then look something like this:

public function bar($first_param, $second_param)
{
    // do something
    return function (&$instance) {
        $instance->profile([
            'from' => '[email protected]'
        ]);
    };
}

setAfterSendCallback( array|string|null $class null, array $args [] )

Pass a callable as the $class parameter. Static and none-static functions are supported.

$email-> afterSendCallback(['Foo::bar'], ['first_param', 'second_param'])

This will call the static bar method inside the Foo class with two parameters after the email was send.

addBeforeSendCallback( array|string|null $class null, array $args [] )

Add an additional callback to beforeSend.

addAfterSendCallback( array|string|null $class null, array $args [] )

Add an additional callback to afterSend.

cake-notifications's People

Contributors

cleptric avatar felixkempf avatar wolfgang-braun avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.