Skip to content

Commit 7ca4b68

Browse files
committed
source freshen up, updates per linter, v 1.0.4
1 parent aedef03 commit 7ca4b68

File tree

2 files changed

+58
-48
lines changed

2 files changed

+58
-48
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cbschuld/logentries",
3-
"version": "1.0.3",
3+
"version": "1.0.4",
44
"description": "A PHP PSR-3 compliant logging class for LogEntries service (https://logentries.com/)",
55
"keywords": [
66
"logging"

src/LogEntries.php

Lines changed: 57 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,32 @@ class LogEntries extends AbstractLogger
1818
const LE_TLS_PORT = 20000;
1919

2020
/** @var LogEntries */
21-
private static $_instance = null;
21+
private static $_instance;
2222
/**
2323
* Writer middleware call stack
2424
*
2525
* @var \SplStack
2626
* @link http://php.net/manual/class.splstack.php
2727
*/
28-
private $_writer_stack = array();
28+
private $_writer_stack;
2929
/** @var resource */
30-
private $_socketResource = null;
30+
private $_socketResource;
3131
/** @var string the token for LogEntries */
32-
private $_token = null;
32+
private $_token;
3333
/** @var string the ip address for DataHub*/
34-
private $_dataHubIPAddress = "";
34+
private $_dataHubIPAddress = '';
3535
/** @var boolean use DataHub */
3636
private $_useDataHub = false;
3737
/** @var int the port number for DataHub */
3838
private $_dataHubPort = 10000;
3939
/** @var string hostname to log, if provided it is added to the string or added to the json */
40-
private $_hostname = "";
40+
private $_hostname;
4141
/** @var int seconds before timeout, defaults to the ini setting value of 'default_socket_timeout' */
4242
private $_connectionTimeout;
4343
/** @var boolean use a persistent connection */
44-
private $_persistent = true;
44+
private $_persistent;
4545
/** @var boolean use ssl for connection */
46-
private $_ssl = false;
46+
private $_ssl;
4747
/** @var string - error number */
4848
private $_error_number;
4949
/** @var string - error description */
@@ -60,8 +60,9 @@ class LogEntries extends AbstractLogger
6060
* @param int $dataHubPort the port number for the LogEntries DataHub if in use
6161
* @param string $hostname the hostname to add to the string or json log entry (optional)
6262
* @return LogEntries
63+
* @throws \InvalidArgumentException
6364
*/
64-
public static function getLogger($token, $persistent = true, $ssl = false, $dataHubEnabled = false, $dataHubIPAddress = "", $dataHubPort = 10000, $hostname = "")
65+
public static function getLogger($token, $persistent = true, $ssl = false, $dataHubEnabled = false, $dataHubIPAddress = '', $dataHubPort = 10000, $hostname = '')
6566
{
6667
if (!self::$_instance) {
6768
self::$_instance = new LogEntries($token, $persistent, $ssl, $dataHubEnabled, $dataHubIPAddress, $dataHubPort, $hostname);
@@ -84,6 +85,7 @@ public static function tearDown()
8485
public function addWriter(LogEntriesWriter $writer) {
8586
$this->_writer_stack->push($writer);
8687
}
88+
8789
/**
8890
* LogEntries constructor which sets up the connection defaults
8991
* @param $token string token for access to their API
@@ -93,8 +95,9 @@ public function addWriter(LogEntriesWriter $writer) {
9395
* @param string $dataHubIPAddress the IP address for the LogEntries DataHub if in use
9496
* @param int $dataHubPort the port number for the LogEntries DataHub if in use
9597
* @param string $hostname the hostname to add to the string or json log entry (optional)
98+
* @throws \InvalidArgumentException
9699
*/
97-
public function __construct($token, $persistent = true, $ssl = false, $dataHubEnabled = false, $dataHubIPAddress = "", $dataHubPort = 0, $hostname = "")
100+
public function __construct($token, $persistent = true, $ssl = false, $dataHubEnabled = false, $dataHubIPAddress = '', $dataHubPort = 0, $hostname = '')
98101
{
99102

100103
$this->_writer_stack = new \SplStack();
@@ -132,6 +135,7 @@ public function __destruct()
132135
/**
133136
* validates the token provided by the caller is not empty
134137
* @param $token
138+
* @throws \InvalidArgumentException
135139
*/
136140
public function validateToken($token)
137141
{
@@ -144,6 +148,7 @@ public function validateToken($token)
144148
/**
145149
* validates the ip address provided by the caller is not empty
146150
* @param $dataHubIPAddress
151+
* @throws \InvalidArgumentException
147152
*/
148153
public function validateDataHubIP($dataHubIPAddress)
149154
{
@@ -189,11 +194,11 @@ public function getPort()
189194
{
190195
if ($this->isTLS()) {
191196
return self::LE_TLS_PORT;
192-
} elseif ($this->isDataHub()) {
197+
}
198+
if ($this->isDataHub()) {
193199
return $this->_dataHubPort;
194-
} else {
195-
return self::LE_PORT;
196200
}
201+
return self::LE_PORT;
197202
}
198203

199204
/**
@@ -213,11 +218,11 @@ public function getAddress()
213218
{
214219
if ($this->isTLS() && !$this->isDataHub()) {
215220
return self::LE_TLS_ADDRESS;
216-
} elseif ($this->isDataHub()) {
221+
}
222+
if ($this->isDataHub()) {
217223
return $this->_dataHubIPAddress;
218-
} else {
219-
return self::LE_ADDRESS;
220224
}
225+
return self::LE_ADDRESS;
221226
}
222227

223228
/**
@@ -278,7 +283,7 @@ private function getSocket($port, $address)
278283
public function writeToSocket($line)
279284
{
280285
if ($this->isConnected() || $this->connectIfNotConnected()) {
281-
fputs($this->_socketResource, $this->_token . $line);
286+
fwrite($this->_socketResource, $this->_token . $line);
282287
}
283288
}
284289

@@ -290,9 +295,7 @@ public function writeToSocket($line)
290295
private function substituteNewline($line)
291296
{
292297
$unicodeChar = chr(13);
293-
$newLine = str_replace(PHP_EOL, $unicodeChar, $line);
294-
295-
return $newLine;
298+
return str_replace(PHP_EOL, $unicodeChar, $line);
296299
}
297300

298301
/**
@@ -323,7 +326,7 @@ private function connect()
323326
*/
324327
private function isJSON($string)
325328
{
326-
return is_string($string) && is_object(json_decode($string)) && (json_last_error() == JSON_ERROR_NONE) ? true : false;
329+
return (is_string($string) && is_object(json_decode($string)) && (json_last_error() === JSON_ERROR_NONE));
327330
}
328331

329332

@@ -348,36 +351,35 @@ public function log($level, $message, array $context = array())
348351
if (!is_string($message)) {
349352
throw new \InvalidArgumentException('the message argument needs to be a string or an array');
350353
}
351-
else {
352-
$isJson = $this->isJSON($message);
353-
if ($isJson) {
354-
$json = json_decode($message, true);
355-
if ("" != $this->_hostname) {
356-
$json["hostname"] = $this->_hostname;
357-
}
358-
$json["level"] = $level;
354+
$isJson = $this->isJSON($message);
355+
if ($isJson) {
356+
$json = json_decode($message, true);
357+
if ('' !== $this->_hostname) {
358+
$json['hostname'] = $this->_hostname;
359+
}
360+
$json['level'] = $level;
361+
if (count($context) > 0) {
362+
$json['context'] = $context;
363+
}
364+
$message = json_encode($json);
365+
} else {
366+
$message = strtoupper($level) . ' - ' . $message;
367+
if ('' !== $this->_hostname) {
368+
$message = "hostname={$this->_hostname} - " . $message;
359369
if (count($context) > 0) {
360-
$json["context"] = $context;
361-
}
362-
$message = json_encode($json);
363-
} else {
364-
$message = strtoupper($level) . " - " . $message;
365-
if ("" != $this->_hostname) {
366-
$message = "hostname={$this->_hostname} - " . $message;
367-
if (count($context) > 0) {
368-
$message .= " - " . json_encode($context);
369-
}
370+
$message .= ' - ' . json_encode($context);
370371
}
371372
}
372-
$this->_writer_stack->rewind();
373-
while( $this->_writer_stack->valid() ) {
374-
/** @var LogEntriesWriter $writer */
375-
$writer = $this->_writer_stack->current();
376-
$message = $writer->log($message,$isJson);
377-
$this->_writer_stack->next();
378-
}
379-
$this->writeToSocket($this->substituteNewline($message) . PHP_EOL);
380373
}
374+
$this->_writer_stack->rewind();
375+
while( $this->_writer_stack->valid() ) {
376+
/** @var LogEntriesWriter $writer */
377+
$writer = $this->_writer_stack->current();
378+
$message = $writer->log($message,$isJson);
379+
$this->_writer_stack->next();
380+
}
381+
$this->writeToSocket($this->substituteNewline($message) . PHP_EOL);
382+
return null;
381383
}
382384

383385
/**
@@ -386,6 +388,7 @@ public function log($level, $message, array $context = array())
386388
* @param string|array $message
387389
* @param array $context
388390
* @return null
391+
* @throws \InvalidArgumentException
389392
*/
390393
public function emergency($message, array $context = array())
391394
{
@@ -401,6 +404,7 @@ public function emergency($message, array $context = array())
401404
* @param string|array $message
402405
* @param array $context
403406
* @return null
407+
* @throws \InvalidArgumentException
404408
*/
405409
public function alert($message, array $context = array())
406410
{
@@ -415,6 +419,7 @@ public function alert($message, array $context = array())
415419
* @param string|array $message
416420
* @param array $context
417421
* @return null
422+
* @throws \InvalidArgumentException
418423
*/
419424
public function critical($message, array $context = array())
420425
{
@@ -428,6 +433,7 @@ public function critical($message, array $context = array())
428433
* @param string|array $message
429434
* @param array $context
430435
* @return null
436+
* @throws \InvalidArgumentException
431437
*/
432438
public function error($message, array $context = array())
433439
{
@@ -443,6 +449,7 @@ public function error($message, array $context = array())
443449
* @param string|array $message
444450
* @param array $context
445451
* @return null
452+
* @throws \InvalidArgumentException
446453
*/
447454
public function warning($message, array $context = array())
448455
{
@@ -455,6 +462,7 @@ public function warning($message, array $context = array())
455462
* @param string|array $message
456463
* @param array $context
457464
* @return null
465+
* @throws \InvalidArgumentException
458466
*/
459467
public
460468
function notice($message, array $context = array())
@@ -470,6 +478,7 @@ function notice($message, array $context = array())
470478
* @param string|array $message
471479
* @param array $context
472480
* @return null
481+
* @throws \InvalidArgumentException
473482
*/
474483
public function info($message, array $context = array())
475484
{
@@ -482,6 +491,7 @@ public function info($message, array $context = array())
482491
* @param string|array $message
483492
* @param array $context
484493
* @return null
494+
* @throws \InvalidArgumentException
485495
*/
486496
public function debug($message, array $context = array())
487497
{

0 commit comments

Comments
 (0)