Passed
Push — master ( 5160e5...b38953 )
by Koen
03:01
created

RouteServiceProvider::resolveRequestSignature()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 3
eloc 5
c 1
b 1
f 0
nc 3
nop 1
dl 0
loc 9
rs 10
ccs 0
cts 0
cp 0
crap 12
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Providers;
6
7
use App\Contracts\Http\Responses\ResponseFactory as ResponseFactoryContract;
8
use App\Http\Responses\ResponseFactory;
9
use Illuminate\Cache\RateLimiter;
10
use Illuminate\Cache\RateLimiting\Limit;
11
use Illuminate\Contracts\Container\Container;
12
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
13
use Illuminate\Http\Request;
14
use Illuminate\Routing\Router;
15
use RuntimeException;
16
use function sha1;
17
18
final class RouteServiceProvider extends ServiceProvider
19
{
20
    protected $namespace = '\\App\\Http\\Controllers\\';
21
22
    /**
23
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
24
     */
25
    public function boot()
26
    {
27
        $this->configureRateLimiting();
28
29
        $this->routes(function () {
30 58
            $router = $this->app->make(Router::class);
31
32 58
            $this->mapApiRoutes($router);
33
34 58
            $this->mapWebRoutes($router);
35
        });
36 58
    }
37 58
38
    public function register()
39 58
    {
40
        parent::register();
41
42 18
        $this->app->singleton(ResponseFactoryContract::class, static function (Container $container) {
43 58
            return $container->make(ResponseFactory::class);
44 58
        });
45
    }
46
47
    /**
48
     * Define the "web" routes for the application.
49
     *
50
     * These routes all receive session state, CSRF protection, etc.
51
     *
52
     * @param  \Illuminate\Routing\Router $router
53
     * @return void
54 58
     */
55
    private function mapWebRoutes(Router $router): void
56 58
    {
57 58
        $router->middleware('web')->namespace($this->namespace)->group(base_path('routes/web.php'));
58 58
    }
59 58
60
    /**
61
     * Define the "api" routes for the application.
62
     *
63
     * These routes are typically stateless.
64
     *
65
     * @param  \Illuminate\Routing\Router $router
66
     * @return void
67
     */
68
    private function mapApiRoutes(Router $router): void
69 58
    {
70
        $router->middleware('api')->namespace($this->namespace)->group(base_path('routes/api.php'));
71 58
    }
72 58
73 58
    /**
74 58
     * Configure the rate limiters for the application.
75
     *
76
     * @return void
77
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
78
     */
79
    private function configureRateLimiting()
80
    {
81
        /** @var RateLimiter $rateLimiter */
82
        $rateLimiter = $this->app->make(RateLimiter::class);
83
84
        $rateLimiter->for('spa_login_lock', function (Request $request) {
85
            return new Limit($this->resolveRequestSignature($request), 15, 5);
86
        });
87
88
        $rateLimiter->for('spa_invitation_lock', function (Request $request) {
89
            return new Limit($this->resolveRequestSignature($request), 15, 5);
90
        });
91
92
        $rateLimiter->for('spa_password_reset_lock', function (Request $request) {
93
            return new Limit($this->resolveRequestSignature($request), 15, 5);
94
        });
95
    }
96
97
    private function resolveRequestSignature(Request $request)
98
    {
99
        if ($user = $request->user()) {
100
            return sha1($user->getAuthIdentifier());
101
        } elseif ($route = $request->route()) {
102
            return sha1($route->getDomain() . '|' . $request->ip());
103
        }
104
105
        throw new RuntimeException('Unable to generate the request signature. Route unavailable.');
106
    }
107
}
108