Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

draft: Add RecaptchaServiceInterface #34

Open
wants to merge 4 commits into
base: 3.8.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 25 additions & 46 deletions src/ReCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
use const E_USER_WARNING;

/**
* Render and verify ReCaptchas
* Render and verify v2 ReCaptchas
*
* @final This class should not be extended and will be marked final in version 4.0
*/
class ReCaptcha implements Stringable
class ReCaptcha implements RecaptchaServiceInterface, Stringable
{
/**
* URI to the API
Expand Down Expand Up @@ -185,13 +185,10 @@ public function getIp()
}

/**
* Set a single parameter
*
* @param string $key
* @param mixed $value
* @return self
* @inheritDoc
* @return $this
*/
public function setParam($key, $value)
public function setParam(string $key, mixed $value): self
{
$this->params[$key] = $value;

Expand Down Expand Up @@ -227,11 +224,9 @@ public function setParams($params)
}

/**
* Get the parameter array
*
* @return array<string, mixed>
* @inheritDoc
*/
public function getParams()
public function getParams(): array
earthiverse marked this conversation as resolved.
Show resolved Hide resolved
{
return $this->params;
}
Expand All @@ -252,13 +247,10 @@ public function getParam($key)
}

/**
* Set a single option
*
* @param string $key
* @param mixed $value
* @return self
* @inheritDoc
* @return $this
*/
public function setOption($key, $value)
public function setOption(string $key, mixed $value): self
{
$this->options[$key] = $value;

Expand All @@ -269,7 +261,7 @@ public function setOption($key, $value)
* Set options
*
* @param iterable<string, mixed> $options
* @return self
* @return $this
* @throws Exception
*/
public function setOptions($options)
Expand All @@ -290,11 +282,9 @@ public function setOptions($options)
}

/**
* Get the options array
*
* @return array<string, mixed>
* @inheritDoc
*/
public function getOptions()
public function getOptions(): array
earthiverse marked this conversation as resolved.
Show resolved Hide resolved
{
return $this->options;
}
Expand All @@ -315,45 +305,37 @@ public function getOption($key)
}

/**
* Get the site key
*
* @return string
* @inheritDoc
*/
public function getSiteKey()
public function getSiteKey(): string
earthiverse marked this conversation as resolved.
Show resolved Hide resolved
{
return $this->siteKey;
}

/**
* Set the site key
*
* @param string $siteKey
* @return self
* @inheritDoc
* @return $this
*/
public function setSiteKey($siteKey)
public function setSiteKey(string $siteKey): self
{
$this->siteKey = $siteKey;

return $this;
}

/**
* Get the secret key
*
* @return string
* @inheritDoc
*/
public function getSecretKey()
public function getSecretKey(): string
{
return $this->secretKey;
}

/**
* Set the secret key
*
* @param string $secretKey
* @return self
* @inheritDoc
* @return $this
*/
public function setSecretKey($secretKey)
public function setSecretKey(string $secretKey): self
{
$this->secretKey = $secretKey;

Expand Down Expand Up @@ -478,15 +460,12 @@ protected function post($responseField)
}

/**
* Verify the user input
* {@inheritDoc}
*
* This method calls up the post method and returns a
* \Laminas\ReCaptcha\Response object.
*
* @param string $responseField
* @return Response
*/
public function verify($responseField)
public function verify(string $responseField): Response
earthiverse marked this conversation as resolved.
Show resolved Hide resolved
{
$response = $this->post($responseField);
return new Response(null, null, $response);
Expand Down
60 changes: 60 additions & 0 deletions src/RecaptchaServiceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace Laminas\ReCaptcha;

/**
* An interface for interacting with a recaptcha service provider
*/
interface RecaptchaServiceInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not necessary to add all of the methods present in the concrete class to the interface.

Probably, the only methods required here are verify and possibly getSiteKey as this may be needed for output in the markup/javascript.

Again, using parameter and return types here will force BC breaking changes on the existing concrete class.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the review!

laminas-captcha has calls to all of the functions in the interface, I think because it can create the service by itself if the options are provided.

I'll remove the types as a start :)

{
/**
* Get the options array
*
* @return array<string, mixed>
*/
public function getOptions(): array;

/**
* Set a single option
*/
public function setOption(string $key, mixed $value): self;

/**
* Get the parameter array
*
* @return array<string, mixed>
*/
public function getParams(): array;

/**
* Set a single parameter
*/
public function setParam(string $key, mixed $value): self;

/**
* Get the site key
*/
public function getSiteKey(): string;

/**
* Set the site key
*/
public function setSiteKey(string $siteKey): self;

/**
* Get the secret key
*/
public function getSecretKey(): string;

/**
* Set the secret key
*/
public function setSecretKey(string $secretKey): self;

/**
* Verify the user input
*/
public function verify(string $responseField): Response;
}