MarkdownDumper::addExplanations()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Cerbero\SqlDumper\Dumpers;
4
5
use SqlFormatter;
6
7
/**
8
 * The markdown dumper.
9
 *
10
 */
11
class MarkdownDumper extends FileDumper
12
{
13
    /**
14
     * The file extension.
15
     *
16
     * @var string
17
     */
18
    protected $extension = 'md';
19
20
    /**
21
     * Add the given query to dump
22
     *
23
     * @param string $query
24
     * @return DumperInterface
25
     */
26 3
    public function addQuery(string $query): DumperInterface
27
    {
28 3
        $this->content .= '### Query' . PHP_EOL;
29 3
        $this->content .= $this->formatSqlBlock($query);
30
31 3
        return $this;
32
    }
33
34
    /**
35
     * Format an SQL code block for the given query
36
     *
37
     * @param string $query
38
     * @return string
39
     */
40 3
    protected function formatSqlBlock(string $query): string
41
    {
42 3
        $formattedQuery = SqlFormatter::format($query, false);
43
44 3
        return '```sql' . PHP_EOL . $formattedQuery . PHP_EOL . '```' . PHP_EOL;
45
    }
46
47
    /**
48
     * Add the query execution time to dump
49
     *
50
     * @param float $milliseconds
51
     * @return DumperInterface
52
     */
53 3
    public function addTime(float $milliseconds): DumperInterface
54
    {
55 3
        $seconds = $milliseconds / 1000;
56
57 3
        $this->content .= "Execution time in seconds: **{$seconds}**" . PHP_EOL;
58
59 3
        return $this;
60
    }
61
62
    /**
63
     * Add the query caller to dump
64
     *
65
     * @param string $file
66
     * @param int $line
67
     * @return DumperInterface
68
     */
69 3
    public function addCaller(string $file, int $line): DumperInterface
70
    {
71 3
        $this->content .= "Executed in file `{$file}` on line **{$line}**" . PHP_EOL;
72
73 3
        return $this;
74
    }
75
76
    /**
77
     * Add the given explanation rows to dump
78
     *
79
     * @param array $explanationRows
80
     * @return DumperInterface
81
     */
82 3
    public function addExplanations(array $explanationRows): DumperInterface
83
    {
84 3
        $this->content .= '### Explanation' . PHP_EOL;
85
86 3
        $headers = array_keys((array) $explanationRows[0]);
87
88 3
        $this->content .= $this->formatTable($headers, $explanationRows);
89
90 3
        return $this;
91
    }
92
93
    /**
94
     * Format a markdown table with the given headers and rows
95
     *
96
     * @param array $headers
97
     * @param array $rows
98
     * @return string
99
     */
100 3
    protected function formatTable(array $headers, array $rows): string
101
    {
102 3
        $table = implode(' | ', $headers) . PHP_EOL;
103 3
        $table .= str_repeat('---|', count($headers)) . PHP_EOL;
104
105 3
        foreach ($rows as $row) {
106 3
            $table .= implode(' | ', (array) $row) . PHP_EOL;
107
        }
108
109 3
        return $table;
110
    }
111
112
    /**
113
     * Add a separator
114
     *
115
     * @return DumperInterface
116
     */
117 3
    public function addSeparator(): DumperInterface
118
    {
119 3
        $this->content .= '---' . PHP_EOL;
120
121 3
        return $this;
122
    }
123
124
    /**
125
     * Add the execution time of all queries to dump
126
     *
127
     * @param float $milliseconds
128
     * @return DumperInterface
129
     */
130 3
    public function addTotalTime(float $milliseconds): DumperInterface
131
    {
132 3
        $seconds = $milliseconds / 1000;
133
134 3
        $this->content .= "Total queries execution time in seconds: **{$seconds}**" . PHP_EOL;
135
136 3
        return $this;
137
    }
138
}
139