Passed
Push — master ( 99257e...b8bfe3 )
by Yaroslav
02:35
created

ReportFile::listItem()   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 list(array|\ArrayAccess $items = [], bool|string $newLine = true, string $styleType = '-'): static
126
    {
127
        foreach ($items as $item) {
128
            $this->listItem($item, true, $styleType);
129
        }
130
        if ($newLine) {
131
            $this->br();
132
        }
133
    }
134
135
    /**
136
     * @inheritDoc
137
     */
138
    public function listItem(string $content = '', bool|string $newLine = true, string $styleType = '-'): static
139
    {
140
        return $this->addContent("{$styleType} {$content}", $newLine);
141
    }
142
143
    /**
144
     * @inheritDoc
145
     */
146
    public function image(string $url, string $alt = '', bool|string $newLine = true): static
147
    {
148
        return $this->addContent("![{$alt}]({$url})", $newLine);
149
    }
150
151
    /**
152
     * @inheritDoc
153
     */
154
    public function link(string $url, string $text = '', bool|string $newLine = true): static
155
    {
156
        return $this->addContent("[{$text}]({$url})", $newLine);
157
    }
158
159
    /**
160
     * @inheritDoc
161
     */
162
    public function screenshot(Browser $browser, ?string $resize = null, ?string $suffix = null, bool|string $newLine = true): static
163
    {
164
        if ($this->reporter->isScreenshotsDisabled()) {
165
            return $this;
166
        }
167
168
        $filename = $filepath = $this->reporter->screenshoter()->make($browser, $this->fileName(), $resize, $suffix);
169
170
        if ($this->reporter->useScreenshotRelativePath()) {
171
            $filepath = $this->filePrefix() . Str::afterLast($filepath, $this->filePrefix());
172
        }
173
174
        return $this->addContent("![{$filename}](./{$filepath})", $newLine);
175
    }
176
177
    /**
178
     * @inheritDoc
179
     */
180
    public function screenshotWithVisibleScreen(Browser $browser, ?string $suffix = null, bool|string $newLine = true): static
181
    {
182
        return $this->screenshot($browser, null, $suffix, $newLine);
183
    }
184
185
    /**
186
     * @inheritDoc
187
     */
188
    public function screenshotWithFitScreen(Browser $browser, ?string $suffix = null, bool|string $newLine = true): static
189
    {
190
        return $this->screenshot($browser, ReportScreenshotContract::RESIZE_FIT, $suffix, $newLine);
191
    }
192
193
    /**
194
     * @inheritDoc
195
     */
196
    public function screenshotWithCombineScreen(Browser $browser, ?string $suffix = null, bool|string $newLine = true): static
197
    {
198
        return $this->screenshot($browser, ReportScreenshotContract::RESIZE_COMBINE, $suffix, $newLine);
199
    }
200
201
    /**
202
     * @return string
203
     */
204
    public function fileName(): string
205
    {
206
        return $this->name;
207
    }
208
209
    /**
210
     * @return string
211
     */
212
    public function filePrefix(): string
213
    {
214
        $array = array_reverse(explode('/', $this->fileName()));
215
216
        return $array[0] ?? '';
217
    }
218
219
    /**
220
     * Save content
221
     *
222
     * @param string $content
223
     * @param bool|string $newLine
224
     *
225
     * @return static
226
     */
227
    protected function addContent(string $content = '', bool|string $newLine = true): static
228
    {
229
        $newLineText = '';
230
        if ($newLine) {
231
            $newLineText = is_string($newLine) ? $newLine : $this->newLine;
232
        }
233
234
        $this->appendToFile($content . $newLineText);
235
236
        return $this;
237
    }
238
239
    /**
240
     * Append content to file
241
     *
242
     * @param string $content
243
     *
244
     * @return void
245
     */
246
    protected function appendToFile(string $content): void
247
    {
248
        if (!$this->reporter->isReportingDisabled()) {
249
            $filePath = $this->reporter->reportFileName($this->fileName());
250
251
            $directoryPath = dirname($filePath);
252
253
            if (!is_dir($directoryPath)) {
254
                mkdir($directoryPath, 0777, true);
255
            }
256
257
            file_put_contents($filePath, $content, FILE_APPEND | LOCK_EX);
258
        }
259
    }
260
}
261