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