Passed
Push — develop ( 4f274b...57ac8e )
by Brent
02:25
created

DevelopmentServer::make()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 3
dl 0
loc 8
ccs 2
cts 2
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Stitcher\Application;
4
5
use GuzzleHttp\Psr7\Response;
6
use Stitcher\Task\PartialParse;
7
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
8
9
class DevelopmentServer extends Server
10
{
11
    protected $rootDirectory;
12
    protected $path = null;
13
    protected $partialParse;
14
15 4
    public function __construct(
16
        string $rootDirectory,
17
        PartialParse $partialParse,
18
        string $path = null
19
    ) {
20 4
        $this->rootDirectory = $rootDirectory;
21 4
        $this->path = $path;
22 4
        $this->partialParse = $partialParse;
23 4
    }
24
25 2
    public static function make(
26
        string $rootDirectory,
27
        PartialParse $partialParse,
28
        string $path = null
29
    ): DevelopmentServer
30
    {
31 2
        return new self($rootDirectory, $partialParse, $path);
32
    }
33
34 2
    protected function handleStaticRoute(): ?Response
35
    {
36 2
        $path = $this->path ?? $this->getCurrentPath();
37
38 2
        $this->partialParse->setFilter($path);
39
40
        try {
41 2
            $this->partialParse->execute();
42
43 2
            $filename = ltrim($path === '/' ? 'index.html' : "{$path}.html", '/');
44
45 2
            $body = @file_get_contents("{$this->rootDirectory}/{$filename}");
46
47 2
            if (! $body) {
48
                return null;
49
            }
50
51 2
            return new Response(200, [], $body);
52
        } catch (ResourceNotFoundException $e) {
53
            return null;
54
        }
55
    }
56
}
57