SqlQuery::logQuery()   B
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 62
rs 8.2068
c 0
b 0
f 0
cc 6
nc 4
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace PragmaRX\Tracker\Data\Repositories;
4
5
use PragmaRX\Support\Config;
6
7
class SqlQuery extends Repository
8
{
9
    private $queries = [];
10
11
    /**
12
     * @var SqlQueryLog
13
     */
14
    private $sqlQueryLogRepository;
15
16
    /**
17
     * @var SqlQueryBinding
18
     */
19
    private $sqlQueryBindingRepository;
20
21
    /**
22
     * @var SqlQueryBindingParameter
23
     */
24
    private $sqlQueryBindingParameterRepository;
25
26
    /**
27
     * @var Connection
28
     */
29
    private $connectionRepository;
30
31
    /**
32
     * @var Log
33
     */
34
    private $logRepository;
35
36
    /**
37
     * @var \PragmaRX\Support\Config
38
     */
39
    private $config;
40
41
    public function __construct(
42
        $model,
43
        SqlQueryLog $sqlQueryLogRepository,
44
        SqlQueryBinding $sqlQueryBindingRepository,
45
        SqlQueryBindingParameter $sqlQueryBindingParameterRepository,
46
        Connection $connectionRepository,
47
        Log $logRepository,
48
        Config $config
49
    ) {
50
        parent::__construct($model);
51
52
        $this->sqlQueryLogRepository = $sqlQueryLogRepository;
53
54
        $this->sqlQueryBindingRepository = $sqlQueryBindingRepository;
55
56
        $this->sqlQueryBindingParameterRepository = $sqlQueryBindingParameterRepository;
57
58
        $this->connectionRepository = $connectionRepository;
59
60
        $this->logRepository = $logRepository;
61
62
        $this->config = $config;
63
    }
64
65
    public function fire()
66
    {
67
        if (!$this->logRepository->getCurrentLogId()) {
68
            return;
69
        }
70
71
        foreach ($this->queries as $query) {
72
            $this->logQuery($query);
73
        }
74
75
        $this->clear();
76
    }
77
78
    private function sqlQueryIsLoggable($sqlQuery)
79
    {
80
        return strpos($sqlQuery, '"tracker_') === false;
81
    }
82
83
    private function serializeBindings($bindings)
84
    {
85
        return serialize($bindings);
86
    }
87
88
    public function push($query)
89
    {
90
        $this->queries[] = $query;
91
92
        $this->fire();
93
    }
94
95
    private function logQuery($query)
96
    {
97
        $sqlQuery = htmlentities($query['query']);
98
99
        $bindings = $query['bindings'];
100
101
        $time = $query['time'];
102
103
        $name = $query['name'];
104
105
        if (!$this->sqlQueryIsLoggable($sqlQuery)) {
106
            return;
107
        }
108
109
        $connectionId = $this->connectionRepository->findOrCreate(
110
            ['name' => $name],
111
            ['name']
112
        );
113
114
        $sqlQueryId = $this->findOrCreate(
115
            [
116
                'sha1'          => sha1($sqlQuery),
117
                'statement'     => $sqlQuery,
118
                'time'          => $time,
119
                'connection_id' => $connectionId,
120
            ],
121
            ['sha1']
122
        );
123
124
        if ($bindings && $this->canLogBindings()) {
125
            $bindingsSerialized = $this->serializeBindings($bindings);
126
127
            $sqlQuery_bindings_id = $this->sqlQueryBindingRepository->findOrCreate(
128
                ['sha1' => sha1($bindingsSerialized), 'serialized' => $bindingsSerialized],
129
                ['sha1'],
130
                $created
131
            );
132
133
            if ($created) {
134
                foreach ($bindings as $parameter => $value) {
135
                    $this->sqlQueryBindingParameterRepository->create(
136
                        [
137
                            'sql_query_bindings_id' => $sqlQuery_bindings_id,
138
139
                            // unfortunately laravel uses question marks,
140
                            // but hopefully someday this will change
141
                            'name'                  => '?',
142
143
                            'value'                 => $value,
144
                        ]
145
                    );
146
                }
147
            }
148
        }
149
150
        $this->sqlQueryLogRepository->create(
151
            [
152
                'log_id'       => $this->logRepository->getCurrentLogId(),
153
                'sql_query_id' => $sqlQueryId,
154
            ]
155
        );
156
    }
157
158
    private function canLogBindings()
159
    {
160
        return $this->config->get('log_sql_queries_bindings');
161
    }
162
163
    /**
164
     * @return array
165
     */
166
    private function clear()
167
    {
168
        return $this->queries = [];
169
    }
170
}
171