1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Netgen\InformationCollection\Core\Service; |
6
|
|
|
|
7
|
|
|
use eZ\Publish\API\Repository\ContentTypeService; |
8
|
|
|
use eZ\Publish\API\Repository\Values\Content\Location; |
9
|
|
|
use eZ\Publish\API\Repository\Values\ContentType\ContentType; |
10
|
|
|
use eZ\Publish\Core\MVC\ConfigResolverInterface; |
11
|
|
|
use Netgen\InformationCollection\API\Service\CaptchaService as CaptchaServiceInterface; |
12
|
|
|
use Netgen\InformationCollection\API\Service\CaptchaValue; |
13
|
|
|
use Netgen\InformationCollection\API\Value\Captcha\ReCaptcha; |
14
|
|
|
use Netgen\InformationCollection\API\Value\Captcha\NullObject; |
15
|
|
|
|
16
|
|
|
class CaptchaService implements CaptchaServiceInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $config; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var \eZ\Publish\API\Repository\ContentTypeService |
25
|
|
|
*/ |
26
|
|
|
protected $contentTypeService; |
27
|
|
|
/** |
28
|
|
|
* @var ConfigResolverInterface |
29
|
|
|
*/ |
30
|
|
|
private $configResolver; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* CaptchaService constructor. |
34
|
|
|
* |
35
|
|
|
* @param \eZ\Publish\API\Repository\ContentTypeService $contentTypeService |
36
|
|
|
* @param array $config |
|
|
|
|
37
|
|
|
*/ |
38
|
|
|
public function __construct(ContentTypeService $contentTypeService, ConfigResolverInterface $configResolver) |
39
|
|
|
{ |
40
|
|
|
$this->config = $configResolver->getParameter('captcha', 'netgen_information_collection'); |
|
|
|
|
41
|
|
|
$this->contentTypeService = $contentTypeService; |
42
|
|
|
$this->configResolver = $configResolver; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
public function isEnabled(Location $location): bool |
49
|
|
|
{ |
50
|
|
|
$config = $this->getConfig($location); |
51
|
|
|
|
52
|
|
|
return $config['enabled']; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
public function getSiteKey(Location $location): string |
59
|
|
|
{ |
60
|
|
|
$config = $this->getConfig($location); |
61
|
|
|
|
62
|
|
|
return $config['site_key']; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
public function getCaptcha(Location $location): CaptchaValue |
69
|
|
|
{ |
70
|
|
|
$config = $this->getConfig($location); |
71
|
|
|
|
72
|
|
|
if ($config['enabled']) { |
73
|
|
|
$reCaptcha = new \ReCaptcha\ReCaptcha($config['secret']); |
74
|
|
|
|
75
|
|
|
if (!empty($config['options'])) { |
76
|
|
|
// if (!empty($config['options']['hostname'])) { |
77
|
|
|
$reCaptcha->setExpectedHostname('localhost'); |
78
|
|
|
// } |
79
|
|
|
// if (!empty($config['options']['apk_package_name'])) { |
80
|
|
|
// $reCaptcha->setExpectedApkPackageName($config['options']['apk_package_name']); |
81
|
|
|
// } |
82
|
|
|
if (!empty($config['options']['action'])) { |
83
|
|
|
$reCaptcha->setExpectedAction($config['options']['action']); |
84
|
|
|
} |
85
|
|
|
// if (!empty($config['options']['score_threshold'])) { |
86
|
|
|
// $reCaptcha->setScoreThreshold($config['options']['score_threshold']); |
87
|
|
|
// } |
88
|
|
|
// if (!empty($config['options']['challenge_timeout'])) { |
89
|
|
|
// $reCaptcha->setChallengeTimeout($config['options']['challenge_timeout']); |
90
|
|
|
// } |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
dump($reCaptcha); |
94
|
|
|
return new ReCaptcha($reCaptcha); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return new NullObject(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Returns filtered config for current Location. |
102
|
|
|
* |
103
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Location $location |
104
|
|
|
* |
105
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
106
|
|
|
* |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
|
|
public function getConfig(Location $location): array |
110
|
|
|
{ |
111
|
|
|
$contentTypeConfig = $this->getConfigForContentType( |
112
|
|
|
$this->getContentType($location) |
113
|
|
|
); |
114
|
|
|
|
115
|
|
|
return (array)array_replace($this->config, $contentTypeConfig); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Returns filtered config for current ContentType. |
120
|
|
|
* |
121
|
|
|
* @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType |
122
|
|
|
* |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
|
|
protected function getConfigForContentType(ContentType $contentType) |
126
|
|
|
{ |
127
|
|
|
if ($this->hasConfigForContentType($contentType)) { |
128
|
|
|
return $this->config['override_by_type'][$contentType->identifier]; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return []; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Checks if override exist for given ContentType. |
136
|
|
|
* |
137
|
|
|
* @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType |
138
|
|
|
* |
139
|
|
|
* @return bool |
140
|
|
|
*/ |
141
|
|
|
protected function hasConfigForContentType(ContentType $contentType) |
142
|
|
|
{ |
143
|
|
|
if (!empty($this->config['override_by_type'])) { |
144
|
|
|
if (in_array($contentType->identifier, array_keys($this->config['override_by_type']), true)) { |
145
|
|
|
return true; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return false; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Helper method for retrieving ContentType from Location. |
154
|
|
|
* |
155
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Location $location |
156
|
|
|
* |
157
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
158
|
|
|
* |
159
|
|
|
* @return \eZ\Publish\API\Repository\Values\ContentType\ContentType |
160
|
|
|
*/ |
161
|
|
|
protected function getContentType(Location $location) |
162
|
|
|
{ |
163
|
|
|
return $this->contentTypeService |
164
|
|
|
->loadContentType($location->contentInfo->contentTypeId); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.