-
-
Save micmania1/96b0b98ad350a8c8fc2dbee172d54c44 to your computer and use it in GitHub Desktop.
Notifier
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Injector: | |
SlackNotifier: | |
factory: 'SlackNotificationFactory' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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