Download the PHP package modera/notification-bundle without Composer

On this page you can find all versions of the php package modera/notification-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package notification-bundle

ModeraNotificationBundle

Provides functionality to store/retrieve notifications.

Installation

Add this dependency to your composer.json:

"modera/notification-bundle": "dev-master"

If bundle is used in conjunction with Modera Foundation then no additional configuration is needed. If you are using plain Symfony, then you need to configure Doctrine ORM and specify what implementation of Symfony\Component\Security\Core\User\UserInterface you are using. For example, if you have a entity Acme/AppBundle/Entity/User which happen to implement UserInterface, then you could add following to config.yml file to make ModeraNotificationBundle bundle work:

doctrine:
    orm:
        resolve_target_entities:
            Symfony\Component\Security\Core\User\UserInterface: Acme/AppBundle/Entity/User

Also, don't forget to update your AppKernel class and add ModeraNotificationBundle there:

new \Modera\NotificationBundle\ModeraNotificationBundle(),

And finally update database:

app/console doctrine:schema:update --force

Documentation

The bundle provides extensible architecture with a simple high-level API which can be used by developers to integrate user notifications support in their own applications. Extensibility is achieved by using so called "channels" which are used to consolidate logic necessary for delivering a notification through a specific medium - for example a channel could be responsible for delivering a piece of text by sending SMS message or even using Push notification for mobile device, the bundle provides a set of very intuitive APIs which developers can use to create their own channels.

The first thing you deal with when you want to add a support of delivering notifications to your application is to use a notification center which is represented by an instance of Modera\NotificationBundle\Dispatching\NotificationCenter class, most of the time though you won't want to create its instance manually but instead use modera_notification.dispatching.notification_center Dependency Injection service. For example, in order to get a notification service from a controller you can do something like that:

public function indexAction()
{
    $notificationCenter = $this->container->get('modera_notification.dispatching.notification_center');
}

In a nutshell, notification center provides a high-level API which you will work with when configuring and sending notifications from your application code, some of the operations that you can achieve by using notification center are:

Sending notifications

As it was already mentioned in order to send a notification you need to use a notification center and rely on its very simply and high-level API. When sending a notification you can configure several things:

The first thing you need to do when you want to send a notification is to create so called "notification builder", it is an object which provides a set of methods which you can use to tweak things that were mentioned in the list above. This is what you need to do in order to send your first notification:

$user = $em->getRepository(User::class)->findOneBy(array('username' => 'foo_user'));

$notificationCenter->createNotificationBuilder('hello world', 'test-group');
    ->addRecipient($user)
    ->dispatch()
;

When you invoke a createNotificationBuilder under the hood the notification center will create an instance of Modera\NotificationBundle\Dispatching\NotificationBuilder linked with the given notification center which provides fluent-interface methods that you can chain to configure your notification. First argument of the method contains a piece of text that this notification is about and second one - a group name (please refer to NotificationBuilder class properties in order to see full description of what "group" parameter is used for). In this case once an instance of notification builder is created the last thing we need to do in order to send-out our notification is to specify who should receive it, for this you can use either addRecipient or setRecipients methods, when invoking them you need to provide implementations of UserInterface which we talked about in "Installation" section of this guide. Once you have configured your notification you can invoke dispatch method and notification will be attempted to be delivered through all registered channels because we neither used setChannels method which accepts list of channel IDs that must be used nor specified them when invoking dispatch method. Here's another example how you can send a notification while specifying which channels should be used:

$notificationCenter->createNotificationBuilder('hello world', 'test-group');
    ->setRecipients([$bob, $jane])
    ->dispatch(['push', 'email'])
;

Getting more control over notifications sending

So far we have taken a look at very simply set of methods that you can use when sending notifications, at times though you may want to get som extra more control over how notification is being sent, what optional meta-information it contains and after it has been sent you may want to see if there were any problems while delivering it. Here's more advanced example:

$notificationCenter->createNotificationBuilder('hello world', 'test-group');
    ->setMetaProperty('sent_by', '[email protected]')
    ->setContextProperty('sms_api_key', 12345)
    ->throwExceptionWhenChannelNotFound()
    ->setRecipients([$bob, $jane])
    ->dispatch(['push', 'sms', 'email'])
