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 ( f38dc2...eafdef )
by Cees-Jan
12s
created

WebrootPreloadMiddleware::__construct()   C

Complexity

Conditions 7
Paths 20

Size

Total Lines 51
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 34
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 32
nc 20
nop 2
dl 0
loc 51
ccs 34
cts 34
cp 1
crap 7
rs 6.9743
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
2
3
namespace WyriHaximus\React\Http\Middleware;
4
5
use Narrowspark\Mimetypes\MimeTypeByExtensionGuesser;
6
use Psr\Http\Message\ServerRequestInterface;
7
use Psr\Log\LoggerInterface;
8
use RingCentral\Psr7\Response;
9
use ScriptFUSION\Byte\ByteFormatter;
10
use function RingCentral\Psr7\stream_for;
11
12
final class WebrootPreloadMiddleware
13
{
14
    /**
15
     * @var array
16
     */
17
    private $files = [];
18
19 10
    public function __construct(string $webroot, LoggerInterface $logger = null)
20
    {
21 10
        $totalSize = 0;
22 10
        $byteFormatter = (new ByteFormatter())->setPrecision(2)->setFormat('%v%u');
23 10
        $directory = new \RecursiveDirectoryIterator($webroot);
24 10
        $directory = new \RecursiveIteratorIterator($directory);
25 10
        $directory = iterator_to_array($directory);
26 10
        usort($directory, function ($a, $b) {
27 10
            return $a->getPathname() <=> $b->getPathname();
28 10
        });
29 10
        foreach ($directory as $fileinfo) {
30 10
            if (!$fileinfo->isFile()) {
31 10
                continue;
32
            }
33
34 10
            $filePath = str_replace(
35
                [
36 10
                    $webroot,
37 10
                    DIRECTORY_SEPARATOR,
38 10
                    '//',
39
                ],
40
                [
41 10
                    DIRECTORY_SEPARATOR,
42 10
                    '/',
43 10
                    '/',
44
                ],
45 10
                $fileinfo->getPathname()
46
            );
47
48 10
            $this->files[$filePath] = [
49 10
                'contents' => file_get_contents($fileinfo->getPathname()),
50
            ];
51
52 10
            $mime = MimeTypeByExtensionGuesser::guess($fileinfo->getExtension());
53 10
            if (is_null($mime)) {
54 10
                $mime = 'application/octet-stream';
55
            }
56 10
            list($mime) = explode(';', $mime);
57 10
            if (strpos($mime, '/') !== false) {
58 10
                $this->files[$filePath]['mime'] = $mime;
59
            }
60
61 10
            if ($logger instanceof LoggerInterface) {
62 1
                $fileSize = strlen($this->files[$filePath]['contents']);
63 1
                $totalSize += $fileSize;
64 10
                $logger->debug($filePath . ': ' . $byteFormatter->format($fileSize));
65
            }
66
        }
67
68 10
        if ($logger instanceof LoggerInterface) {
69 1
            $logger->info('Preloaded ' . count($this->files) . ' file(s) with a combined size of ' . $byteFormatter->format($totalSize) . ' from "' . $webroot . '" into memory');
70
        }
71 10
    }
72
73 9
    public function __invoke(ServerRequestInterface $request, callable $next)
74
    {
75 9
        $path = $request->getUri()->getPath();
76 9
        if (!isset($this->files[$path])) {
77 1
            return $next($request);
78
        }
79
80 8
        $response = (new Response(200))->withBody(stream_for($this->files[$path]['contents']));
81 8
        if (!isset($this->files[$path]['mime'])) {
82
            return $response;
83
        }
84
85 8
        return $response->withHeader('Content-Type', $this->files[$path]['mime']);
86
    }
87
}
88