Completed
Push — master ( 673fc6...a88edb )
by Oleg
03:40
created

DbDriver   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 59
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 21 3
A sendMessage() 0 8 1
1
<?php /** MicroDbDriver */
2
3
namespace Micro\Logger\Drivers;
4
5
use Micro\Base\Exception;
6
use Micro\Db\Adapter;
7
use Micro\Db\Injector;
8
9
/**
10
 * DB logger class file   .
11
 *
12
 * Writer logs in DB
13
 *
14
 * @author Oleg Lunegov <[email protected]>
15
 * @link https://github.com/linpax/microphp-framework
16
 * @copyright Copyright (c) 2013 Oleg Lunegov
17
 * @license https://github.com/linpax/microphp-framework/blob/master/LICENSE
18
 * @package Micro
19
 * @subpackage Logger\Driver
20
 * @version 1.0
21
 * @since 1.0
22
 */
23
class DbDriver extends LoggerDriver
24
{
25
    /** @var string $tableName logger table name */
26
    public $tableName;
27
    /** @var Adapter $db */
28
    protected $db;
29
30
31
    /**
32
     * Constructor prepare DB
33
     *
34
     * @access public
35
     *
36
     * @param array $params configuration params
37
     *
38
     * @result void
39
     * @throws Exception
40
     */
41
    public function __construct(array $params = [])
42
    {
43
        parent::__construct($params);
44
45
        $this->tableName = !empty($params['table']) ? $params['table'] : 'logs';
46
        $this->db = (new Injector)->getDriver();
0 ignored issues
show
Documentation Bug introduced by
It seems like (new \Micro\Db\Injector())->getDriver() of type object<Micro\Db\Drivers\IDriver> is incompatible with the declared type object<Micro\Db\Adapter> of property $db.

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...
47
48
        if (!$this->db->tableExists($this->tableName)) {
49
            $this->db->createTable(
50
                $this->tableName,
51
                array(
52
                    '`id` INT AUTO_INCREMENT',
53
                    '`level` VARCHAR(20) NOT NULL',
54
                    '`message` TEXT NOT NULL',
55
                    '`date_create` INT NOT NULL',
56
                    'PRIMARY KEY(id)'
57
                ),
58
                'ENGINE=InnoDB DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci'
59
            );
60
        }
61
    }
62
63
    /**
64
     * Send log message into DB
65
     *
66
     * @access public
67
     *
68
     * @param integer $level level number
69
     * @param string $message message to write
70
     *
71
     * @return void
72
     */
73
    public function sendMessage($level, $message)
0 ignored issues
show
Coding Style introduced by
sendMessage uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
74
    {
75
        $this->db->insert($this->tableName, [
0 ignored issues
show
Bug introduced by
The method insert() does not seem to exist on object<Micro\Db\Adapter>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
76
            'level' => $level,
77
            'message' => $message,
78
            'date_create' => $_SERVER['REQUEST_TIME']
79
        ]);
80
    }
81
}
82