GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#73)
by
unknown
14:32
created

CaptchaService::getCaptcha()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 9.424
c 0
b 0
f 0
cc 4
nc 4
nop 1
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
0 ignored issues
show
Documentation introduced by
There is no parameter named $config. Did you maybe mean $configResolver?

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 method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
37
     */
38
    public function __construct(ContentTypeService $contentTypeService, ConfigResolverInterface $configResolver)
39
    {
40
        $this->config = $configResolver->getParameter('captcha', 'netgen_information_collection');
0 ignored issues
show
Documentation Bug introduced by
It seems like $configResolver->getPara...nformation_collection') of type * is incompatible with the declared type array of property $config.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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