DetectRoute::handleMatchedRoute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 6
cp 0
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Loadsman\LaravelPlugin\Http\Middleware;
4
5
use Loadsman\LaravelPlugin\Entities\RouteInfo;
6
use Closure;
7
use Illuminate\Contracts\Events\Dispatcher;
8
use Illuminate\Http\Request;
9
use Illuminate\Routing\Events\RouteMatched;
10
11
/**
12
 * Class DetectRouteMiddleware
13
 *
14
 * @package \Loadsman\Laravel\Http\Middleware
15
 */
16
class DetectRoute
17
{
18
    const ROUTE_INFO = 'route-info';
19
20
    /**
21
     * @type \Illuminate\Contracts\Events\Dispatcher
22
     */
23
    protected $events;
24
25
    public function __construct(Dispatcher $dispatcher)
26
    {
27
        $this->events = $dispatcher;
28
    }
29
30
    public function handle(Request $request, Closure $next)
31
    {
32
        // In case the request was sent by Api Tester and route info is wanted
33
        // we will halt the request and output route information instead.
34
        if ($request->header('X-Api-Tester') === static::ROUTE_INFO) {
35
36
            // Laravel 5.1 event
37
            $this->events->listen('router.matched', [$this, 'handleMatchedRoute']);
38
39
            // Laravel 5.2 event
40
            $this->events->listen(RouteMatched::class,
41
                function (RouteMatched $event) {
42
                    $this->handleMatchedRoute($event->route);
43
                });
44
        }
45
46
        return $next($request);
47
    }
48
49
    public function handleMatchedRoute($route){
50
        $routeInfo = new RouteInfo($route);
51
52
        response()->json([
53
            'data' => $routeInfo,
54
        ])->send();
55
56
        exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The method handleMatchedRoute() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
57
    }
58
}
59