Passed
Push — master ( 4aacc0...bb1936 )
by Marcin
03:14
created

Writer::createDirectoryIfNotExists()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Mnabialek\LaravelSqlLogger;
4
5
use Carbon\Carbon;
6
use Illuminate\Contracts\Foundation\Application;
7
use Mnabialek\LaravelSqlLogger\Objects\SqlQuery;
8
9
class Writer
10
{
11
    /**
12
     * @var Application
13
     */
14
    private $app;
15
16
    /**
17
     * @var Formatter
18
     */
19
    private $formatter;
20
21
    /**
22
     * @var Config
23
     */
24
    private $config;
25
26
    /**
27
     * Writer constructor.
28
     *
29
     * @param Application $app
30
     * @param Formatter $formatter
31
     * @param Config $config
32
     */
33
    public function __construct(Application $app, Formatter $formatter, Config $config)
34
    {
35
        $this->app = $app;
36
        $this->formatter = $formatter;
37
        $this->config = $config;
38
    }
39
40
    /**
41
     * Save queries to log.
42
     *
43
     * @param SqlQuery $query
44
     */
45
    public function save(SqlQuery $query)
46
    {
47
        $this->createDirectoryIfNotExists($query->number());
48
49
        $line = $this->formatter->getLine($query);
50
51
        if ($this->shouldLogQuery($query)) {
52
            $this->saveLine($line, $this->logName(), $this->shouldOverrideFile($query));
53
        }
54
55
        if ($this->shouldLogSlowQuery($query)) {
56
            $this->saveLine($line, $this->slowLogName());
57
        }
58
    }
59
60
    /**
61
     * Create directory if it does not exist yet.
62
     *
63
     * @param int $queryNumber
64
     */
65
    protected function createDirectoryIfNotExists($queryNumber)
66
    {
67
        if ($queryNumber == 1 && ! file_exists($directory = $this->directory())) {
68
            mkdir($directory, 0777, true);
69
        }
70
    }
71
72
    /**
73
     * Get directory where file should be located.
74
     *
75
     * @return string
76
     */
77
    protected function directory()
78
    {
79
        return rtrim($this->config->logDirectory(), '\\/');
80
    }
81
82
    /**
83
     * Get log name.
84
     *
85
     * @return string
86
     */
87
    protected function logName()
88
    {
89
        return Carbon::now()->format('Y-m-d') . $this->suffix() . '-log.sql';
90
    }
91
92
    /**
93
     * Get slow log name.
94
     *
95
     * @return string
96
     */
97
    protected function slowLogName()
98
    {
99
        return Carbon::now()->format('Y-m-d') . $this->suffix() . '-slow-log.sql';
100
    }
101
102
    /**
103
     * Get file suffix.
104
     *
105
     * @return string
106
     */
107
    protected function suffix()
108
    {
109
        return $this->app->runningInConsole() ? $this->config->consoleSuffix() : '';
110
    }
111
112
    /**
113
     * Verify whether query should be logged.
114
     *
115
     * @param SqlQuery $query
116
     *
117
     * @return bool
118
     */
119
    protected function shouldLogQuery(SqlQuery $query)
120
    {
121
        return $this->config->logAllQueries() &&
122
            preg_match($this->config->allQueriesPattern(), $query->raw());
123
    }
124
125
    /**
126
     * Verify whether slow query should be logged.
127
     *
128
     * @param SqlQuery $query
129
     *
130
     * @return bool
131
     */
132
    protected function shouldLogSlowQuery(SqlQuery $query)
133
    {
134
        return $this->config->logSlowQueries() && $query->time() >= $this->config->slowLogTime() &&
135
            preg_match($this->config->slowQueriesPattern(), $query->raw());
136
    }
137
138
    /**
139
     * Save data to log file.
140
     *
141
     * @param string $line
142
     * @param string $fileName
143
     * @param bool $override
144
     */
145
    protected function saveLine($line, $fileName, $override = false)
146
    {
147
        file_put_contents($this->directory() . DIRECTORY_SEPARATOR . $fileName,
148
            $line, $override ? 0 : FILE_APPEND);
149
    }
150
151
    /**
152
     * Verify whether file should be overridden.
153
     *
154
     * @param SqlQuery $query
155
     *
156
     * @return bool
157
     */
158
    private function shouldOverrideFile(SqlQuery $query)
159
    {
160
        return ($query->number() == 1 && $this->config->overrideFile());
161
    }
162
}
163