Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 14
iaErrorReporterDisplay
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 3
210.00
0.00% covered (danger)
0.00%
0 / 14
 reportErrors
0.00% covered (danger)
0.00%
0 / 1
20.00
0.00% covered (danger)
0.00%
0 / 5
 pageDisplayErrors
n/a
0 / 0
2
n/a
0 / 0
 emailSend
n/a
0 / 0
3
n/a
0 / 0
 ramUsage
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 6
 timeUsage
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 3
<?php
namespace ia\Lib;
/**
 * @author Informática Asocaida SA de CV
 * @version 1.0.0
 * @copyright 2017
 */
/**
 * iaErrorReporterDisplay.
 *
 * Display and email errors collected by iacErrorReporter
 *
 */
class iaErrorReporterDisplay {
    /**
     * report errors to screen
     *
     * @param bool $display
     * @param string $emailTo
     * @param string $html
     * @param array $count
     * @param string $info
     * @return void
     */
    public function reportErrors($display, $emailTo, $html, $count, $info) {
        if($display) {
            $this->pageDisplayErrors($html,$count['Bug'] + $count['newbug'] +$count['Fixed'] , $info);
        }
        if( !empty($emailTo) && ($count['newbug'] + $count['Fixed']) > 0) {
            $this->emailSend($emailTo, $html,$count);
        }
    }
    /**
     * Display errors in html
     *
     *
     * @codeCoverageIgnore
     *
     * @param string $html
     * @param int $count total number of errors
     * @param string $info additional html to output
     * @return void
     */
    protected function pageDisplayErrors($html,$count,$info) {
        $style = $count == 0 ? '' : ' style="color:red" ';
        echo "
<script>
function iacHtmlDetialsClick(e) {e.nextElementSibling.style.display=(e.nextElementSibling.style.display=='none' ? 'block':'none');e.firstElementChild.innerHTML=(e.nextElementSibling.style.display=='none' ? '&#8680;' : '&#8681;');}
</script>
<div class='iacDetailsSeparator np'></div><div class='iacDetailsByDiv np'>
<span style='cursor:pointer;color:darkgreen;background-color:white;' title='Click to view' onclick=\"iacHtmlDetialsClick(this);\">
<span>&#8680;</span> <span$style>Page Info ($count)</span>
</span>
<div style='display:none;border:1px silver solid;background-color:white;padding:0 1em 1em 1em;margin:0 1em auto 1em;'>
".$this->ramUsage().$this->timeUsage()."<ol style='color:red;'>$html</ol>$info
</div></div>
";
    }
    /**
     * Send email with errors
     *
     * @codeCoverageIgnore
     *
     * @param string $error
     * @param string $html
     * @param array $count
     * @return void
     */
    protected function emailSend($emailTo, $html, $count) {
       $subject = $_SERVER["HTTP_HOST"]." iac detected Errors.";
       if($count['Fixed']>0)
        $subject.=" $count[Fixed] Unfixed errors.";
       if($count['newbug']>0)
        $subject.=" $count[newbug] New errors.";
       @mail($emailTo,
            $subject,
            "<html><body><p>IacErrorReporter reporting:<p><ul><li>".$_SERVER["HTTP_HOST"]."<li>".PHP_VERSION."<li>".php_uname().
                "<li><ul style='color:red;'>".$html."</ul></ul>".$this->ramUsage().$this->timeUsage()."</body></html>"
        );
    }
    /**
     * Reports memory usage
     *
     * @return string a div with memory usage information
     */
    protected function ramUsage() {
        // just in case, defensive programming
        if(!function_exists('memory_get_usage') || !function_exists('memory_get_peak_usage')) {
            return '';
        }
        return "<div style='font-family:courier;margin-left:16px;'>"
            ."Used: ".FormatIt::bytes2units( memory_get_usage() )." Real: ".FormatIt::bytes2units( memory_get_usage(true) )
            ." / "
            ."Peak Used: ".FormatIt::bytes2units( memory_get_peak_usage() )." Real: ".FormatIt::bytes2units( memory_get_peak_usage(true) )
            ."</div>"
            ;
    }
    /**
     * Reports current timing from $_SERVER['REQUEST_TIME'] to current time
     *
     * @return string html code with timing information
     */
    protected function timeUsage() {
        if(!array_key_exists('REQUEST_TIME',$_SERVER)) {
            return '';
        }
        return "<div style='font-family:courier;margin-left:16px;'>"."Total: ".iaLib::milliSecondsFormat(abs( microtime(true) - $_SERVER['REQUEST_TIME'] ))."</div>";
    }
}