Skip to content

Instantly share code, notes, and snippets.

@micmania1
Last active November 24, 2016 08:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save micmania1/96b0b98ad350a8c8fc2dbee172d54c44 to your computer and use it in GitHub Desktop.
Save micmania1/96b0b98ad350a8c8fc2dbee172d54c44 to your computer and use it in GitHub Desktop.
Notifier
Injector:
SlackNotifier:
factory: 'SlackNotificationFactory'
<?php
use SilverStripe\Framework\Injector\Factory;
/**
* Creates a SlackNotifier from constants if they're set. If not, then it will
* return a NullNotifier.
*
* This means we don't have to have constant "defined" checks anywhere we need to use slack.
*/
class SlackNotificationFactory implements Factory {
/**
* {@inheritdoc}
*
* @return Notifier
*/
public function create($service, array $params = []) {
if(defined('DEPLOYMENT_BACKEND_ALERT_URI') && defined('DEPLOYMENT_BACKEND_ALERT_CHANNEL')) {
$username = defined('DEPLOYMENT_BACKEND_USERNAME') ? DEPLOYMENT_BACKEND_USERNAME : 'Deploynaut';
return new SlackNotifier(
DEPLOYMENT_BACKEND_ALERT_URI,
DEPLOYMENT_BACKEND_ALERT_CHANNEL,
$username
);
}
return new NullNotifier();
}
}
<?php
// SlackNotifer and NullNotifier both implement a comment 'Notifier' interface
//
// In live or in testing, wherever slack constants are defined, we will get
// an instance of SlackNotifier which will notify to the specified channel.
//
// In dev, we can omit these constants which means we get an instance of
// NullNotifier which notifies into nothing.
$notifier = SlackNotificerFactory::create();
// or...
$notifier = Injector::inst()->get('SlackNotifier');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment