diff --git a/public_html/deployment/classes/messenger.class.php b/public_html/deployment/classes/messenger.class.php index f888cefbadd64ea35d841a27788db0d6f4f80525..3e8caac7378a1b6856a3be6bb5450dad0486847f 100644 --- a/public_html/deployment/classes/messenger.class.php +++ b/public_html/deployment/classes/messenger.class.php @@ -4,70 +4,80 @@ * 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, + $this->_aCfg['email']['to'], + $sSubject, + $this->_sMessage, "From: " . $this->_aCfg['email']['from'] . "\r\n" . "Reply-To: " . $this->_aCfg['email']['from'] . "\r\n" ); } return false; } - + /** - * 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); + $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) { - $this->_sMessage=$sMessage; + public function sendMessage(string $sMessage): void + { + $this->_sMessage = $sMessage; // echo '<pre>'.print_r($this->_aCfg, 1).'</pre>'.$sMessage.'<br>'; $this->_sendEmail(); $this->_sendToSlack(); - } }