Passed
Push — master ( 4c1ee6...e469ee )
by 世昌
01:57
created

DebugObserver   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 30
dl 0
loc 55
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A observe() 0 34 5
A __construct() 0 3 1
1
<?php
2
3
namespace suda\application\database;
4
5
use suda\framework\Debugger;
6
use suda\orm\statement\Statement;
7
use suda\orm\connection\Connection;
8
use suda\orm\statement\QueryAccess as StatementQueryAccess;
9
use suda\orm\connection\observer\Observer;
10
11
class DebugObserver implements Observer
12
{
13
    /**
14
     * 调试记录
15
     *
16
     * @var Debugger
17
     */
18
    protected $debug;
19
20
    public function __construct(Debugger $debug)
21
    {
22
        $this->debug = $debug;
23
    }
24
25
    /**
26
     * @param StatementQueryAccess $access
27
     * @param Connection $connection
28
     * @param Statement $statement
29
     * @param $timeSpend
30
     * @param bool $result
31
     */
32
    public function observe(StatementQueryAccess $access, Connection $connection, Statement $statement, $timeSpend, bool $result)
33
    {
34
        $query = $connection->prefix($statement->getString());
35
        $status = $result ? 'OK' : 'Err';
36
        if ($result) {
37
            $effect = $statement->getStatement()->rowCount();
38
            $this->debug->info(sprintf(
39
                "query [%s] %s %ss",
40
                $status,
41
                $query,
42
                number_format($timeSpend, 5)
43
            ));
44
            $this->debug->info(sprintf("query effect %s rows", $effect));
45
        } else {
46
            $this->debug->error(sprintf(
47
                "query [%s] %s %ss",
48
                $status,
49
                $query,
50
                number_format($timeSpend, 5)
51
            ));
52
            $this->debug->error(sprintf(
53
                "query [%s] %s",
54
                $status,
55
                implode(':', $statement->getStatement()->errorInfo())
56
            ));
57
        }
58
        $binder = $statement->getBinder();
59
        foreach ($binder as $item) {
60
            if ($item->getKey() !== null) {
61
                $value = $access->getMiddleware()->input($item->getKey(), $item->getValue());
62
            } else {
63
                $value = $item->getValue();
64
            }
65
            $this->debug->debug(sprintf("query value :%s = %s", $item->getName(), json_encode($value)));
66
        }
67
    }
68
}
69