Passed
Push — develop ( 429d81...1647ec )
by Brent
02:48
created

DevelopmentServer::run()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.1406

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 4
nop 0
dl 0
loc 16
ccs 6
cts 8
cp 0.75
crap 3.1406
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
            return new Response(200, [], $body);
48
        } catch (ResourceNotFoundException $e) {
49
            return null;
50
        }
51
    }
52
}
53