|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Arcanedev\NoCaptcha; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Support\Manager; |
|
8
|
|
|
use Arcanedev\NoCaptcha\Contracts\NoCaptcha as NoCaptchaContract; |
|
9
|
|
|
use Arcanedev\NoCaptcha\Contracts\NoCaptchaManager as NoCaptchaManagerContract; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class NoCaptchaManager |
|
13
|
|
|
* |
|
14
|
|
|
* @author ARCANEDEV <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
class NoCaptchaManager extends Manager implements NoCaptchaManagerContract |
|
17
|
|
|
{ |
|
18
|
|
|
/* ----------------------------------------------------------------- |
|
19
|
|
|
| Getters |
|
20
|
|
|
| ----------------------------------------------------------------- |
|
21
|
|
|
*/ |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Get the default driver name. |
|
25
|
|
|
* |
|
26
|
|
|
* @return string |
|
27
|
|
|
*/ |
|
28
|
4 |
|
public function getDefaultDriver(): string |
|
29
|
|
|
{ |
|
30
|
4 |
|
return $this->config('version'); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/* ----------------------------------------------------------------- |
|
34
|
|
|
| Main Methods |
|
35
|
|
|
| ----------------------------------------------------------------- |
|
36
|
|
|
*/ |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Get the NoCaptcha driver by the given version. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string|null $version |
|
42
|
|
|
* |
|
43
|
|
|
* @return \Arcanedev\NoCaptcha\NoCaptchaV3|\Arcanedev\NoCaptcha\NoCaptchaV2 |
|
44
|
|
|
*/ |
|
45
|
32 |
|
public function version($version = null): NoCaptchaContract |
|
46
|
|
|
{ |
|
47
|
32 |
|
return $this->driver($version); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Create the v2 captcha. |
|
52
|
|
|
* |
|
53
|
|
|
* @return \Arcanedev\NoCaptcha\NoCaptchaV2 |
|
54
|
|
|
*/ |
|
55
|
2 |
|
public function createV2Driver(): NoCaptchaContract |
|
56
|
|
|
{ |
|
57
|
2 |
|
return $this->buildDriver(NoCaptchaV2::class); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Create the v3 captcha. |
|
62
|
|
|
* |
|
63
|
|
|
* @return \Arcanedev\NoCaptcha\NoCaptchaV3 |
|
64
|
|
|
*/ |
|
65
|
14 |
|
public function createV3Driver(): NoCaptchaContract |
|
66
|
|
|
{ |
|
67
|
14 |
|
return $this->buildDriver(NoCaptchaV3::class); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/* ----------------------------------------------------------------- |
|
71
|
|
|
| Other Methods |
|
72
|
|
|
| ----------------------------------------------------------------- |
|
73
|
|
|
*/ |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Build a driver. |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $driver |
|
79
|
|
|
* |
|
80
|
|
|
* @return \Arcanedev\NoCaptcha\Contracts\NoCaptcha |
|
81
|
|
|
*/ |
|
82
|
28 |
|
protected function buildDriver(string $driver): NoCaptchaContract |
|
83
|
|
|
{ |
|
84
|
28 |
|
return $this->container->make($driver, [ |
|
85
|
28 |
|
'secret' => $this->config('secret'), |
|
86
|
28 |
|
'siteKey' => $this->config('sitekey'), |
|
87
|
28 |
|
'locale' => $this->config('lang') ?: $this->container->getLocale(), |
|
|
|
|
|
|
88
|
|
|
]); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Get a value from the config file. |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $key |
|
95
|
|
|
* |
|
96
|
|
|
* @return mixed |
|
97
|
|
|
*/ |
|
98
|
28 |
|
protected function config(string $key = '') |
|
99
|
|
|
{ |
|
100
|
28 |
|
return $this->config->get("no-captcha.{$key}"); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: