Completed
Pull Request — master (#74)
by
unknown
02:42
created

Phroute::__invoke()   A

Complexity

Conditions 3
Paths 11

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 13
nc 11
nop 3
1
<?php
2
3
namespace Psr7Middlewares\Middleware;
4
5
use Psr\Http\Message\ServerRequestInterface;
6
use Psr\Http\Message\ResponseInterface;
7
use Phroute\Phroute\Dispatcher;
8
9
class Phroute
10
{
11
12
    /**
13
     * @var Dispatcher Phroute dispatcher
14
     */
15
    private $router;
16
17
    /**
18
     * Set the Dispatcher instance.
19
     *
20
     * @param Dispatcher|null $router
21
     */
22
    public function __construct(Dispatcher $router)
23
    {
24
        $this->router = $router;
25
    }
26
27
    /**
28
     * Execute the middleware.
29
     *
30
     * @param ServerRequestInterface $request
31
     * @param ResponseInterface      $response
32
     * @param callable               $next
33
     *
34
     * @return ResponseInterface
35
     */
36
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) {
37
        try {
38
            ob_start();
39
            $this->router->dispatch($request->getMethod(), $request->getUri()->getPath());
40
            $bufferedBody = ob_get_clean();
41
            $response->getBody()->write($bufferedBody);
42
            $response = $response->withStatus(200);
43
        }
44
        catch (\Phroute\Phroute\Exception\HttpRouteNotFoundException $e) {
0 ignored issues
show
Bug introduced by
The class Phroute\Phroute\Exceptio...pRouteNotFoundException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
45
                $reponse = new \Zend\Diactoros\Response\HtmlResponse($e->getMessage(), 404);
0 ignored issues
show
Unused Code introduced by
$reponse is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
46
        }
47
        catch (\Phroute\Phroute\Exception\BadRouteException $e) {
0 ignored issues
show
Bug introduced by
The class Phroute\Phroute\Exception\BadRouteException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
48
            $allowedMethods = $routeInfo[1];
0 ignored issues
show
Bug introduced by
The variable $routeInfo does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Unused Code introduced by
$allowedMethods is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
49
                $reponse = new \Zend\Diactoros\Response\HtmlResponse($e->getMessage(), 405);
0 ignored issues
show
Unused Code introduced by
$reponse is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
50
        }
51
        return $next($request, $response);
52
    }
53
}
54