Completed
Push — master ( 9da0fd...2ae4f2 )
by Tomáš
9s
created

ResponseWriter::prepareResponseMessage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Symplify
7
 * Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz).
8
 */
9
10
namespace Symplify\PHP7_Sculpin\HttpServer;
11
12
use React\Http\Request;
13
use React\Http\Response;
14
use Symfony\Component\Console\Output\OutputInterface;
15
16
final class ResponseWriter
17
{
18
    /**
19
     * @var OutputInterface
20
     */
21
    private $output;
22
23 4
    public function __construct(OutputInterface $output)
24
    {
25 4
        $this->output = $output;
26 4
    }
27
28 1 View Code Duplication
    public function send404Response(Request $request, Response $response)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
29
    {
30 1
        $this->writeResponse(404, $request->getPath());
31 1
        $response->writeHead(404, [
32 1
            'Content-Type' => 'text/html',
33
        ]);
34
35 1
        $response->end(
36
            '<h1>404 - Not Found</h1>'.
37 1
            '<p>Sculpin web server could not find the requested resource.</p>'
38
        );
39 1
    }
40
41 1 View Code Duplication
    public function send200Response(Request $request, Response $response, string $path)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
    {
43 1
        $response->writeHead(200, [
44 1
            'Content-Type' => 'text/html',
45
        ]);
46
47 1
        $this->writeResponse(200, $request->getPath());
48 1
        $response->end(file_get_contents($path));
49 1
    }
50
51 2
    private function writeResponse(int $responseCode, string $path)
52
    {
53 2
        $message = sprintf(
54 2
            'Response code "%s" for path: "%s"',
55
            $responseCode,
56
            $path
57
        );
58
59 2
        $this->output->writeln($this->prepareResponseMessage($responseCode, $message));
60 2
    }
61
62 2
    private function prepareResponseMessage(int $responseCode, string $message) : string
63
    {
64 2
        $wrapOpen = '<info>';
65 2
        $wrapClose = '</info>';
66 2
        if ($responseCode >= 400) {
67 1
            $wrapOpen = '<error>';
68 1
            $wrapClose = '</error>';
69
        }
70
71 2
        return $wrapOpen.$message.$wrapClose;
72
    }
73
}
74