;

Sometimes after a notification is delivered to a user and user wants to perform some action on it there is a need to store some additional normalized information with the notification that handling logic can used to get better understanding what actions should be taken when user has acknowledged a notification - for this purpose you have two methods at your disposal - setMeta and setMetaProperty. First method allows you to override all available meta information stored in a specific notification-builder while second one - setMetaProperty can be used to specify one meta-property at time.

Next method that we have in our invocation chain is setContextProperty. While meta-information is stored in persistence storage with the notification's contents itself, context-properties will only live during this specific request-response server cycle. Usually you may want to use context properties to tweak a specific notification channel configuration properties that affect how a notification is delivered, but once a notification is delivered these details can be discarded.

Last method throwExceptionWhenChannelNotFound can be used to instruct the notification center that if some of the specified channels are not found then exception must be thrown - default behaviour is the opposite, if some channels are not found they simply will be ignored. Once throwExceptionWhenChannelNotFound has been invoked you can use suppressChannelNotFoundException to revert it back to default behaviour.

Controlling notification dispatch status

What we haven't yet talked about is that it is possible to figure out if a notification has been successfully delivered or what channels could have possibly failed and with what error. When you dispatch a notification using dispatch method on NotificationBuilder then an instance of Modera\NotificationBundle\Dispatching\DeliveryReport is returned which you can use to get dispatching status. This is an example showing some of the methods that you can use:

$report = $notificationCenter->createNotificationBuilder('hello world', 'test-group');
    // ...
    ->dispatch()
;

if ($report->isSuccessful()) {
    echo "Everything's good, it seems all channel managed to deliver a notification.";
} else {
    foreach ($report->getFailedDeliveries() as $info) {
        echo sprintf("%s: failed to deliver a notification, error: ", $info['channel']->getId(), $info['error']);
        echo $info['meta'] ? print_r($info['meta'], true) : 'No meta-information provided';
        echo "\n";
    }
}

Besides isSuccessful and getFailedDeliveries methods there's another few you can use:

For a full list of available methods please refer to DeliveryReport class.

Command line

Bundle provides several command line commands that might prove useful as in development as well as in production environment.

Cleaning up old notifications

Whenever a new notification is dispatched a few records in database are created and when you have a massive deployment with a lots of users your notifications database eventually might grow and performance might start to degrade. In order to address this issue the bundle ships modera:notification:clean-up console command which you can use to to clean those notifications which were already marked as read:

app/console modera:notification:clean-up

Dispatching a notification

Sometimes during development you might want to emulate a notification being dispatched without writing required source manually, if this is the case then we got your covered, the bundle provides modera:notification:send-notification command which you can use to dispatch notifications from a console environment, here is an example:

app/console modera:notification:send-notification "Hello world" modera_backend_chat_notification_bridge "*"

This is the bare minimum of configuration parameters that you need to pass in order to dispatch a notification, let's take a look at them more closely:

Additionally there are two other parameters which we haven't yet shown, it is possible to specify channels which should be used to dispatch a notification as well as you have a chance to specify meta-keys that will be stored with given notification. This is an example how you would specify that a notification should only be dispatched using "email", "sms" channels and contain "sender" meta-key:

app/console modera:notification:send-notification "Hello world" modera_backend_chat_notification_bridge "*" --channels="email,sms" --meta="[email protected]"

You can specify as many --meta configuration options as you want, the contents of this option is a string separated by an equal sign (=) where on left side is parameter name and on the right side its value.

Architecture

The bundle has quite simple and straightforward architecture consisting of three components - a notifications center, channels and notification objects. The first one you have already seen in previous sections, as reminder, a notification class is represented by a Modera\NotificationBundle\Dispatching\NotificationCenter, this is the class whose API you will use in your application logic when adding support for delivering notifications. The second that we have slightly mentioned is far is "channel", which is represented by classes which implement Modera\NotificationBundle\Dispatching\ChannelInterface interface. In a nutshell a notification center when it is coming to delivering notification is responsible only for mitigating collaboration between channels that a developer has showed a desire to dispatch a notification through and storing notifications data in database but all heavy lifting for delivering a notification through different mediums is taken care of in channels. The third component is notification object itself, which is represented by Modera\NotificationBundle\Model\NotificationInterface, this interface provides very generic and persistence technology agnostic API for reading notification details, methods fetchBy and fetchOneBy described in a next section return instances of this interface.

