ToEmail   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 177
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 45
c 2
b 0
f 0
dl 0
loc 177
rs 10
ccs 0
cts 50
cp 0
wmc 17

5 Methods

Rating   Name   Duplication   Size   Complexity  
A flush() 0 16 5
A addToLog() 0 17 5
A __construct() 0 6 2
A singleton() 0 15 3
A __destruct() 0 4 2
1
<?php
2
/**
3
 * Send logs by email
4
 *
5
 * @author    Vitex <[email protected]>
6
 * @copyright 2009-2019 [email protected] (G)
7
 */
8
9
namespace Ease\Logger;
10
11
/**
12
 * Log to Email
13
 *
14
 * @author    Vitex <[email protected]>
15
 * @copyright 2009-2019 [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
     *
83
     * @var string
84
     */
85
    public $recipient = null;
86
87
    /**
88
     * Log Subject
89
     *
90
     * @var string
91
     */
92
    public $subject = null;
93
94
    /**
95
     * Logger to mail Class
96
     *
97
     * @param string $recipient
98
     * @param string $subject   of message
99
     */
100
    public function __construct($recipient = null, $subject = null)
101
    {
102
        $this->recipient = $recipient;
103
        $this->subject   = empty($subject) ? $_SERVER['PHP_SELF'] : $subject;
104
        $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...
Bug introduced by
It seems like $this->recipient can also be of type null; however, parameter $emailAddress of Ease\Mailer::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

104
        $this->mailer = new \Ease\Mailer(/** @scrutinizer ignore-type */ $this->recipient, $this->subject);
Loading history...
105
        $this->mailer->setUserNotification(false);
106
    }
107
108
    /**
109
     * Pri vytvareni objektu pomoci funkce singleton (ma stejne parametry, jako
110
     * konstruktor) se bude v ramci behu programu pouzivat pouze jedna jeho
111
     * instance (ta prvni).
112
     *
113
     * @link http://docs.php.net/en/language.oop5.patterns.html Dokumentace a
114
     * priklad
115
     */
116
    public static function singleton()
117
    {
118
        if (!isset(self::$_instance)) {
119
            $class = __CLASS__;
120
            if (defined('EASE_APPNAME')) {
121
                self::$_instance = new $class(
122
                    constant('EASE_EMAILTO'),
123
                    constant('EASE_APPNAME')
124
                );
125
            } else {
126
                self::$_instance = new $class('EaseFramework');
127
            }
128
        }
129
130
        return self::$_instance;
131
    }
132
133
    /**
134
     * Zapise zapravu do logu.
135
     *
136
     * @param string $caller  název volajícího objektu
137
     * @param string $message zpráva
138
     * @param string $type    typ zprávy (success|info|error|warning|*)
139
     *
140
     * @return null|boolean byl report zapsán ?
141
     */
142
    public function addToLog($caller, $message, $type = 'notice')
143
    {
144
145
        ++$this->messageID;
146
        if (($this->logLevel == 'silent') && ($type != 'error')) {
147
            return;
148
        }
149
        if (($this->logLevel != 'debug') && ($type == 'debug')) {
150
            return;
151
        }
152
153
        $this->statusMessages[$type][$this->messageID] = $message;
154
155
        $logLine = strftime("%D %T").' `'.$caller.'`: '.$message;
156
157
        $this->mailer->textBody .= "\n" . $logLine;
158
        return true;
159
    }
160
161
    /**
162
     * Send collected messages.
163
     */
164
    public function __destruct()
165
    {
166
        if (strlen($this->mailer->mailBody) > 0 ) {
167
            $this->mailer->send();
168
        }
169
    }
170
171
    /**
172
     * Flush Messages.
173
     *
174
     * @param string $caller
175
     *
176
     * @return int how many messages was flushed
177
     */
178
    public function flush($caller = null)
179
    {
180
        $flushed = 0;
181
        if (count($this->statusMessages)) {
182
            foreach ($this->statusMessages as $type => $messages) {
183
                foreach ($messages as $messageID => $message) {
184
                    if (!isset($this->flushed[$type][$messageID])) {
185
                        $this->addToLog($caller, $message, $type);
186
                        $this->flushed[$type][$messageID] = true;
187
                        ++$flushed;
188
                    }
189
                }
190
            }
191
        }
192
193
        return $flushed;
194
    }
195
}
196