Skip to content

vraginya/notifier-bundle

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

notifier-bundle

Symfony 4 bundle for sending notifications

Requirements

  • Php 7.4
  • Symfony 4 application

Installation

  1. Add a repository to the repositories section in your composer.json file
"repositories": [
    {
        "type": "vcs",
        "url": "https://github.com/vraginya/notifier-bundle.git"
    }
]
  1. Add the bundle to your project :
composer require vrag/notifier:dev-master

Set up

  1. Put v_rag_notifier.yaml file to the directory config/packages in yor symfony 4 application:
v_rag_notifier:

  email:
    # Set the sms transport service
    transport: 'vrag_notifier.mailer_transport'

  sms:
    # Set the sms transport service (live blank if not required)
    transport: 'vrag_notifier.twilio_transport'

  twilio:
    # Twilio SID (live blank if not required)
    sid: '%env(TWILIO_SID)%'
    # Twilio Token (live blank if not required)
    token: '%env(TWILIO_TOKEN)%'
  1. Define the bundle in the config/bundles.php file:
<?php

return [
    //~~~
    VRag\NotifierBundle\VRagNotifierBundle::class => ['all' => true],
    //~~~
];

Basic usage

Fluent interface

<?php

namespace App\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use VRag\NotifierBundle\Service\Manager;
use VRag\NotifierBundle\Service\Notification;

class TestCommand extends Command
{
    protected static $defaultName = 'Test';

    private $manager;
    
    // Inject the Manager service
    public function __construct(Manager $manager)
    {
        $this->manager = $manager;

        parent::__construct();
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $notification = new Notification('HELLO WORLD!');

        //Send email and sms
        $operator = $this->manager
            ->getOperator(Manager::EMAIL)
            ->setNotification($notification)
            ->setRecipients('[email protected]')
            ->setOptions([
                'subject' => 'Test subject',
                'from' => '[email protected]',
            ])
            ->getOperator(Manager::SMS)
            ->setNotification($notification)
            ->setRecipients('+12345555555')
            ->setOptions(['from' => '+12344444444']);

        $operator->deliver();

        $output->write(print_r($operator->getErrors()));

        return 0;
    }
}

Using getOperator method with parameters

<?php
    // ***
    $notification = new Notification('HELLO WORLD!');
    $this->manager->getOperator(
        Manager::EMAIL,
        $notification,
        ['[email protected]','[email protected]'],
        [
            'subject' => 'Test subject',
            'from' => '[email protected]',
            'cc' => '[email protected]'
        ]
    )->deliver();

    $operator = $this->manager->getOperator(
        Manager::SMS,
        $notification,
        '+12345555555',
        ['from' => '+12344444444']
    );
    $operator->deliver();
    // ***

Building notification with Twig

    // Autowire
    // VRag\NotifierBundle\Service\Manager;
    // VRag\NotifierBundle\Service\Builder\TwigNotificationBuilder;
    public function testAction(Manager $manager, TwigNotificationBuilder $builder): int
    {
        $notification = $this->builder->build('email.html.twig', [
            'param1' => 'Hello', 
            'param2' => 'World'
        ]);

        $this->manager
            ->getOperator(Manager::EMAIL)
            ->setNotification($notification)
            ->setRecipients(['[email protected]','[email protected]'])
            ->setOptions([
                'subject' => 'Test subject',
                'from' => '[email protected]',
            ])
            ->deliver();
    }

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages