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   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 36
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 15 5
A handleSVG() 0 14 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