GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 554531...ef01a1 )
by Maurice
58s
created

SvgFixerMiddleware::handle()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4555
c 0
b 0
f 0
cc 5
nc 2
nop 2
1
<?php
2
3
namespace DigiFactory\SvgFixer;
4
5
use Closure;
6
use Illuminate\Http\Request;
7
use Illuminate\Http\UploadedFile;
8
use Illuminate\Support\Str;
9
10
class SvgFixerMiddleware
11
{
12
    protected $response;
13
    protected $xmlDeclaration = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>';
14
15
    public function handle(Request $request, Closure $next)
16
    {
17
        // Check if request is a POST request and has at least one file
18
        if ($request->method() === 'POST' && $request->files->count() > 0) {
19
            /** @var UploadedFile $file */
20
            foreach ($request->files as $file) {
21
                // Check if uploaded file is an SVG
22
                if (Str::startsWith($file->getMimeType(), 'image/svg')) {
23
                    $this->handleSVG($file);
24
                }
25
            }
26
        }
27
28
        return $next($request);
29
    }
30
31
    private function handleSVG($file)
32
    {
33
        $handle = fopen($file->getPathname(), 'r+');
34
        $contents = fread($handle, filesize($file->getPathname()));
35
36
        rewind($handle);
37
        // Check if uploaded file is an SVG with starting XML declaration.
38
        // If not then we add the XML declaration.
39
        if (Str::startsWith($contents, '<svg')) {
40
            fwrite($handle, $this->xmlDeclaration.PHP_EOL.$contents);
41
        }
42
43
        fclose($handle);
44
    }
45
}
46