Passed
Push — master ( 183777...4a21d6 )
by Yaroslav
10:56
created

ReportFile::screenshotWithCombineScreen()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 2
1
<?php
2
3
namespace LaravelDuskReporter\Generation;
4
5
use Illuminate\Support\Str;
6
use Laravel\Dusk\Browser;
7
use LaravelDuskReporter\Reporter;
8
9
class ReportFile implements ReportFileContract
10
{
11
    protected Reporter $reporter;
12
13
    protected string $name;
14
15
    protected string $newLine = PHP_EOL;
16
17
    /**
18
     * ReportFile constructor.
19
     *
20
     * @param Reporter $reporter
21
     * @param string $name
22
     */
23
    public function __construct(Reporter $reporter, string $name)
24
    {
25
        $this->reporter = $reporter;
26
        $this->name     = $name;
27
28
        $this->reporter->addToTableOfContents($this->fileName());
29
    }
30
31
    /**
32
     * @param string|null $newLine
33
     *
34
     * @return $this
35
     */
36
    public function setNewLine(?string $newLine): static
37
    {
38
        $this->newLine = is_string($newLine) ? $newLine : PHP_EOL;
39
40
        return $this;
41
    }
42
43
    /**
44
     * @inheritDoc
45
     */
46
    public function isEmpty(): bool
47
    {
48
        $filePath = $this->reporter->reportFileName($this->fileName());
49
        clearstatcache();
50
51
        return !(file_exists($filePath) && filesize($filePath));
52
    }
53
54
    /**
55
     * @inheritDoc
56
     */
57
    public function raw(string $content = '', bool|string $newLine = false): static
58
    {
59
        return $this->addContent($content, $newLine);
60
    }
61
62
    /**
63
     * @inheritDoc
64
     */
65
    public function h1(string $content = '', bool|string $newLine = true): static
66
    {
67
        return $this->addContent("# {$content}", $newLine);
68
    }
69
70
    /**
71
     * @inheritDoc
72
     */
73
    public function h2(string $content = '', bool|string $newLine = true): static
74
    {
75
        return $this->addContent("## {$content}", $newLine);
76
    }
77
78
    /**
79
     * @inheritDoc
80
     */
81
    public function h3(string $content = '', bool|string $newLine = true): static
82
    {
83
        return $this->addContent("### {$content}", $newLine);
84
    }
85
86
    /**
87
     * @inheritDoc
88
     */
89
    public function h4(string $content = '', bool|string $newLine = true): static
90
    {
91
        return $this->addContent("#### {$content}", $newLine);
92
    }
93
94
    /**
95
     * @inheritDoc
96
     */
97
    public function h5(string $content = '', bool|string $newLine = true): static
98
    {
99
        return $this->addContent("##### {$content}", $newLine);
100
    }
101
102
    /**
103
     * @inheritDoc
104
     */
105
    public function h6(string $content = '', bool|string $newLine = true): static
106
    {
107
        return $this->addContent("###### {$content}", $newLine);
108
    }
109
110
    /**
111
     * @inheritDoc
112
     */
113
    public function br(int $count = 1): static
114
    {
115
        foreach (range(1, $count) as $num) {
116
            $this->addContent('', true);
117
        }
118
119
        return $this;
120
    }
121
122
    /**
123
     * @inheritDoc
124
     */
125
    public function image(string $url, string $alt = '', bool|string $newLine = true): static
126
    {
127
        return $this->addContent("![{$alt}]({$url})", $newLine);
128
    }
129
130
    /**
131
     * @inheritDoc
132
     */
133
    public function link(string $url, string $text = '', bool|string $newLine = true): static
134
    {
135
        return $this->addContent("[{$text}]({$url})", $newLine);
136
    }
137
138
    /**
139
     * @inheritDoc
140
     */
141
    public function screenshot(Browser $browser, ?string $resize = null, ?string $suffix = null, bool|string $newLine = true): static
142
    {
143
        if ($this->reporter->isScreenshotsDisabled()) {
144
            return $this;
145
        }
146
147
        $filename = $filepath = $this->reporter->screenshoter()->make($browser, $this->fileName(), $resize, $suffix);
148
149
        if ($this->reporter->useScreenshotRelativePath()) {
150
            $filepath = $this->filePrefix() . Str::afterLast($filepath, $this->filePrefix());
151
        }
152
153
        return $this->addContent("![{$filename}](./{$filepath})", $newLine);
154
    }
155
156
    /**
157
     * @inheritDoc
158
     */
159
    public function screenshotWithVisibleScreen(Browser $browser, ?string $suffix = null, bool|string $newLine = true): static
160
    {
161
        return $this->screenshot($browser, null, $suffix, $newLine);
162
    }
163
164
    /**
165
     * @inheritDoc
166
     */
167
    public function screenshotWithFitScreen(Browser $browser, ?string $suffix = null, bool|string $newLine = true): static
168
    {
169
        return $this->screenshot($browser, ReportScreenshotContract::RESIZE_FIT, $suffix, $newLine);
170
    }
171
172
    /**
173
     * @inheritDoc
174
     */
175
    public function screenshotWithCombineScreen(Browser $browser, ?string $suffix = null, bool|string $newLine = true): static
176
    {
177
        return $this->screenshot($browser, ReportScreenshotContract::RESIZE_COMBINE, $suffix, $newLine);
178
    }
179
180
    /**
181
     * @return string
182
     */
183
    public function fileName(): string
184
    {
185
        return $this->name;
186
    }
187
188
    /**
189
     * @return string
190
     */
191
    public function filePrefix(): string
192
    {
193
        $array = array_reverse(explode('/', $this->fileName()));
194
195
        return $array[0] ?? '';
196
    }
197
198
    /**
199
     * Save content
200
     *
201
     * @param string $content
202
     * @param bool|string $newLine
203
     *
204
     * @return static
205
     */
206
    protected function addContent(string $content = '', bool|string $newLine = true): static
207
    {
208
        $newLineText = '';
209
        if ($newLine) {
210
            $newLineText = is_string($newLine) ? $newLine : $this->newLine;
211
        }
212
213
        $this->appendToFile($content . $newLineText);
214
215
        return $this;
216
    }
217
218
    /**
219
     * Append content to file
220
     *
221
     * @param string $content
222
     *
223
     * @return void
224
     */
225
    protected function appendToFile(string $content): void
226
    {
227
        if (!$this->reporter->isReportingDisabled()) {
228
            $filePath = $this->reporter->reportFileName($this->fileName());
229
230
            $directoryPath = dirname($filePath);
231
232
            if (!is_dir($directoryPath)) {
233
                mkdir($directoryPath, 0777, true);
234
            }
235
236
            file_put_contents($filePath, $content, FILE_APPEND | LOCK_EX);
237
        }
238
    }
239
}
240