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

DevelopmentServer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 86.67%

Importance

Changes 0
Metric Value
dl 0
loc 42
ccs 13
cts 15
cp 0.8667
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A make() 0 8 1
A run() 0 16 3
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