Passed
Push — master ( 586ee5...6c113d )
by Roberto
03:06
created

ReCaptchaServiceProvider   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
wmc 9
eloc 31
dl 0
loc 97
ccs 30
cts 36
cp 0.8333
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A provides() 0 3 1
A boot() 0 7 1
A register() 0 7 1
A addValidationRule() 0 6 1
A registerRoutes() 0 8 1
A registerReCaptchaBuilder() 0 19 4
1
<?php
2
/**
3
 * Copyright (c) 2017 - present
4
 * LaravelGoogleRecaptcha - ReCaptchaServiceProvider.php
5
 * author: Roberto Belotti - [email protected]
6
 * web : robertobelotti.com, github.com/biscolab
7
 * Initial version created on: 12/9/2018
8
 * MIT license: https://github.com/biscolab/laravel-recaptcha/blob/master/LICENSE
9
 */
10
11
namespace Biscolab\ReCaptcha;
12
13
use Illuminate\Support\Facades\Route;
14
use Illuminate\Support\ServiceProvider;
15
use Validator;
16
17
/**
18
 * Class ReCaptchaServiceProvider
19
 * @package Biscolab\ReCaptcha
20
 */
21
class ReCaptchaServiceProvider extends ServiceProvider {
22
23
	/**
24
	 * Indicates if loading of the provider is deferred.
25
	 *
26
	 * @var bool
27
	 */
28
	protected $defer = false;
29
30
	/**
31
	 *
32
	 */
33 14
	public function boot() {
34
35 14
		$this->addValidationRule();
36
//		$this->loadRoutesFrom(__DIR__ . '/routes/routes.php');
37 14
		$this->registerRoutes();
38 14
		$this->publishes([
39 14
			__DIR__ . '/../config/recaptcha.php' => config_path('recaptcha.php'),
40
		]);
41
42 14
	}
43
44
	/**
45
	 * Extends Validator to include a recaptcha type
46
	 */
47 14
	public function addValidationRule() {
48
49
		Validator::extendImplicit('recaptcha', function ($attribute, $value, $parameters, $validator) {
0 ignored issues
show
Unused Code introduced by
The parameter $validator is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

49
		Validator::extendImplicit('recaptcha', function ($attribute, $value, $parameters, /** @scrutinizer ignore-unused */ $validator) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $parameters is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

49
		Validator::extendImplicit('recaptcha', function ($attribute, $value, /** @scrutinizer ignore-unused */ $parameters, $validator) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
50
51
			return app('recaptcha')->validate($value);
52 14
		}, trans('validation.recaptcha'));
53 14
	}
54
55
	/**
56
	 * Register the service provider.
57
	 *
58
	 * @return void
59
	 */
60 14
	public function register() {
61
62 14
		$this->mergeConfigFrom(
63 14
			__DIR__ . '/../config/recaptcha.php', 'recaptcha'
64
		);
65
66 14
		$this->registerReCaptchaBuilder();
67 14
	}
68
69
	/**
70
	 * Get the services provided by the provider.
71
	 *
72
	 * @return array
73
	 */
74
	public function provides(): array {
75
76
		return ['recaptcha'];
77
	}
78
79
	/**
80
	 * @return ReCaptchaServiceProvider
81
	 *
82
	 * @since v3.4.1
83
	 */
84 14
	protected function registerRoutes(): ReCaptchaServiceProvider {
85
86 14
		Route::get(
87 14
			config('recaptcha.default_validation_route', 'biscolab-recaptcha/validate'),
88 14
			['uses' => 'Biscolab\ReCaptcha\Controllers\ReCaptchaController@validateV3']
89 14
		)->middleware('web');
90
91 14
		return $this;
92
	}
93
94
	/**
95
	 * Register the HTML builder instance.
96
	 *
97
	 * @return void
98
	 */
99 14
	protected function registerReCaptchaBuilder() {
100
101
		$this->app->singleton('recaptcha', function ($app) {
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

101
		$this->app->singleton('recaptcha', function (/** @scrutinizer ignore-unused */ $app) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
102
103 2
			$recaptcha_class = '';
104
105 2
			switch (config('recaptcha.version')) {
106 2
				case 'v3' :
107 1
					$recaptcha_class = ReCaptchaBuilderV3::class;
108 1
					break;
109 1
				case 'v2' :
110 1
					$recaptcha_class = ReCaptchaBuilderV2::class;
111 1
					break;
112
				case 'invisible':
113
					$recaptcha_class = ReCaptchaBuilderInvisible::class;
114
					break;
115
			}
116
117 2
			return new $recaptcha_class(config('recaptcha.api_site_key'), config('recaptcha.api_secret_key'));
118
119 14
		});
120 14
	}
121
122
}
123