Passed
Push — master ( dd3fe0...2f26d2 )
by Zlatin
02:38
created

getCurrentUrl()   D

Complexity

Conditions 9
Paths 36

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 19
nc 36
nop 0
dl 0
loc 31
rs 4.909
c 1
b 0
f 0
1
<?php
2
include_once './vendor/autoload.php';
3
4
use Psr\Http\Message\RequestInterface;
5
use Psr\Http\Message\ResponseInterface;
6
7
$router = new \DevOp\Core\Router\Router();
8
9
$router->add('homepage', ['GET', 'POST'], '/', function(RequestInterface $request, ResponseInterface $response) {
10
    $body = $response->withStatus(404);
11
    $body->getBody()->write('Error 404');
12
    echo $body->getBody();
13
});
14
$router->add('anything_else', ['GET'], '/[page:\w]', function() {
15
    echo "ANYTHING ELSE" . PHP_EOL;
16
});
17
18
$router->any('anything_else', '/users/[action:\w]/[id:\d+]', function() {
19
    echo "ANYTHING ELSE" . PHP_EOL;
20
});
21
22
function getCurrentUrl()
23
{
24
    $url = '';
25
    
26
    if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'On') {
27
        $url .= 'https://';
28
    } else if (isset($_SERVER['REQUEST_SCHEME'])) {
29
        $url .=  $_SERVER['REQUEST_SCHEME'];
30
    }
31
    
32
    $url .= "://";
33
    
34
    if (isset($_SERVER['HTTP_HOST'])) {
35
        $url .= $_SERVER['HTTP_HOST'];
36
    } else if (isset($_SERVER['SERVER_NAME'])) {
37
        $url .= $_SERVER['SERVER_NAME'];
38
    } else {
39
        $url .= 'localhost';
40
    }
41
    
42
    if (isset($_SERVER['SERVER_PORT']) && !in_array($_SERVER['SERVER_PORT'], [80, 443])) {
43
        $url .= ':' . $_SERVER['SERVER_PORT'];
44
    }
45
    
46
    if (isset($_SERVER['BASE'])) {
47
        $url .= str_replace($_SERVER['BASE'], '', $_SERVER['REQUEST_URI']);
48
    } else {
49
        $url .= $_SERVER['REQUEST_URI'];
50
    }
51
    
52
    return $url;
53
}
54
55
$uri = (new \DevOp\Core\Http\Factory\UriFactory())->createUri(getCurrentUrl());
56
$request = (new DevOp\Core\Http\Factory\RequestFactory())->createRequest('GET', $uri);
57
$response = (new DevOp\Core\Http\Factory\ResponseFactory())->createResponse(200);
58
$router->dispatch($request, $response);
59