ReportFile::screenshotWithCombineScreen()   A
last analyzed

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;
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
        $this->setNewLine();
28
29
        $this->reporter->addToTableOfContents($this->fileName());
30
    }
31
32
    /**
33
     * @param string|null $newLine
34
     *
35
     * @return $this
36
     */
37
    public function setNewLine(?string $newLine = null): static
38
    {
39
        $this->newLine = is_string($newLine) ? $newLine : '<br>' . PHP_EOL;
40
41
        return $this;
42
    }
43
44
    /**
45
     * @inheritDoc
46
     */
47
    public function isEmpty(): bool
48
    {
49
        $filePath = $this->reporter->reportFileName($this->fileName());
50
        clearstatcache();
51
52
        return !(file_exists($filePath) && filesize($filePath));
53
    }
54
55
    /**
56
     * @inheritDoc
57
     */
58
    public function raw(string $content = '', bool|string $newLine = false): static
59
    {
60
        return $this->addContent($content, $newLine);
61
    }
62
63
    /**
64
     * @inheritDoc
65
     */
66
    public function h1(string $content = '', bool|string $newLine = PHP_EOL . PHP_EOL): static
67
    {
68
        return $this->addContent("# {$content}", $newLine);
69
    }
70
71
    /**
72
     * @inheritDoc
73
     */
74
    public function h2(string $content = '', bool|string $newLine = PHP_EOL . PHP_EOL): static
75
    {
76
        return $this->addContent("## {$content}", $newLine);
77
    }
78
79
    /**
80
     * @inheritDoc
81
     */
82
    public function h3(string $content = '', bool|string $newLine = PHP_EOL . PHP_EOL): static
83
    {
84
        return $this->addContent("### {$content}", $newLine);
85
    }
86
87
    /**
88
     * @inheritDoc
89
     */
90
    public function h4(string $content = '', bool|string $newLine = PHP_EOL . PHP_EOL): static
91
    {
92
        return $this->addContent("#### {$content}", $newLine);
93
    }
94
95
    /**
96
     * @inheritDoc
97
     */
98
    public function h5(string $content = '', bool|string $newLine = PHP_EOL . PHP_EOL): static
99
    {
100
        return $this->addContent("##### {$content}", $newLine);
101
    }
102
103
    /**
104
     * @inheritDoc
105
     */
106
    public function h6(string $content = '', bool|string $newLine = PHP_EOL . PHP_EOL): static
107
    {
108
        return $this->addContent("###### {$content}", $newLine);
109
    }
110
111
    /**
112
     * @inheritDoc
113
     */
114
    public function p(string $content = '', bool|string $newLine = true): static
115
    {
116
        return $this->addContent($content, $newLine);
117
    }
118
119
    /**
120
     * @inheritDoc
121
     */
122
    public function br(int $count = 1, ?string $lineString = null): static
123
    {
124
        foreach (range(1, $count) as $num) {
125
            $this->addContent('', $lineString ?? true);
126
        }
127
128
        return $this;
129
    }
130
131
    /**
132
     * @inheritDoc
133
     */
134
    public function list(array|\ArrayAccess $items = [], bool|string $newLine = PHP_EOL, string $styleType = '-'): static
135
    {
136
        if (count($items)) {
137
            foreach ($items as $item) {
138
                $this->listItem($item, true, $styleType);
139
            }
140
            if ($newLine) {
141
                $this->br(lineString: is_string($newLine) ? $newLine : null);
142
            }
143
        }
144
145
        return $this;
146
    }
147
148
    /**
149
     * @inheritDoc
150
     */
151
    public function listItem(string $content = '', bool|string $newLine = PHP_EOL, string $styleType = '-'): static
152
    {
153
        return $this->addContent("{$styleType} {$content}", $newLine);
154
    }
155
156
    /**
157
     * @inheritDoc
158
     */
159
    public function image(string $url, string $alt = '', bool|string $newLine = true): static
160
    {
161
        return $this->addContent("![{$alt}]({$url})", $newLine);
162
    }
163
164
    /**
165
     * @inheritDoc
166
     */
167
    public function link(string $url, string $text = '', bool|string $newLine = false): static
168
    {
169
        return $this->addContent("[{$text}]({$url})", $newLine);
170
    }
171
172
    /**
173
     * @inheritDoc
174
     */
175
    public function screenshot(Browser $browser, ?string $resize = null, ?string $suffix = null, bool|string $newLine = true): static
176
    {
177
        if ($this->reporter->isScreenshotsDisabled()) {
178
            return $this;
179
        }
180
181
        $filename = $filepath = $this->reporter->screenshoter()->make($browser, $this->fileName(), $resize, $suffix);
182
183
        if ($this->reporter->useScreenshotRelativePath()) {
184
            $filepath = $this->filePrefix() . Str::afterLast($filepath, $this->filePrefix());
185
        }
186
187
        return $this->addContent("![{$filename}](./{$filepath})", $newLine);
188
    }
189
190
    /**
191
     * @inheritDoc
192
     */
193
    public function screenshotWithVisibleScreen(Browser $browser, ?string $suffix = null, bool|string $newLine = true): static
194
    {
195
        return $this->screenshot($browser, null, $suffix, $newLine);
196
    }
197
198
    /**
199
     * @inheritDoc
200
     */
201
    public function screenshotWithFitScreen(Browser $browser, ?string $suffix = null, bool|string $newLine = true): static
202
    {
203
        return $this->screenshot($browser, ReportScreenshotContract::RESIZE_FIT, $suffix, $newLine);
204
    }
205
206
    /**
207
     * @inheritDoc
208
     */
209
    public function screenshotWithCombineScreen(Browser $browser, ?string $suffix = null, bool|string $newLine = true): static
210
    {
211
        return $this->screenshot($browser, ReportScreenshotContract::RESIZE_COMBINE, $suffix, $newLine);
212
    }
213
214
    /**
215
     * @return string
216
     */
217
    public function fileName(): string
218
    {
219
        return $this->name;
220
    }
221
222
    /**
223
     * @return string
224
     */
225
    public function filePrefix(): string
226
    {
227
        return Str::afterLast($this->fileName(), '/');
228
    }
229
230
    /**
231
     * Save content.
232
     *
233
     * @param string $content
234
     * @param bool|string $newLine
235
     *
236
     * @return static
237
     */
238
    protected function addContent(string $content = '', bool|string $newLine = true): static
239
    {
240
        return $this->appendToFile($content . (is_string($newLine) ? $newLine : ($newLine ? $this->newLine : '')));
241
    }
242
243
    /**
244
     * Append content to file.
245
     *
246
     * @param string $content
247
     *
248
     * @return static
249
     */
250
    protected function appendToFile(string $content): static
251
    {
252
        if (!$this->reporter->isReportingDisabled()) {
253
            $filePath = $this->reporter->reportFileName($this->fileName());
254
255
            if (!is_dir($directoryPath = dirname($filePath))) {
256
                mkdir($directoryPath, 0777, true);
257
            }
258
259
            file_put_contents($filePath, $content, FILE_APPEND | LOCK_EX);
260
        }
261
262
        return $this;
263
    }
264
}
265