Skip to content
Snippets Groups Projects
Commit 43b5d42e authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

messenger class: php8 only; added variable types

parent 3c9e168a
Branches
No related tags found
1 merge request!66php8 only; added variable types; short array syntax; remove glyphicons
This commit is part of merge request !66. Comments created here will be created in the context of that merge request.
......@@ -4,53 +4,59 @@
* send messenger notifications
*
* @author hahn
*
* 2024-08-23 v1.1 Axel Hahn php8 only; added variable types
*/
class messenger {
class messenger
{
/**
* config array for messengers
* @var type
* @var array
*/
protected $_aCfg = array();
protected array $_aCfg = [];
/**
* content of messagetext to send
* @var string
*/
protected $_sMessage = '';
protected string $_sMessage = '';
/**
* Constructor
*
* @example
* $oMessenger = new messenger(array(
* 'slack'=>array(
* $oMessenger = new messenger([
* 'slack'=>[
* 'incomingurl'=>[WebHook url],
* 'user'=>[visible username in slack],
* 'icon'=>[Slack Icon], // hm, does not seem to work
* ),
* 'email'=>array(
* ],
* 'email'=>[
* 'from'=>[senders e-mail]
* 'to'=>[email-address(es)] // multiple emails must be delimited with ";"
* )
* ));
* ]
* ]);
* @param array $aCfg config array for notification targets
* @return boolean
*/
public function __construct($aCfg) {
public function __construct(array $aCfg)
{
$this->_aCfg = $aCfg;
return true;
}
/**
* send an email if _aCfg['email']['to'] exists
* Send an email if _aCfg['email']['to'] exists
* @return bool
*/
private function _sendEmail(){
private function _sendEmail(): bool
{
if (isset($this->_aCfg['email']['to']) && $this->_aCfg['email']['to']) {
preg_match('/^(.*)\n/', $this->_sMessage, $aTmp);
$sSubject = $aTmp[0];
return mail(
$this->_aCfg['email']['to'],
$sSubject, $this->_sMessage,
$sSubject,
$this->_sMessage,
"From: " . $this->_aCfg['email']['from'] . "\r\n" .
"Reply-To: " . $this->_aCfg['email']['from'] . "\r\n"
);
......@@ -59,15 +65,19 @@ class messenger {
}
/**
* send a message to slack if _aCfg['slack']['incomingurl'] exists
* Send a message to slack if _aCfg['slack']['incomingurl'] exists
* @return bool|string
*/
private function _sendToSlack(){
private function _sendToSlack(): bool|string
{
if (isset($this->_aCfg['slack']['incomingurl']) && $this->_aCfg['slack']['incomingurl']) {
require_once(__DIR__ . '/../../vendor/shooker/shooker.php');
$shkr = new Shooker();
$shkr->setupIncoming($this->_aCfg['slack']['incomingurl']);
$sUser = (isset($this->_aCfg['slack']['user']) ? $this->_aCfg['slack']['user'] : false);
$sIcon = (isset($this->_aCfg['slack']['icon']) ? $this->_aCfg['slack']['icon'] : false);
// returns bool or string with response from Slack API
return $shkr->sendMessage($this->_sMessage, $sUser, $sIcon);
}
return false;
......@@ -77,13 +87,14 @@ class messenger {
/**
* send a message to all targets
* @param string $sMessage
* @return void
*/
public function sendMessage($sMessage) {
public function sendMessage(string $sMessage): void
{
$this->_sMessage = $sMessage;
// echo '<pre>'.print_r($this->_aCfg, 1).'</pre>'.$sMessage.'<br>';
$this->_sendEmail();
$this->_sendToSlack();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment