Test Failed
Push — master ( 22b809...326de4 )
by Brent
05:15 queued 39s
created

DevelopmentServer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 9
ccs 0
cts 5
cp 0
crap 2
rs 9.9666
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
    /** @var string */
12
    protected $rootDirectory;
13
14
    /** @var null|string */
15
    protected $path;
16
17
    /** @var \Stitcher\Task\PartialParse */
18
    protected $partialParse;
19
20
    public function __construct(
21
        string $rootDirectory,
22
        PartialParse $partialParse,
23
        string $path = null
24
    ) {
25
        $this->rootDirectory = $rootDirectory;
26
        $this->path = $path;
27
        $this->partialParse = $partialParse;
28
    }
29
30
    public static function make(
31
        string $rootDirectory,
32
        PartialParse $partialParse,
33
        string $path = null
34
    ): DevelopmentServer
35
    {
36
        return new self($rootDirectory, $partialParse, $path);
37
    }
38
39
    protected function handleStaticRoute(): ?Response
40
    {
41
        $path = $this->path ?? $this->getCurrentPath();
42
43
        $this->partialParse->setFilter($path);
44
45
        try {
46
            $this->partialParse->execute();
47
48
            $filename = ltrim($path === '/' ? 'index.html' : "{$path}.html", '/');
49
50
            $body = @file_get_contents("{$this->rootDirectory}/{$filename}");
51
52
            return $body ? new Response(200, [], $body) : null;
53
        } catch (ResourceNotFoundException $e) {
54
            return null;
55
        }
56
    }
57
}
58