Passed
Push — master ( 856966...11e739 )
by Oleg
03:58
created

DbDriver::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 13

Duplication

Lines 20
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 20
loc 20
rs 9.4285
cc 3
eloc 13
nc 4
nop 2
1
<?php /** MicroDbDriver */
2
3
namespace Micro\Logger\Driver;
4
5
use Micro\Base\Exception;
6
use Micro\Base\IContainer;
7
8
/**
9
 * DB logger class file.
10
 *
11
 * Writer logs in DB
12
 *
13
 * @author Oleg Lunegov <[email protected]>
14
 * @link https://github.com/linpax/microphp-framework
15
 * @copyright Copyright &copy; 2013 Oleg Lunegov
16
 * @license /LICENSE
17
 * @package Micro
18
 * @subpackage Logger\Driver
19
 * @version 1.0
20
 * @since 1.0
21
 */
22
class DbDriver extends LoggerDriver
23
{
24
    /** @var string $tableName logger table name */
25
    public $tableName;
26
27
28
    /**
29
     * Constructor prepare DB
30
     *
31
     * @access public
32
     *
33
     * @param IContainer $container Container
34
     * @param array $params configuration params
35
     *
36
     * @result void
37
     * @throws Exception
38
     */
39 View Code Duplication
    public function __construct(IContainer $container, array $params = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
    {
41
        parent::__construct($container, $params);
42
43
        $this->tableName = !empty($params['table']) ? $params['table'] : 'logs';
44
45
        if (!$this->container->db->tableExists($this->tableName)) {
0 ignored issues
show
Bug introduced by
Accessing db on the interface Micro\Base\IContainer suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
46
            $this->container->db->createTable(
0 ignored issues
show
Bug introduced by
Accessing db on the interface Micro\Base\IContainer suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
47
                $this->tableName,
48
                array(
49
                    '`id` INT AUTO_INCREMENT',
50
                    '`level` VARCHAR(20) NOT NULL',
51
                    '`message` TEXT NOT NULL',
52
                    '`date_create` INT NOT NULL',
53
                    'PRIMARY KEY(id)'
54
                ),
55
                'ENGINE=InnoDB DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci'
56
            );
57
        }
58
    }
59
60
    /**
61
     * Send log message into DB
62
     *
63
     * @access public
64
     *
65
     * @param integer $level level number
66
     * @param string $message message to write
67
     *
68
     * @return void
69
     */
70
    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...
71
    {
72
        $this->container->db->insert($this->tableName, [
0 ignored issues
show
Bug introduced by
Accessing db on the interface Micro\Base\IContainer suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
73
            'level' => $level,
74
            'message' => $message,
75
            'date_create' => $_SERVER['REQUEST_TIME']
76
        ]);
77
    }
78
}
79