Completed
Push — master ( 86fce8...3dc221 )
by Vítězslav
05:51
created

ToEmail::error()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 6
ccs 0
cts 5
cp 0
crap 6
rs 10
1
<?php
2
/**
3
 * Send logs by email
4
 *
5
 * @author    Vitex <[email protected]>
6
 * @copyright 2009-2016 [email protected] (G)
7
 */
8
9
namespace Ease\Logger;
10
11
/**
12
 * Log to Email
13
 *
14
 * @author    Vitex <[email protected]>
15
 * @copyright 2009-2012 [email protected] (G)
16
 */
17
class ToEmail extends ToMemory implements Loggingable
18
{
19
    /**
20
     * Předvolená metoda logování.
21
     *
22
     * @var string
23
     */
24
    public $logType = 'email';
25
26
    /**
27
     * úroveň logování.
28
     *
29
     * @var string - silent,debug
30
     */
31
    public $logLevel = 'debug';
32
33
    /**
34
     * Odkaz na vlastnící objekt.
35
     *
36
     * @var \Ease\Sand
37
     */
38
    public $parentObject = null;
39
40
    /**
41
     * Pole uložených zpráv.
42
     *
43
     * @var array
44
     */
45
    public $statusMessages = [];
46
47
    /**
48
     * ID naposledy ulozene zpravy.
49
     *
50
     * @var int unsigned
51
     */
52
    private $messageID = 0;
53
54
    /**
55
     * Obecné konfigurace frameworku.
56
     *
57
     * @var \Ease\Shared
58
     */
59
    public $easeShared = null;
60
61
    /**
62
     * List of allready flushed messages.
63
     *
64
     * @var array
65
     */
66
    public $flushed = [];
67
68
    /**
69
     * Saves obejct instace (singleton...).
70
     */
71
    private static $_instance = null;
72
73
    /**
74
     * Handle to mailer.
75
     *
76
     * @var resource
77
     */
78
    public $mailer = null;
79
80
    /**
81
     * Recipient
82
     * @var string
83
     */
84
    public $recipient = null;
85
86
    /**
87
     * Log Subject
88
     * @var string
89
     */
90
    public $subject = null;
91
92
    /**
93
     * Logger to mail Class
94
     *
95
     * @param string $recipient
96
     * @param string $subject of message
97
     */
98
    public function __construct($recipient = null, $subject = null)
99
    {
100
        $this->recipient = $recipient;
101
        $this->subject   = empty($subject) ? $_SERVER['PHP_SELF'] : $subject;
102
    }
103
104
    /**
105
     * Pri vytvareni objektu pomoci funkce singleton (ma stejne parametry, jako
106
     * konstruktor) se bude v ramci behu programu pouzivat pouze jedna jeho
107
     * instance (ta prvni).
108
     *
109
     * @link http://docs.php.net/en/language.oop5.patterns.html Dokumentace a
110
     * priklad
111
     */
112
    public static function singleton()
113
    {
114
        if (!isset(self::$_instance)) {
115
            $class = __CLASS__;
116
            if (defined('EASE_APPNAME')) {
117
                self::$_instance = new $class(constant('EASE_EMAILTO'),
118
                    constant('EASE_APPNAME'));
119
            } else {
120
                self::$_instance = new $class('EaseFramework');
121
            }
122
        }
123
124
        return self::$_instance;
125
    }
126
127
    /**
128
     * Zapise zapravu do logu.
129
     *
130
     * @param string $caller  název volajícího objektu
131
     * @param string $message zpráva
132
     * @param string $type    typ zprávy (success|info|error|warning|*)
133
     *
134
     * @return null|boolean byl report zapsán ?
135
     */
136
    public function addToLog($caller, $message, $type = 'notice')
137
    {
138
139
        if (!is_object($this->mailer)) {
0 ignored issues
show
introduced by
The condition is_object($this->mailer) is always false.
Loading history...
140
            $this->mailer = new \Ease\Mailer($this->recipient, $this->subject);
0 ignored issues
show
Documentation Bug introduced by
It seems like new Ease\Mailer($this->recipient, $this->subject) of type Ease\Mailer is incompatible with the declared type resource of property $mailer.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
141
            $this->mailer->setUserNotification(false);
142
        }
143
144
        ++$this->messageID;
145
        if (($this->logLevel == 'silent') && ($type != 'error')) {
146
            return;
147
        }
148
        if (($this->logLevel != 'debug') && ($type == 'debug')) {
149
            return;
150
        }
151
152
        $this->statusMessages[$type][$this->messageID] = $message;
153
154
        $logLine = strftime("%D %T").' `'.$caller.'`: '.$message;
155
156
        $this->mailer->textBody .= "\n" . $logLine;
157
        return true;
158
    }
159
160
    /**
161
     * Send collected messages.
162
     */
163
    public function __destruct()
164
    {
165
        if (is_object($this->mailer) && ( $this->mailer->getItemsCount() > 0 )) {
0 ignored issues
show
introduced by
The condition is_object($this->mailer) is always false.
Loading history...
166
            $this->mailer->send();
167
        }
168
    }
169
170
    /**
171
     * Flush Messages.
172
     *
173
     * @param string $caller
174
     *
175
     * @return int how many messages was flushed
176
     */
177
    public function flush($caller = null)
178
    {
179
        $flushed = 0;
180
        if (count($this->statusMessages)) {
181
            foreach ($this->statusMessages as $type => $messages) {
182
                foreach ($messages as $messageID => $message) {
183
                    if (!isset($this->flushed[$type][$messageID])) {
184
                        $this->addToLog($caller, $message, $type);
185
                        $this->flushed[$type][$messageID] = true;
186
                        ++$flushed;
187
                    }
188
                }
189
            }
190
        }
191
192
        return $flushed;
193
    }
194
}
195