Notification center

So far we have used only one method from NotificationCenter - createNotificationCenter but in fact there's another few ones that you can use to query and modify notifications:

Channels & creating a channel

As it was already mentioned in the previous section, channels are the ones who actually are responsible for doing the heavy lifting and eventually delivering a notification through a specific medium. The best way to understand how channels work is to create one and in this section we will create a very simple channel which will write dispatched notifications to Monolog logger.

If you have already read previous sections then probably you remember that a channel is represented by ChannelInterface, this interface contains only a handful method and usually you still won't want to implement this interface directly but instead extend Modera\NotificationBundle\Dispatching\AbstractChannel which will have some methods implemented for you, but let's still take a moment and describe what the interface's methods do:

Now that you know what channel's methods are let's write our custom channel, this is our Monolog channel's implementation (all comments are intentionally skipped here):

namespace Modera\NotificationBundle\Channels;

use Modera\NotificationBundle\Dispatching\AbstractChannel;
use Modera\NotificationBundle\Dispatching\DeliveryReport;
use Modera\NotificationBundle\Dispatching\NotificationBuilder;
use Psr\Log\LoggerInterface;
use Symfony\Component\Security\Core\User\UserInterface;

class MonologChannel extends AbstractChannel
{
    private $logger;

    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    public function getId()
    {
        return 'monolog';
    }

    public function dispatch(NotificationBuilder $builder, DeliveryReport $report)
    {
        $usernames = [];
        foreach ($builder->getRecipients() as $user) {
            /* @var UserInterface $user */
            $usernames[] = $user->getUsername();
        }

        try {
            $message = sprintf(
                'Notification with contents "%s" dispatched for %d users: %s.',
                $builder->getMessage(),
                count($usernames),
                implode(', ', $usernames)
            );

            $this->logger->info($message, $builder->getMeta());

            $report->markDelivered($this);
        } catch (\Exception $e) {
            $report->markFailed($this, $e->getMessage());
        }
    }
}

The channel's implementation is pretty much self-explanatory, the only thing that I want you to pay attention is how we invoke $report's markDelivered and markFailed method depending if channel has succeeded or not. Once channel is created there is one more step we need to complete to make our notification center see that there's a new channel is available. For this to happen we need to create a contribution to modera_notification.channels extension-point:

namespace Modera\NotificationBundle\Contributions;

use Modera\NotificationBundle\Dispatching\ChannelInterface;
use Sli\ExpanderBundle\Ext\ContributorInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Modera\NotificationBundle\Channels\MonologChannel;

class ChannelsProvider implements ContributorInterface
{
    private $container;

    private $channels;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    public function getItems(): array
    {
        if (!$this->channels) {
            $this->channels = [
                new MonologChannel($this->container->get('logger'))
            ];
        }

        return $this->channels;
    }
}

And register our contributor class in dependency injection container:

<service id="modera_notification.contributions.channels_provider"
         class="Modera\NotificationBundle\Contributions\ChannelsProvider">

    <argument type="service" id="service_container" />

    <tag name="modera_notification.channels_provider" />
</service>

As of now if you try to dispatch a new notification then the notification channel should be able to discover our monolog channel and to use it as well to deliver notifications, you can try it yourself by running in console something like:

echo "" > app/logs/dev.log && app/console modera:notification:send-notification "hello test" test_group "*"

And then checking contents of app/logs/dev.log file.

Licensing

This bundle is under the MIT license. See the complete license in the bundle: Resources/meta/LICENSE


All versions of notification-bundle with dependencies

PHP Build Version
Package Version
Requires php Version >=5.6
doctrine/orm Version ^2.4
doctrine/doctrine-bundle Version ^1.4|^2.0
symfony/framework-bundle Version ~2.8|^3.0|^4.0|^5.0
sergeil/expander-bundle Version ^1.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package modera/notification-bundle contains the following files

Loading the files please wait ....