-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathCaptchaService.php
More file actions
129 lines (106 loc) · 4.17 KB
/
Copy pathCaptchaService.php
File metadata and controls
129 lines (106 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
declare(strict_types=1);
namespace Netgen\InformationCollection\Core\Service;
use Ibexa\Contracts\Core\Repository\ContentTypeService;
use Ibexa\Contracts\Core\Repository\Values\Content\Location;
use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType;
use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface;
use Netgen\InformationCollection\API\Service\CaptchaService as CaptchaServiceInterface;
use Netgen\InformationCollection\API\Service\CaptchaValue;
use Netgen\InformationCollection\API\Value\Captcha\NullObject;
use Netgen\InformationCollection\API\Value\Captcha\ReCaptcha;
use function array_keys;
use function array_replace;
use function in_array;
class CaptchaService implements CaptchaServiceInterface
{
protected array $config;
protected ContentTypeService $contentTypeService;
private ConfigResolverInterface $configResolver;
public function __construct(ContentTypeService $contentTypeService, ConfigResolverInterface $configResolver)
{
$this->config = $configResolver->getParameter('captcha', 'netgen_information_collection');
$this->contentTypeService = $contentTypeService;
$this->configResolver = $configResolver;
}
public function isEnabled(Location $location): bool
{
$config = $this->getConfig($location);
return $config['enabled'];
}
public function getSiteKey(Location $location): string
{
$config = $this->getConfig($location);
return $config['site_key'];
}
public function getCaptcha(Location $location): CaptchaValue
{
$config = $this->getConfig($location);
if ($config['enabled']) {
$reCaptcha = new \ReCaptcha\ReCaptcha($config['secret']);
if (!empty($config['options'])) {
// if (!empty($config['options']['hostname'])) {
// $reCaptcha->setExpectedHostname('localhost');
// }
// if (!empty($config['options']['apk_package_name'])) {
// $reCaptcha->setExpectedApkPackageName($config['options']['apk_package_name']);
// }
if (!empty($config['options']['action'])) {
$reCaptcha->setExpectedAction($config['options']['action']);
}
// if (!empty($config['options']['score_threshold'])) {
// $reCaptcha->setScoreThreshold($config['options']['score_threshold']);
// }
// if (!empty($config['options']['challenge_timeout'])) {
// $reCaptcha->setChallengeTimeout($config['options']['challenge_timeout']);
// }
}
return new ReCaptcha($reCaptcha);
}
return new NullObject();
}
/**
* Returns filtered config for current Location.
*
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
*/
public function getConfig(Location $location): array
{
$contentTypeConfig = $this->getConfigForContentType(
$this->getContentType($location)
);
return array_replace($this->config, $contentTypeConfig);
}
/**
* Returns filtered config for current ContentType.
*/
protected function getConfigForContentType(ContentType $contentType): array
{
if ($this->hasConfigForContentType($contentType)) {
return $this->config['override_by_type'][$contentType->identifier];
}
return [];
}
/**
* Checks if override exist for given ContentType.
*/
protected function hasConfigForContentType(ContentType $contentType): bool
{
if (!empty($this->config['override_by_type'])) {
if (in_array($contentType->identifier, array_keys($this->config['override_by_type']), true)) {
return true;
}
}
return false;
}
/**
* Helper method for retrieving ContentType from Location.
*
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
*/
protected function getContentType(Location $location): ContentType
{
return $this->contentTypeService
->loadContentType($location->contentInfo->contentTypeId);
}
}