Passed
Push — develop ( 80559e...1e38ed )
by Brent
02:12
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 Stitcher\Command\PartialParse;
6
use Stitcher\Exception\Http;
7
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
8
9
class DevelopmentServer
10
{
11
    protected $rootDirectory;
12
    protected $uri = null;
13
    protected $partialParse;
14
15 3
    public function __construct(
16
        string $rootDirectory,
17
        PartialParse $partialParse,
18
        string $uri = null
19
    ) {
20 3
        $this->rootDirectory = $rootDirectory;
21 3
        $this->uri = $uri;
22 3
        $this->partialParse = $partialParse;
23 3
    }
24
25 2
    public static function make(
26
        string $rootDirectory,
27
        PartialParse $partialParse,
28
        string $uri = null
29
    ): DevelopmentServer
30
    {
31 2
        return new self($rootDirectory, $partialParse, $uri);
32
    }
33
34 2
    public function run(): string
35
    {
36 2
        $uri = $this->uri ?? $_SERVER['REQUEST_URI'];
37
38 2
        $this->partialParse->setFilter($uri);
39
40
        try {
41 2
            $this->partialParse->execute();
42
43 2
            $filename = ltrim($uri === '/' ? 'index.html' : "{$uri}.html", '/');
44
45 2
            return @file_get_contents("{$this->rootDirectory}/{$filename}");
46
        } catch (ResourceNotFoundException $e) {
47
            throw Http::notFound($uri);
48
        }
49
    }
50
}
51