DetectRoute   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 6
dl 0
loc 43
ccs 0
cts 18
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 18 2
A handleMatchedRoute() 0 9 1
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