Completed
Push — master ( a8d3e1...8005db )
by Marcin
05:51
created

Formatter::removeNewLines()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Mnabialek\LaravelSqlLogger;
4
5
use Carbon\Carbon;
6
use Illuminate\Container\Container;
7
use Mnabialek\LaravelSqlLogger\Objects\Concerns\ReplacesBindings;
8
use Mnabialek\LaravelSqlLogger\Objects\SqlQuery;
9
10
class Formatter
11
{
12
    use ReplacesBindings;
13
14
    /**
15
     * @var Container
16
     */
17
    private $app;
18
19
    /**
20
     * @var Config
21
     */
22
    private $config;
23
24
    /**
25
     * Formatter constructor.
26
     *
27
     * @param Container $app
28
     * @param Config $config
29
     */
30
    public function __construct(Container $app, Config $config)
31
    {
32
        $this->app = $app;
33
        $this->config = $config;
34
    }
35
36
    /**
37
     * Get formatted line.
38
     *
39
     * @param SqlQuery $query
40
     *
41
     * @return string
42
     */
43
    public function getLine(SqlQuery $query)
44
    {
45
        $replace = [
46
            '[origin]' => $this->originLine(),
47
            '[query_nr]' => $query->number(),
48
            '[datetime]' => Carbon::now()->toDateTimeString(),
49
            '[query_time]' => $this->time($query->time()),
50
            '[query]' => $this->queryLine($query),
51
            '[separator]' => $this->separatorLine(),
52
            '\n' => PHP_EOL,
53
        ];
54
55
        $entry = str_replace(array_keys($replace), array_values($replace), $this->config->entryFormat());
56
57
        return preg_replace_callback('(\[user_(\w*)\])', function ($matches) {
58
            return ($user = $this->app['auth']->user()) ? $user->{$matches[1]} : '-';
59
        }, $entry);
60
    }
61
62
    /**
63
     * Format time.
64
     *
65
     * @param float $time
66
     *
67
     * @return string
68
     */
69
    protected function time($time)
70
    {
71
        return $this->config->useSeconds() ? ($time / 1000.0) . 's' : $time . 'ms';
72
    }
73
74
    /**
75
     * Get origin line.
76
     *
77
     * @return string
78
     */
79
    protected function originLine()
80
    {
81
        return 'Origin ' . ($this->app->runningInConsole()
0 ignored issues
show
introduced by
The method runningInConsole() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

81
        return 'Origin ' . ($this->app->/** @scrutinizer ignore-call */ runningInConsole()
Loading history...
82
                ? '(console): ' . $this->getArtisanLine()
83
                : '(request): ' . $this->getRequestLine());
84
    }
85
86
    /**
87
     * Get query line.
88
     *
89
     * @param SqlQuery $query
90
     *
91
     * @return string
92
     */
93
    protected function queryLine(SqlQuery $query)
94
    {
95
        return $this->format($query->get()) . ';';
96
    }
97
98
    /**
99
     * Get Artisan line.
100
     *
101
     * @return string
102
     */
103
    protected function getArtisanLine()
104
    {
105
        $command = $this->app['request']->server('argv', []);
106
107
        if (is_array($command)) {
0 ignored issues
show
introduced by
The condition is_array($command) is always true.
Loading history...
108
            $command = implode(' ', $command);
109
        }
110
111
        return $command;
112
    }
113
114
    /**
115
     * Get request line.
116
     *
117
     * @return string
118
     */
119
    protected function getRequestLine()
120
    {
121
        return $this->app['request']->method() . ' ' . $this->app['request']->fullUrl();
122
    }
123
124
    /**
125
     * Get separator line.
126
     *
127
     * @return string
128
     */
129
    protected function separatorLine()
130
    {
131
        return '/*' . str_repeat('=', 50) . '*/';
132
    }
133
134
    /**
135
     * Format given query.
136
     *
137
     * @param string $query
138
     *
139
     * @return string
140
     */
141
    protected function format($query)
142
    {
143
        return $this->removeNewLines($query);
144
    }
145
146
    /**
147
     * Remove new lines from SQL to keep it in single line if possible.
148
     *
149
     * @param string $sql
150
     *
151
     * @return string
152
     */
153
    protected function removeNewLines($sql)
154
    {
155
        if (! $this->config->newLinesToSpaces()) {
156
            return $sql;
157
        }
158
159
        return preg_replace($this->wrapRegex($this->notInsideQuotes('\v', false)), ' ', $sql);
160
    }
161
}
162