Passed
Push — develop ( 80559e...1e38ed )
by Brent
02:12
created

DevelopmentServer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 3
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 1
rs 9.6666
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