Subversion Repositories oidplus

Rev

Rev 1189 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * OIDplus 2.0
  5.  * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft
  6.  *
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  *
  11.  *     http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS,
  15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  * See the License for the specific language governing permissions and
  17.  * limitations under the License.
  18.  */
  19.  
  20. namespace ViaThinkSoft\OIDplus;
  21.  
  22. // phpcs:disable PSR1.Files.SideEffects
  23. \defined('INSIDE_OIDPLUS') or die;
  24. // phpcs:enable PSR1.Files.SideEffects
  25.  
  26. class OIDplusNotification extends OIDplusBaseClass {
  27.  
  28.         /**
  29.          * @var string
  30.          */
  31.         private $severity;
  32.  
  33.         /**
  34.          * @var string
  35.          */
  36.         private $message; // TODO: Rename this to $htmlMessage everywhere
  37.  
  38.         /**
  39.          * @param string $severity One of OK, INFO, WARN, ERR, or CRIT
  40.          * @param string $message
  41.          * @throws OIDplusException
  42.          */
  43.         public function __construct(string $severity, string $message) {
  44.                 // Same severities as the log plugin (also same CSS classes)
  45.                 if (($severity != 'OK') && ($severity != 'INFO') && ($severity != 'WARN') && ($severity != 'ERR') && ($severity != 'CRIT')) {
  46.                         throw new OIDplusException(_L('Invalid severity "%1"', $severity));
  47.                 }
  48.  
  49.                 $this->severity = $severity;
  50.                 $this->message = $message;
  51.         }
  52.  
  53.         /**
  54.          * @return int
  55.          */
  56.         public function getSeverityAsInt(): int {
  57.                 if      ($this->severity == 'OK')   return 1; // (this makes no sense)
  58.                 else if ($this->severity == 'INFO') return 2;
  59.                 else if ($this->severity == 'WARN') return 3;
  60.                 else if ($this->severity == 'ERR')  return 4;
  61.                 else if ($this->severity == 'CRIT') return 5;
  62.                 else assert(false);
  63.                 return 0; // otherwise PHPstan complains...
  64.         }
  65.  
  66.         /**
  67.          * @return string
  68.          */
  69.         public function getSeverityAsString(): string {
  70.                 return $this->severity;
  71.         }
  72.  
  73.         /**
  74.          * @param bool $plural
  75.          * @return string
  76.          * @throws OIDplusConfigInitializationException
  77.          * @throws OIDplusException
  78.          */
  79.         public function getSeverityAsHumanFriendlyString(bool $plural): string {
  80.                 if ($plural) {
  81.                         if      ($this->severity == 'OK')   return _L('OK');
  82.                         else if ($this->severity == 'INFO') return _L('Informational');
  83.                         else if ($this->severity == 'WARN') return _L('Warnings');
  84.                         else if ($this->severity == 'ERR')  return _L('Errors');
  85.                         else if ($this->severity == 'CRIT') return _L('Critical issues');
  86.                         else assert(false);
  87.                 } else {
  88.                         if      ($this->severity == 'OK')   return _L('OK');
  89.                         else if ($this->severity == 'INFO') return _L('Informational');
  90.                         else if ($this->severity == 'WARN') return _L('Warning');
  91.                         else if ($this->severity == 'ERR')  return _L('Error');
  92.                         else if ($this->severity == 'CRIT') return _L('Critical issue');
  93.                         else assert(false);
  94.                 }
  95.                 return ''; // otherwise PHPstan complains...
  96.         }
  97.  
  98.         /**
  99.          * @return string
  100.          */
  101.         public function getMessage(): string {
  102.                 // TODO: Rename this method to getHtmlMessage() everywhere
  103.                 return $this->message;
  104.         }
  105.  
  106. }
  107.