Completed
Push — master ( 68b891...7cfff0 )
by Marcin
06:22
created

Formatter::format()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
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
        return '/* ' . $this->originLine() . PHP_EOL .
46
            '   ' . $this->querySummaryLine($query) . ' */' . PHP_EOL .
47
            $this->queryLine($query) . PHP_EOL .
48
            $this->separatorLine() . PHP_EOL;
49
    }
50
51
    /**
52
     * Format time.
53
     *
54
     * @param float $time
55
     *
56
     * @return string
57
     */
58
    protected function time($time)
59
    {
60
        return $this->config->useSeconds() ? ($time / 1000.0) . 's' : $time . 'ms';
61
    }
62
63
    /**
64
     * Get origin line.
65
     *
66
     * @return string
67
     */
68
    protected function originLine()
69
    {
70
        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

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