Adapter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
namespace Vados\TCPLogger;
4
5
use Phalcon\Logger as PhalconLogger;
6
use Phalcon\Logger\AdapterInterface;
7
use Phalcon\Logger\Formatter\Line;
8
use Phalcon\Logger\FormatterInterface;
9
use Vados\TCPLogger\socket\Socket;
10
11
/**
12
 * Class Adapter
13
 * @package Vados\TCPLogger
14
 */
15
class Adapter implements AdapterInterface
16
{
17
    /**
18
     * @var FormatterInterface
19
     */
20
    private $formatter;
21
22
    /**
23
     * @var int
24
     */
25
    private $logLevel = PhalconLogger::ERROR;
26
27
    /**
28
     * @var bool
29
     */
30
    private $transactionStatus = false;
31
32
    /**
33
     * @var array
34
     */
35
    private $transactionStack = [];
36
37
    /**
38
     * @var Socket
39
     */
40
    private $socket;
41
42
    /**
43
     * Logger constructor.
44
     * @param string $host
45
     * @param int $port
46
     * @param int $protocol
47
     */
48
    public function __construct(string $host, int $port, int $protocol = Protocol::TCP)
49
    {
50
        $this->socket = SocketFactory::getByProtocol($protocol, $host, $port);
51
    }
52
53
    /**
54
     * Sets the message formatter
55
     *
56
     * @param FormatterInterface $formatter
57
     * @return AdapterInterface
58
     */
59
    public function setFormatter(FormatterInterface $formatter): AdapterInterface
60
    {
61
        $this->formatter = $formatter;
62
        return $this;
63
    }
64
65
    /**
66
     * Returns the internal formatter
67
     *
68
     * @return FormatterInterface
69
     */
70
    public function getFormatter(): FormatterInterface
71
    {
72
        if ($this->formatter === null) {
73
            $this->formatter = new Line();
74
        }
75
        return $this->formatter;
76
    }
77
78
    /**
79
     * Filters the logs sent to the handlers to be greater or equals than a specific level
80
     *
81
     * @param int $level
82
     * @return AdapterInterface
83
     */
84
    public function setLogLevel($level): AdapterInterface
85
    {
86
        $this->logLevel = $level;
87
        return $this;
88
    }
89
90
    /**
91
     * Returns the current log level
92
     *
93
     * @return int
94
     */
95
    public function getLogLevel(): int
96
    {
97
        return $this->logLevel;
98
    }
99
100
    /**
101
     * Sends/Writes messages to the file log
102
     *
103
     * @param mixed $type
104
     * @param mixed $message
105
     * @param array $context
106
     * @return AdapterInterface
107
     */
108
    public function log($type, $message = null, array $context = null): AdapterInterface
109
    {
110
        if ($this->transactionStatus) {
111
            $this->transactionStack[] = [
112
                'type' => $type,
113
                'message' => $message,
114
                'context' => $context
115
            ];
116
        } else {
117
            $package = $this->getFormatter()->format($message, $type, time(), $context);
118
            $package = str_replace(PHP_EOL, '<=>', $package) . PHP_EOL;
119
            $this->socket->send($package);
120
        }
121
        return $this;
122
    }
123
124
    /**
125
     * Starts a transaction
126
     *
127
     * @return AdapterInterface
128
     */
129
    public function begin(): AdapterInterface
130
    {
131
        $this->transactionStatus = true;
132
        return $this;
133
    }
134
135
    /**
136
     * Commits the internal transaction
137
     *
138
     * @return AdapterInterface
139
     */
140
    public function commit(): AdapterInterface
141
    {
142
        $this->transactionStatus = false;
143
        foreach ($this->transactionStack as $item) {
144
            $this->log($item['type'], $item['message'], $item['context']);
145
        }
146
        $this->transactionStack = [];
147
        return $this;
148
    }
149
150
    /**
151
     * Rollbacks the internal transaction
152
     *
153
     * @return AdapterInterface
154
     */
155
    public function rollback(): AdapterInterface
156
    {
157
        $this->transactionStack = [];
158
        $this->transactionStatus = false;
159
        return $this;
160
    }
161
162
    /**
163
     * Closes the logger
164
     *
165
     * @return bool
166
     */
167
    public function close(): bool
168
    {
169
        return $this->socket->close();
170
    }
171
172
    /**
173
     * Sends/Writes a debug message to the log
174
     *
175
     * @param string $message
176
     * @param array $context
177
     * @return AdapterInterface
178
     */
179
    public function debug($message, array $context = null): AdapterInterface
180
    {
181
        $this->log(PhalconLogger::DEBUG, $message, $context);
182
        return $this;
183
    }
184
185
    /**
186
     * Sends/Writes an error message to the log
187
     *
188
     * @param string $message
189
     * @param array $context
190
     * @return AdapterInterface
191
     */
192
    public function error($message, array $context = null): AdapterInterface
193
    {
194
        $this->log(PhalconLogger::ERROR, $message, $context);
195
        return $this;
196
    }
197
198
    /**
199
     * Sends/Writes an info message to the log
200
     *
201
     * @param string $message
202
     * @param array $context
203
     * @return AdapterInterface
204
     */
205
    public function info($message, array $context = null): AdapterInterface
206
    {
207
        $this->log(PhalconLogger::INFO, $message, $context);
208
        return $this;
209
    }
210
211
    /**
212
     * Sends/Writes a notice message to the log
213
     *
214
     * @param string $message
215
     * @param array $context
216
     * @return AdapterInterface
217
     */
218
    public function notice($message, array $context = null): AdapterInterface
219
    {
220
        $this->log(PhalconLogger::NOTICE, $message, $context);
221
        return $this;
222
    }
223
224
    /**
225
     * Sends/Writes a warning message to the log
226
     *
227
     * @param string $message
228
     * @param array $context
229
     * @return AdapterInterface
230
     */
231
    public function warning($message, array $context = null): AdapterInterface
232
    {
233
        $this->log(PhalconLogger::WARNING, $message, $context);
234
        return $this;
235
    }
236
237
    /**
238
     * Sends/Writes an alert message to the log
239
     *
240
     * @param string $message
241
     * @param array $context
242
     * @return AdapterInterface
243
     */
244
    public function alert($message, array $context = null): AdapterInterface
245
    {
246
        $this->log(PhalconLogger::ALERT, $message, $context);
247
        return $this;
248
    }
249
250
    /**
251
     * Sends/Writes an emergency message to the log
252
     *
253
     * @param string $message
254
     * @param array $context
255
     * @return AdapterInterface
256
     */
257
    public function emergency($message, array $context = null): AdapterInterface
258
    {
259
        $this->log(PhalconLogger::EMERGENCY, $message, $context);
260
        return $this;
261
    }
262
263
    public function __destruct()
264
    {
265
        $this->close();
266
    }
267
}