Completed
Push — master ( 7b9b82...f4954a )
by Roberto
07:53 queued 11s
created

registerReCaptchaBuilder()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 4
eloc 13
c 2
b 0
f 1
nc 1
nop 0
dl 0
loc 20
rs 9.8333
ccs 13
cts 13
cp 1
crap 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
	/**
25
	 * Indicates if loading of the provider is deferred.
26
	 *
27
	 * @var bool
28
	 */
29
	protected $defer = false;
30
31
	/**
32
	 *
33
	 */
34 31
	public function boot()
35
	{
36
37 31
		$this->addValidationRule();
38
//		$this->loadRoutesFrom(__DIR__ . '/routes/routes.php');
39 31
		$this->registerRoutes();
40 31
		$this->publishes([
41 31
			__DIR__ . '/../config/recaptcha.php' => config_path('recaptcha.php'),
42
		]);
43
44 31
	}
45
46
	/**
47
	 * Extends Validator to include a recaptcha type
48
	 */
49 31
	public function addValidationRule()
50
	{
51
52
		Validator::extendImplicit('recaptcha', function ($attribute, $value, $parameters, $validator) {
0 ignored issues
show
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

52
		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...
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

52
		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...
53
54
			return app('recaptcha')->validate($value);
0 ignored issues
show
Bug introduced by
The method validate() does not exist on Illuminate\Contracts\Foundation\Application. ( Ignorable by Annotation )

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

54
			return app('recaptcha')->/** @scrutinizer ignore-call */ validate($value);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
55 31
		}, trans('validation.recaptcha'));
0 ignored issues
show
Bug introduced by
It seems like trans('validation.recaptcha') can also be of type array; however, parameter $message of Illuminate\Support\Facad...dator::extendImplicit() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

55
		}, /** @scrutinizer ignore-type */ trans('validation.recaptcha'));
Loading history...
56 31
	}
57
58
	/**
59
	 * Register the service provider.
60
	 *
61
	 * @return void
62
	 */
63 31
	public function register()
64
	{
65
66 31
		$this->mergeConfigFrom(
67 31
			__DIR__ . '/../config/recaptcha.php', 'recaptcha'
68
		);
69
70 31
		$this->registerReCaptchaBuilder();
71 31
	}
72
73
	/**
74
	 * Get the services provided by the provider.
75
	 *
76
	 * @return array
77
	 */
78
	public function provides(): array
79
	{
80
81
		return ['recaptcha'];
82
	}
83
84
	/**
85
	 * @return ReCaptchaServiceProvider
86
	 *
87
	 * @since v3.4.1
88
	 */
89 31
	protected function registerRoutes(): ReCaptchaServiceProvider
90
	{
91
92 31
		Route::get(
93 31
			config('recaptcha.default_validation_route', 'biscolab-recaptcha/validate'),
94 31
			['uses' => 'Biscolab\ReCaptcha\Controllers\ReCaptchaController@validateV3']
95 31
		)->middleware('web');
96
97 31
		return $this;
98
	}
99
100
	/**
101
	 * Register the HTML builder instance.
102
	 *
103
	 * @return void
104
	 */
105 31
	protected function registerReCaptchaBuilder()
106
	{
107
108
		$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

108
		$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...
109
110 11
			$recaptcha_class = '';
111
112 11
			switch (config('recaptcha.version')) {
113 11
				case 'v3' :
114 1
					$recaptcha_class = ReCaptchaBuilderV3::class;
115 1
					break;
116 10
				case 'v2' :
117 6
					$recaptcha_class = ReCaptchaBuilderV2::class;
118 6
					break;
119 4
				case 'invisible':
120 4
					$recaptcha_class = ReCaptchaBuilderInvisible::class;
121 4
					break;
122
			}
123
124 11
			return new $recaptcha_class(config('recaptcha.api_site_key'), config('recaptcha.api_secret_key'));
125
126 31
		});
127 31
	}
128
129
}
130