LogDBObject::log()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 3
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
namespace Asymptix\tools\logging;
4
5
use Asymptix\db\DBCore;
6
7
/**
8
 * Simple Log class to store Logger messages into teh database.
9
 *
10
 * @category Asymptix PHP Framework
11
 * @author Dmytro Zarezenko <[email protected]>
12
 * @copyright (c) 2016, Dmytro Zarezenko
13
 * @license http://opensource.org/licenses/MIT
14
 */
15
class LogDBObject extends \Asymptix\db\DBObject {
16
17
    const TABLE_NAME = "log";
18
    const ID_FIELD_NAME = "";
19
    protected $fieldsList = [
20
        'type' => "", // varchar(10), default ''
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
21
        'message' => "", // text, not null, default ''
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
22
        'count' => 0, // int(6) unsigned, not null, default ''
0 ignored issues
show
Unused Code Comprehensibility introduced by
48% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
23
        'last_seen' => "CURRENT_TIMESTAMP", // datetime, not null, default 'CURRENT_TIMESTAMP'
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
24
    ];
25
26
    /**
27
     * Log method, must be in all classes for Logger class functionality.
28
     *
29
     * @param string $type Log message type from Logger class.
30
     * @param string $message Message text.
31
     * @param int $time Timestamp.
32
     *
33
     * @return int Number of affected rows.
34
     * @throws \Asymptix\db\DBCoreException If some database error occurred.
35
     */
36
    public static function log($type, $message, $time = null) {
37
        if (is_null($time)) {
38
            $time = time();
39
        }
40
41
        $query = "INSERT INTO " . self::TABLE_NAME . " (type, message, count, last_seen) VALUES (?, ?, 1, ?)
42
                  ON DUPLICATE KEY UPDATE count = count + 1, last_seen = ?";
43
        try {
44
            return DBCore::doUpdateQuery($query, "ssss", [
45
                $type, $message, date("Y-m-d H:i:s", $time), date("Y-m-d H:i:s", $time)
46
            ]);
47
        } catch (\Asymptix\db\DBCoreException $e) {
48
            print($e->getMessage());
49
        }
50
    }
51
52
}
53