Skip to content

Commit

Permalink
Apply fixes from StyleCI
Browse files Browse the repository at this point in the history
  • Loading branch information
StyleCIBot committed Jan 9, 2024
1 parent a359ebd commit b45be4e
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 21 deletions.
10 changes: 10 additions & 0 deletions src/Api/Controllers/AnswerChallengeQuestionController.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php

/*
* This file is part of fof/anti-spam.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace FoF\AntiSpam\Api\Controllers;

use Flarum\Foundation\ValidationException;
Expand All @@ -23,6 +32,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
'answer' => 'Invalid answer',
]);
}

return new JsonResponse([
'token' => $token,
]);
Expand Down
9 changes: 9 additions & 0 deletions src/Api/Controllers/ShowChallengeQuestionController.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php

/*
* This file is part of fof/anti-spam.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace FoF\AntiSpam\Api\Controllers;

use Flarum\Api\Controller\AbstractShowController;
Expand Down
1 change: 0 additions & 1 deletion src/Api/Serializers/BasicChallengeQuestionSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace FoF\AntiSpam\Api\Serializers;

use Flarum\Api\Serializer\AbstractSerializer;
use Flarum\Http\RequestUtil;

class BasicChallengeQuestionSerializer extends AbstractSerializer
{
Expand Down
1 change: 0 additions & 1 deletion src/Api/Serializers/ChallengeQuestionSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace FoF\AntiSpam\Api\Serializers;

use Flarum\Api\Serializer\AbstractSerializer;
use Flarum\Http\RequestUtil;

class ChallengeQuestionSerializer extends BasicChallengeQuestionSerializer
Expand Down
6 changes: 3 additions & 3 deletions src/Api/SfsClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class SfsClient

protected $endpoints = [
'closest' => 'https://api.stopforumspam.org/',
'europe' => 'https://europe.stopforumspam.org/',
'us' => 'https://us.stopforumspam.org/'
'europe' => 'https://europe.stopforumspam.org/',
'us' => 'https://us.stopforumspam.org/'
];

/**
Expand All @@ -41,7 +41,7 @@ public function __construct(SettingsRepositoryInterface $settings)

$this->client = new Client([
'base_uri' => $this->endpoint(),
'verify' => false
'verify' => false
]);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Job/ReportSpammerJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public function handle(StopForumSpam $sfs, SfsClient $client, LoggerInterface $l

try {
$client->report([
'ip_addr' => $this->ipAddress,
'ip_addr' => $this->ipAddress,
'username' => $this->username,
'email' => $this->email,
'email' => $this->email,
]);
} catch (\Throwable $e) {
$log->error("[FoF Anti Spam] Failed to report spammer to StopForumSpam: {$e->getMessage()}");
Expand Down
13 changes: 11 additions & 2 deletions src/Listener/ValidateRegistration.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php

/*
* This file is part of fof/anti-spam.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace FoF\AntiSpam\Listener;

use Flarum\User\Event\Saving;
Expand All @@ -10,12 +19,12 @@
class ValidateRegistration
{
protected $challengeRepository;

public function __construct(ChallengeRepository $challengeRepository)
{
$this->challengeRepository = $challengeRepository;
}

public function handle(Saving $event)
{
// We also check for the actor's admin status, so that we can allow admins to create users from the admin panel without a challenge token.
Expand Down
4 changes: 2 additions & 2 deletions src/Model/ChallengeQuestion.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ public static function validateAnswer(?int $challengeId, ?string $answer): ?stri

if ($challenge->case_sensitive) {
if ($challenge->answer === $answer) {
return '1234';//self::generateToken($challengeId);
return '1234'; //self::generateToken($challengeId);
}
} else {
if (strtolower($challenge->answer) === strtolower($answer)) {
return '12345';//self::generateToken($challengeId);
return '12345'; //self::generateToken($challengeId);
}
}

Expand Down
15 changes: 12 additions & 3 deletions src/Model/ChallengeToken.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php

/*
* This file is part of fof/anti-spam.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace FoF\AntiSpam\Model;

use Flarum\Database\AbstractModel;
Expand All @@ -10,13 +19,13 @@ class ChallengeToken extends AbstractModel
{
public static function validateToken(?string $token, User $user)
{
if (!$token) {
if (! $token) {
throw new ValidationException(['fof-challenge-token' => 'No challenge token provided']);
}

$challengeToken = self::query()->where('token', $token)->first();

if (!$challengeToken) {
if (! $challengeToken) {
throw new ValidationException(['fof-challenge-token' => 'Invalid challenge token']);
}

Expand Down
11 changes: 10 additions & 1 deletion src/Repository/ChallengeRepository.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php

/*
* This file is part of fof/anti-spam.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace FoF\AntiSpam\Repository;

use Flarum\Settings\SettingsRepositoryInterface;
Expand All @@ -8,7 +17,7 @@
class ChallengeRepository
{
protected $settings;

public function __construct(SettingsRepositoryInterface $settings)
{
$this->settings = $settings;
Expand Down
17 changes: 11 additions & 6 deletions tests/integration/api/ChallengeQuestions/AnswerChallengeTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php

/*
* This file is part of fof/anti-spam.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace FoF\AntiSpam\Tests\integration\Api;

use Flarum\Testing\integration\TestCase;
Expand Down Expand Up @@ -40,7 +49,7 @@ public function can_answer_challenge_question_incorrectly()
$this->assertEquals(422, $response->getStatusCode());

$json = json_decode($response->getBody()->getContents(), true);

$this->assertArrayHasKey('errors', $json);

$errors = $json['errors'];
Expand Down Expand Up @@ -70,14 +79,10 @@ public function can_answer_challenge_question_correctly()
$this->assertEquals(200, $response->getStatusCode());

$json = json_decode($response->getBody()->getContents(), true);

$this->assertArrayHasKey('token', $json);

$token = $json['token'];
$this->assertNotNull($token, 'Correct answer should have a token');


}


}

0 comments on commit b45be4e

Please sign in to comment.