Completed
Push — master ( d4e593...c1d04e )
by Iman
20:25 queued 13:41
created

RouteAuthorizer::authorizeMatchedRoutes()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 1
nop 0
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Imanghafoori\HeyMan;
4
5
use Illuminate\Routing\Events\RouteMatched;
6
use Illuminate\Support\Facades\Route;
7
use Imanghafoori\HeyMan\WatchingStrategies\RouterEventManager;
8
9
class RouteAuthorizer
10
{
11 87
    public function authorizeMatchedRoutes()
12
    {
13
        Route::matched(function (RouteMatched $eventObj) {
14 44
            $route = $eventObj->route;
15 44
            foreach (['GET', 'POST', 'PUT', 'PATCH', 'DELETE'] as $verb) {
16 44
                if ($eventObj->request->method() === $verb) {
17 44
                    $this->authorizeUrls($verb.$route->uri);
18
                }
19
            }
20 28
            $this->authorizeRouteNames($route->getName());
21 26
            $this->authorizeRouteActions($route->getActionName());
22 87
        });
23 87
    }
24
25
    /**
26
     * @param $actionName
27
     *
28
     * @throws \Illuminate\Auth\Access\AuthorizationException
29
     */
30 26
    private function authorizeRouteActions($actionName)
31
    {
32 26
        $this->setGuardFor('Actions', $actionName);
33 24
    }
34
35
    /**
36
     * @param $routeName
37
     *
38
     * @throws \Illuminate\Auth\Access\AuthorizationException
39
     */
40 28
    private function authorizeRouteNames($routeName)
41
    {
42 28
        $this->setGuardFor('RouteNames', $routeName);
43 26
    }
44
45
    /**
46
     * @param $url
47
     *
48
     * @throws \Illuminate\Auth\Access\AuthorizationException
49
     */
50 44
    public function authorizeUrls($url)
51
    {
52 44
        $this->setGuardFor('Urls', $url);
53 28
    }
54
55
    /**
56
     * @param $method
57
     * @param $key
58
     */
59 44
    private function setGuardFor(string $method, $key)
60
    {
61 44
        $method = 'get'.$method;
62 44
        $closures = app(RouterEventManager::class)->{$method}($key);
63 44
        foreach ($closures as $cb) {
64 44
            $cb();
65
        }
66 28
    }
67
}
68