Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
48.10% |
38 / 79 |
|
54.55% |
6 / 11 |
CRAP | |
0.00% |
0 / 1 |
| BaseController | |
48.10% |
38 / 79 |
|
54.55% |
6 / 11 |
217.17 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| getLogList | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| clearLogList | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getSpendTime | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| sendMailer | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
6 | |||
| removeEntityOlderThanOneHour | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| deleteEntityFromQueue | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
| testEntity | |
39.13% |
9 / 23 |
|
0.00% |
0 / 1 |
22.43 | |||
| monitorProcesses | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
56 | |||
| log | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
3.02 | |||
| convertCollectionToArray | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * |
| 5 | * @package Zmsmessaging |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | namespace BO\Zmsmessaging; |
| 10 | |
| 11 | use BO\Zmsentities\Mail; |
| 12 | use BO\Zmsentities\Notification; |
| 13 | use BO\Zmsentities\Mimepart; |
| 14 | use BO\Mellon\Validator; |
| 15 | |
| 16 | class BaseController |
| 17 | { |
| 18 | protected $verbose = false; |
| 19 | protected static $logList = []; |
| 20 | protected $workstation = null; |
| 21 | protected $startTime; |
| 22 | protected $maxRunTime = 50; |
| 23 | |
| 24 | public function __construct($verbose = false, $maxRunTime = 50) |
| 25 | { |
| 26 | $this->verbose = $verbose; |
| 27 | $this->startTime = microtime(true); |
| 28 | $this->maxRunTime = $maxRunTime; |
| 29 | } |
| 30 | |
| 31 | public static function getLogList() |
| 32 | { |
| 33 | return static::$logList; |
| 34 | } |
| 35 | |
| 36 | public static function clearLogList() |
| 37 | { |
| 38 | static::$logList = []; |
| 39 | } |
| 40 | |
| 41 | protected function getSpendTime() |
| 42 | { |
| 43 | $time = round(microtime(true) - $this->startTime, 3); |
| 44 | return $time; |
| 45 | } |
| 46 | |
| 47 | protected function sendMailer(\BO\Zmsentities\Schema\Entity $entity, $mailer = null, $action = false) |
| 48 | { |
| 49 | // @codeCoverageIgnoreStart |
| 50 | $hasSendSuccess = ($action) ? $mailer->Send() : $action; |
| 51 | if (false !== $action && null !== $mailer && ! $hasSendSuccess) { |
| 52 | $this->log("Exception: SendingFailed - " . \App::$now->format('c')); |
| 53 | throw new Exception\SendingFailed(); |
| 54 | } |
| 55 | // @codeCoverageIgnoreEnd |
| 56 | $log = new Mimepart(['mime' => 'text/plain']); |
| 57 | $log->content = ($entity instanceof Mail) ? $entity->subject : $entity->message; |
| 58 | \App::$http->readPostResult('/log/process/' . $entity->process['id'] . '/', $log); |
| 59 | return $mailer; |
| 60 | } |
| 61 | |
| 62 | protected function removeEntityOlderThanOneHour($entity) |
| 63 | { |
| 64 | if (3600 < \App::$now->getTimestamp() - $entity->createTimestamp) { |
| 65 | $this->deleteEntityFromQueue($entity); |
| 66 | $log = new Mimepart(['mime' => 'text/plain']); |
| 67 | $log->content = 'Zmsmessaging Failure: Queue entry older than 1 hour has been removed'; |
| 68 | \App::$http->readPostResult('/log/process/' . $entity->process['id'] . '/', $log, ['error' => 1]); |
| 69 | \App::$log->warning($log->content); |
| 70 | return false; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | public function deleteEntityFromQueue($entity) |
| 75 | { |
| 76 | $type = ($entity instanceof \BO\Zmsentities\Mail) ? 'mails' : 'notification'; |
| 77 | try { |
| 78 | $entity = \App::$http->readDeleteResult('/' . $type . '/' . $entity->id . '/')->getEntity(); |
| 79 | } catch (\BO\Zmsclient\Exception $exception) { |
| 80 | throw $exception; |
| 81 | } |
| 82 | return ($entity) ? true : false; |
| 83 | } |
| 84 | |
| 85 | public function testEntity($entity) |
| 86 | { |
| 87 | if (!isset($entity['department'])) { |
| 88 | throw new \Exception("Could not resolve department for message " . $entity['id']); |
| 89 | } |
| 90 | if (!isset($entity['department']['email'])) { |
| 91 | throw new \Exception( |
| 92 | "No mail address for department " |
| 93 | . $entity['department']['name'] |
| 94 | . " (departmentID=" |
| 95 | . $entity['department']['id'] |
| 96 | . " Vorgang=" |
| 97 | . $entity['process']['id'] |
| 98 | . ") " |
| 99 | . $entity['id'] |
| 100 | ); |
| 101 | } |
| 102 | if (! $entity->hasContent()) { |
| 103 | throw new \BO\Zmsmessaging\Exception\MailWithoutContent(); |
| 104 | } |
| 105 | if ($entity instanceof Mail) { |
| 106 | $isMail = Validator::value($entity->getRecipient())->isMail()->getValue(); |
| 107 | if (!$isMail) { |
| 108 | throw new \BO\Zmsmessaging\Exception\InvalidMailAddress(); |
| 109 | } |
| 110 | if (\App::$verify_dns_enabled) { |
| 111 | $hasDns = Validator::value($entity->getRecipient())->isMail()->hasDNS()->getValue(); |
| 112 | if (!$hasDns) { |
| 113 | throw new \BO\Zmsmessaging\Exception\InvalidMailAddress(); |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | protected function monitorProcesses($processHandles) |
| 120 | { |
| 121 | $running = true; |
| 122 | while ($running) { |
| 123 | $running = false; |
| 124 | foreach ($processHandles as &$handle) { |
| 125 | if (is_resource($handle['process'])) { |
| 126 | $status = proc_get_status($handle['process']); |
| 127 | if ($status['running']) { |
| 128 | $running = true; |
| 129 | } else { |
| 130 | $output = stream_get_contents($handle['pipes'][1]); // stdout |
| 131 | $errorOutput = stream_get_contents($handle['pipes'][2]); // stderr |
| 132 | fclose($handle['pipes'][1]); |
| 133 | fclose($handle['pipes'][2]); |
| 134 | if (trim($output)) { |
| 135 | $this->log("\nProcess stdout: " . trim($output) . "\n"); |
| 136 | } |
| 137 | if (trim($errorOutput)) { |
| 138 | $this->log("\nProcess stderr: " . trim($errorOutput) . "\n"); |
| 139 | } |
| 140 | |
| 141 | proc_close($handle['process']); |
| 142 | $handle['process'] = null; |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | usleep(500000); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | public function log($message) |
| 154 | { |
| 155 | if (is_array($message)) { |
| 156 | $message = print_r($message, true); |
| 157 | } |
| 158 | |
| 159 | $time = $this->getSpendTime(); |
| 160 | $memory = memory_get_usage() / (1024 * 1024); |
| 161 | $text = sprintf("[MailProcessor log %07.3fs %07.1fmb] %s", $time, $memory, $message); |
| 162 | |
| 163 | if ($this->verbose) { |
| 164 | //error_log($text); |
| 165 | } |
| 166 | |
| 167 | // Explicitly flush the output buffer |
| 168 | echo $text . "\n"; |
| 169 | flush(); |
| 170 | } |
| 171 | |
| 172 | protected function convertCollectionToArray($collection) |
| 173 | { |
| 174 | $this->log("Converting collection to array"); |
| 175 | $array = []; |
| 176 | foreach ($collection as $item) { |
| 177 | $array[] = $item; |
| 178 | } |
| 179 | $this->log("Conversion complete, array size: " . count($array)); |
| 180 | return $array; |
| 181 | } |
| 182 | } |