Writer::shouldLogQuery()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
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 Mnabialek\LaravelSqlLogger\Objects\SqlQuery;
6
7
class Writer
8
{
9
    /**
10
     * @var Formatter
11
     */
12
    private $formatter;
13
14
    /**
15
     * @var Config
16
     */
17
    private $config;
18
19
    /**
20
     * @var FileName
21
     */
22
    private $fileName;
23
24
    /**
25
     * Writer constructor.
26
     *
27
     * @param Formatter $formatter
28
     * @param Config $config
29
     * @param FileName $fileName
30
     */
31
    public function __construct(Formatter $formatter, Config $config, FileName $fileName)
32
    {
33
        $this->formatter = $formatter;
34
        $this->config = $config;
35
        $this->fileName = $fileName;
36
    }
37
38
    /**
39
     * Save queries to log.
40
     *
41
     * @param SqlQuery $query
42
     */
43
    public function save(SqlQuery $query)
44
    {
45
        $this->createDirectoryIfNotExists($query->number());
46
47
        $line = $this->formatter->getLine($query);
48
49
        if ($this->shouldLogQuery($query)) {
50
            $this->saveLine($line, $this->fileName->getForAllQueries(), $this->shouldOverrideFile($query));
51
        }
52
53
        if ($this->shouldLogSlowQuery($query)) {
54
            $this->saveLine($line, $this->fileName->getForSlowQueries());
55
        }
56
    }
57
58
    /**
59
     * Create directory if it does not exist yet.
60
     *
61
     * @param int $queryNumber
62
     */
63
    protected function createDirectoryIfNotExists($queryNumber)
64
    {
65
        if ($queryNumber == 1 && ! file_exists($directory = $this->directory())) {
66
            mkdir($directory, 0777, true);
67
        }
68
    }
69
70
    /**
71
     * Get directory where file should be located.
72
     *
73
     * @return string
74
     */
75
    protected function directory()
76
    {
77
        return rtrim($this->config->logDirectory(), '\\/');
78
    }
79
80
    /**
81
     * Verify whether query should be logged.
82
     *
83
     * @param SqlQuery $query
84
     *
85
     * @return bool
86
     */
87
    protected function shouldLogQuery(SqlQuery $query)
88
    {
89
        return $this->config->logAllQueries() &&
90
            preg_match($this->config->allQueriesPattern(), $query->raw());
91
    }
92
93
    /**
94
     * Verify whether slow query should be logged.
95
     *
96
     * @param SqlQuery $query
97
     *
98
     * @return bool
99
     */
100
    protected function shouldLogSlowQuery(SqlQuery $query)
101
    {
102
        return $this->config->logSlowQueries() && $query->time() >= $this->config->slowLogTime() &&
103
            preg_match($this->config->slowQueriesPattern(), $query->raw());
104
    }
105
106
    /**
107
     * Save data to log file.
108
     *
109
     * @param string $line
110
     * @param string $fileName
111
     * @param bool $override
112
     */
113
    protected function saveLine($line, $fileName, $override = false)
114
    {
115
        file_put_contents(
116
            $this->directory() . DIRECTORY_SEPARATOR . $fileName,
117
            $line,
118
            $override ? 0 : FILE_APPEND
119
        );
120
    }
121
122
    /**
123
     * Verify whether file should be overridden.
124
     *
125
     * @param SqlQuery $query
126
     *
127
     * @return bool
128
     */
129
    private function shouldOverrideFile(SqlQuery $query)
130
    {
131
        return ($query->number() == 1 && $this->config->overrideFile());
132
    }
133
}
134