Completed
Push — master ( bb1936...2de20d )
by Marcin
03:24
created

FileName::suffix()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 0
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\Contracts\Foundation\Application;
7
8
class FileName
9
{
10
    /**
11
     * @var Application
12
     */
13
    private $app;
14
15
    /**
16
     * @var Config
17
     */
18
    private $config;
19
20
    /**
21
     * @var Carbon
22
     */
23
    private $now;
24
25
    /**
26
     * FileName constructor.
27
     *
28
     * @param Application $app
29
     * @param Config $config
30
     */
31
    public function __construct(Application $app, Config $config)
32
    {
33
        $this->app = $app;
34
        $this->config = $config;
35
        $this->now = Carbon::now();
36
    }
37
38
    /**
39
     * Get file name for all queries log.
40
     *
41
     * @return string
42
     */
43
    public function getForAllQueries()
44
    {
45
        return $this->createFileName($this->config->allQueriesFileName());
46
    }
47
48
    /**
49
     * Get file name for slow queries log.
50
     *
51
     * @return string
52
     */
53
    public function getForSlowQueries()
54
    {
55
        return $this->createFileName($this->config->slowQueriesFileName());
56
    }
57
58
    /**
59
     * Create file name.
60
     *
61
     * @param string $fileName
62
     *
63
     * @return string
64
     */
65
    protected function createFileName($fileName)
66
    {
67
        return $this->parseFileName($fileName) . $this->suffix() . $this->config->fileExtension();
68
    }
69
70
    /**
71
     * Get file suffix.
72
     *
73
     * @return string
74
     */
75
    protected function suffix()
76
    {
77
        return $this->app->runningInConsole() ? $this->config->consoleSuffix() : '';
78
    }
79
80
    /**
81
     * Parse file name to include date in it.
82
     *
83
     * @param string $fileName
84
     *
85
     * @return string
86
     */
87
    protected function parseFileName($fileName)
88
    {
89
        return preg_replace_callback('#(\[.*\])#U', function ($matches) {
90
            $format = str_replace(['[',']'], [], $matches[1]);
91
92
            return $this->now->format($format);
93
        }, $fileName);
94
    }
95
}
96