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