Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
50.57% covered (warning)
50.57%
44 / 87
45.45% covered (danger)
45.45%
5 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
BaseController
50.57% covered (warning)
50.57%
44 / 87
45.45% covered (danger)
45.45%
5 / 11
182.91
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
84.62% covered (warning)
84.62%
11 / 13
0.00% covered (danger)
0.00%
0 / 1
3.03
 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\Zmsentities\Schema\Entity;
14use BO\Mellon\Validator;
15
16class 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(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        if (!($entity instanceof Mail)) {
77            return false;
78        }
79        try {
80            $entity = \App::$http
81                ->readDeleteResult('/mails/' . $entity->id . '/')
82                ->getEntity();
83        } catch (\BO\Zmsclient\Exception $exception) {
84            throw $exception;
85        }
86
87        return (bool) $entity;
88    }
89
90    public function testEntity($entity)
91    {
92        if (!isset($entity['department'])) {
93            throw new \Exception("Could not resolve department for message " . $entity['id']);
94        }
95        if (!isset($entity['department']['email'])) {
96            throw new \Exception(
97                "No mail address for department "
98                . $entity['department']['name']
99                . " (departmentID="
100                . $entity['department']['id']
101                . " Vorgang="
102                . $entity['process']['id']
103                . ") "
104                . $entity['id']
105            );
106        }
107        if (! $entity->hasContent()) {
108            throw new \BO\Zmsmessaging\Exception\MailWithoutContent();
109        }
110        if ($entity instanceof Mail) {
111            $isMail = Validator::value($entity->getRecipient())->isMail()->getValue();
112            if (!$isMail) {
113                throw new \BO\Zmsmessaging\Exception\InvalidMailAddress();
114            }
115            if (\App::$verify_dns_enabled) {
116                $hasDns = Validator::value($entity->getRecipient())->isMail()->hasDNS()->getValue();
117                if (!$hasDns) {
118                    throw new \BO\Zmsmessaging\Exception\InvalidMailAddress();
119                }
120            }
121        }
122    }
123
124    protected function monitorProcesses($processHandles)
125    {
126        $running = true;
127        while ($running) {
128            $running = false;
129            foreach ($processHandles as &$handle) {
130                if (is_resource($handle['process'])) {
131                    $status = proc_get_status($handle['process']);
132                    if ($status['running']) {
133                        $running = true;
134                    } else {
135                        $output = stream_get_contents($handle['pipes'][1]);  // stdout
136                        $errorOutput = stream_get_contents($handle['pipes'][2]);  // stderr
137                        fclose($handle['pipes'][1]);
138                        fclose($handle['pipes'][2]);
139                        if (trim($output)) {
140                            $this->log("\nProcess stdout: " . trim($output) . "\n");
141                        }
142                        if (trim($errorOutput)) {
143                            $this->log("\nProcess stderr: " . trim($errorOutput) . "\n");
144                        }
145
146                        proc_close($handle['process']);
147                        $handle['process'] = null;
148                    }
149                }
150            }
151            usleep(500000);
152        }
153    }
154
155
156
157
158    public function log($message)
159    {
160        if (is_array($message)) {
161            $message = print_r($message, true);
162        }
163
164        $time = $this->getSpendTime();
165        $memory = memory_get_usage() / (1024 * 1024);
166        static::$logList[] = $message;
167
168        $context = [
169            'component' => 'zmsmessaging',
170            'elapsed' => $time,
171            'memory_mb' => $memory,
172        ];
173        if ($this->verbose) {
174            \App::$log->debug($message, $context);
175        } else {
176            \App::$log->info($message, $context);
177        }
178    }
179
180    protected function convertCollectionToArray($collection)
181    {
182        $this->log("Converting collection to array");
183        $array = [];
184        foreach ($collection as $item) {
185            $array[] = $item;
186        }
187        $this->log("Conversion complete, array size: " . count($array));
188        return $array;
189    }
190}