Skip to content

Latest commit

 

History

History
100 lines (76 loc) · 2.39 KB

notifiers.md

File metadata and controls

100 lines (76 loc) · 2.39 KB

Custom notifier

You are free to create your own notifier. You can send email, SMS or whatever you want.

Email Notifier

namespace App\EventSubscriber;

use BugCatcher\Entity\NotifierEmail;
use BugCatcher\Event\NotifyEvent;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mailer\MailerInterface;

#[AsEventListener]
class EmailNotifyListener {
	
	public function __construct(
		private readonly MailerInterface $mailer
	) {}

	public function __invoke(NotifyEvent $event): void {
		if ($event->notifier instanceof NotifierEmail) {
			if ($event->importance->isHigherOrEqualThan($event->notifier->getMinimalImportance())) {
			    foreach($event->project->getUsers() as $user){
                    $email = (new Email())
                        ->from('[email protected]')
                        ->to($user->getEmail())
                        ->subject("Threshold reached {$event->project->getName()}")
                        ->text('Hey, the threshold was reached!')
                        ->html('<p>Hey, the threshold was reached!</p>');
    
                    $this->mailer->send($email);
				}
			}
		}
	}

}

Custom Notifier

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use BugCatcher\Repository\NotifierRepository;

class NotifierSms extends Notifier {

    #[ORM\Column(type: 'string')]
	private string $telephoneNumber;

	public function getTelephoneNumber(): string {
		return $this->telephoneNumber;
	}

	public function setTelephoneNumber(string $number): self {
		$this->telephoneNumber = $number;

		return $this;
	}

}

See how to oweride entity for more details.

cp vendor/php-bug-catcher/bug-catcher/config/doctrine/Notifier.orm.xml config/doctrine/BugCatcherBundle/Notifier.orm.xml
<!--config/doctrine/BugCatcherBudnle/Notifier.orm.xml-->
<!--...-->
<discriminator-map>
    <!--...-->
    <discriminator-mapping value="sms-notifier" class="App\Entity\NotifierSms"/>
</discriminator-map>
<!--...-->
namespace App\EventSubscriber;

use BugCatcher\Entity\NotifierEmail;
use BugCatcher\Event\NotifyEvent;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;

#[AsEventListener]
class SmsNotifyListener {
	public function __invoke(NotifyEvent $event): void {
		if ($event->notifier instanceof NotifierSms){
			// send sms
		}
	}
}