Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
79.79% |
75 / 94 |
|
20.00% |
1 / 5 |
CRAP | |
0.00% |
0 / 1 |
| Notification | |
79.79% |
75 / 94 |
|
20.00% |
1 / 5 |
27.37 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
| initQueueTransmission | |
90.00% |
18 / 20 |
|
0.00% |
0 / 1 |
8.06 | |||
| sendQueueItem | |
94.12% |
16 / 17 |
|
0.00% |
0 / 1 |
5.01 | |||
| getValidMailer | |
84.21% |
16 / 19 |
|
0.00% |
0 / 1 |
4.06 | |||
| readMailer | |
56.67% |
17 / 30 |
|
0.00% |
0 / 1 |
5.30 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * |
| 5 | * @package Zmsmessaging |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | namespace BO\Zmsmessaging; |
| 10 | |
| 11 | use BO\Zmsentities\Ics; |
| 12 | use BO\Zmsentities\Mimepart; |
| 13 | use PHPMailer\PHPMailer\PHPMailer; |
| 14 | use PHPMailer\PHPMailer\Exception as PHPMailerException; |
| 15 | |
| 16 | class Notification extends BaseController |
| 17 | { |
| 18 | protected $messagesQueue = null; |
| 19 | |
| 20 | public function __construct($verbose = false, $maxRunTime = 50) |
| 21 | { |
| 22 | parent::__construct($verbose, $maxRunTime); |
| 23 | $this->log( |
| 24 | "Read Notification QueueList start with limit " . \App::$mails_per_minute . " - " . \App::$now->format('c') |
| 25 | ); |
| 26 | $queueList = \App::$http->readGetResult('/notification/')->getCollection(); |
| 27 | if (null !== $queueList) { |
| 28 | $this->messagesQueue = $queueList->sortByCustomKey('createTimestamp'); |
| 29 | $this->log("QueueList sorted by createTimestamp - " . \App::$now->format('c')); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | public function initQueueTransmission($action = false) |
| 34 | { |
| 35 | $resultList = []; |
| 36 | if ($this->messagesQueue && count($this->messagesQueue)) { |
| 37 | foreach ($this->messagesQueue as $item) { |
| 38 | if ($this->maxRunTime < $this->getSpendTime()) { |
| 39 | $this->log("Max Runtime exceeded - " . \App::$now->format('c')); |
| 40 | break; |
| 41 | } |
| 42 | try { |
| 43 | $resultList[] = $this->sendQueueItem($action, $item); |
| 44 | } catch (\Exception $exception) { |
| 45 | $log = new Mimepart(['mime' => 'text/plain']); |
| 46 | $log->content = $exception->getMessage(); |
| 47 | if (isset($item['process']) && isset($item['process']['id'])) { |
| 48 | $this->log("Init Queue Exception message: " . $log->content . ' - ' . \App::$now->format('c')); |
| 49 | $this->log("Init Queue Exception log readPostResult start - " . \App::$now->format('c')); |
| 50 | \App::$http->readPostResult('/log/process/' . $item['process']['id'] . '/', $log, ['error' => 1]); |
| 51 | $this->log("Init Queue Exception log readPostResult finished - " . \App::$now->format('c')); |
| 52 | } |
| 53 | \App::$log->error($log->content); |
| 54 | } |
| 55 | } |
| 56 | } else { |
| 57 | $resultList[] = array( |
| 58 | 'errorInfo' => 'No notification entry found in Database...' |
| 59 | ); |
| 60 | } |
| 61 | return $resultList; |
| 62 | } |
| 63 | |
| 64 | public function sendQueueItem($action, $item) |
| 65 | { |
| 66 | $result = []; |
| 67 | $entity = new \BO\Zmsentities\Notification($item); |
| 68 | $mailer = $this->getValidMailer($entity); |
| 69 | if (! $mailer) { |
| 70 | throw new \Exception("No valid mailer"); |
| 71 | } |
| 72 | $result = $this->sendMailer($entity, $mailer, $action); |
| 73 | if ($result instanceof PHPMailer) { |
| 74 | $result = array( |
| 75 | 'id' => ($result->getLastMessageID()) ? $result->getLastMessageID() : $entity->id, |
| 76 | 'recipients' => $result->getAllRecipientAddresses(), |
| 77 | 'mime' => $result->getMailMIME(), |
| 78 | 'identification' => $result->FromName, |
| 79 | 'subject' => $result->Subject |
| 80 | ); |
| 81 | if ($action) { |
| 82 | $this->deleteEntityFromQueue($entity); |
| 83 | } |
| 84 | } else { |
| 85 | // @codeCoverageIgnoreStart |
| 86 | $result = array( |
| 87 | 'errorInfo' => $result->ErrorInfo |
| 88 | ); |
| 89 | // @codeCoverageIgnoreEnd |
| 90 | } |
| 91 | return $result; |
| 92 | } |
| 93 | |
| 94 | protected function getValidMailer(\BO\Zmsentities\Notification $entity) |
| 95 | { |
| 96 | $message = ''; |
| 97 | $messageId = $entity['id']; |
| 98 | try { |
| 99 | $mailer = $this->readMailer($entity); |
| 100 | // @codeCoverageIgnoreStart |
| 101 | } catch (PHPMailerException $exception) { |
| 102 | $message = "Message #$messageId PHPMailer Failure: " . $exception->getMessage(); |
| 103 | \App::$log->warning($message, []); |
| 104 | } catch (\Exception $exception) { |
| 105 | $message = "Message #$messageId Failure: " . $exception->getMessage(); |
| 106 | \App::$log->warning($message, []); |
| 107 | } |
| 108 | if ($message) { |
| 109 | $this->removeEntityOlderThanOneHour($entity); |
| 110 | $log = new Mimepart(['mime' => 'text/plain']); |
| 111 | $log->content = $message; |
| 112 | $this->log("Build Mailer Exception log message: " . $message); |
| 113 | $this->log("Build Mailer Exception log readPostResult start - " . \App::$now->format('c')); |
| 114 | \App::$http->readPostResult('/log/process/' . $entity->process['id'] . '/', $log, ['error' => 1]); |
| 115 | $this->log("Build Mailer Exception log readPostResult finished - " . \App::$now->format('c')); |
| 116 | return false; |
| 117 | } |
| 118 | return $mailer; |
| 119 | } |
| 120 | |
| 121 | protected function readMailer(\BO\Zmsentities\Notification $entity) |
| 122 | { |
| 123 | $config = \App::$http->readGetResult('/config/')->getEntity(); |
| 124 | $this->testEntity($entity); |
| 125 | $sender = $entity->getIdentification(); |
| 126 | $from = $sender ? $sender : $entity['department']['email']; |
| 127 | $mailer = new Mailer(true); |
| 128 | $message = trim($entity->getMessage()); |
| 129 | $mailer->CharSet = 'UTF-8'; |
| 130 | $mailer->Encoding = 'base64'; |
| 131 | $mailer->SetLanguage('de'); |
| 132 | $mailer->SetFrom($from, $sender); |
| 133 | $mailer->AddAddress($entity->getRecipient($config->getPreference('notifications', 'smsGatewayUrl'))); |
| 134 | $mailer->Subject = $message; |
| 135 | $mailer->Body = ''; |
| 136 | $mailer->AllowEmpty = true; |
| 137 | $mailer->XMailer = \App::IDENTIFIER; |
| 138 | |
| 139 | if (\App::$smtp_enabled) { |
| 140 | $mailer->IsSMTP(); |
| 141 | $mailer->SMTPAuth = \App::$smtp_auth_enabled; |
| 142 | $mailer->SMTPSecure = \App::$smtp_auth_method; |
| 143 | $mailer->Port = \App::$smtp_port; |
| 144 | $mailer->Host = \App::$smtp_host; |
| 145 | $mailer->Username = \App::$smtp_username; |
| 146 | $mailer->Password = \App::$smtp_password; |
| 147 | if (\App::$smtp_skip_tls_verify) { |
| 148 | $mailer->SMTPOptions['ssl'] = [ |
| 149 | 'verify_peer' => false, |
| 150 | 'verify_peer_name' => false, |
| 151 | 'allow_self_signed' => true, |
| 152 | ]; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | return $mailer; |
| 157 | } |
| 158 | } |