Skip to content

Commit

Permalink
task: add easy-coding-standard
Browse files Browse the repository at this point in the history
  • Loading branch information
cngJo committed Jun 29, 2023
1 parent ff70a22 commit 4ebd0b8
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 15 deletions.
8 changes: 8 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,11 @@ phpstan:
script:
- composer install --no-scripts
- php vendor/bin/phpstan --dry-run

easy-coding-standard:
stage: static-analysis
image: $CI_REGISTRY_IMAGE/pipeline-runner:$CI_COMMIT_REF_SLUG
script:
- composer install --no-scripts
- php vendor/bin/ecs

3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"require-dev": {
"phpstan/phpstan": "^1.10",
"rector/rector": "^0.17.2",
"phpunit/phpunit": "~9.6"
"phpunit/phpunit": "~9.6",
"symplify/easy-coding-standard": "^11.5"
},
"config": {
"allow-plugins": {
Expand Down
19 changes: 19 additions & 0 deletions ecs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

use Symplify\EasyCodingStandard\Config\ECSConfig;
use Symplify\EasyCodingStandard\ValueObject\Set\SetList;

return static function (ECSConfig $ecsConfig): void {

$ecsConfig->paths([
__DIR__ . '/src',
__DIR__ . '/tests',
]);

$ecsConfig->sets([
SetList::PSR_12,
SetList::STRICT,
SetList::CLEAN_CODE,
SetList::COMMON,
]);
};
14 changes: 8 additions & 6 deletions src/Controller/Mamo/MetricsController.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace MobilisticsGmbH\MamoConnector\Controller\Mamo;

use MobilisticsGmbH\MamoConnector\MobiMamoConnector;
Expand All @@ -26,17 +28,17 @@ public function __construct(
], methods: ['GET'])]
public function indexAction(Request $request): Response
{
$secret = $this->systemConfigService->get(MobiMamoConnector::PLUGIN_IDENTIFIER . ".config.secret");
if (!is_string($secret)) {
$secret = $this->systemConfigService->get(MobiMamoConnector::PLUGIN_IDENTIFIER . '.config.secret');
if (! is_string($secret)) {
// Can only happen, when we change our config template or Shopware itself screws up.
$this->logger->error("Configuration Secret is not a string.", [
"receivedType" => gettype($secret),
$this->logger->error('Configuration Secret is not a string.', [
'receivedType' => gettype($secret),
]);
throw new HttpException(500);
}

if (!$this->requestAuthorizationService->isAuthorized($request, $secret)) {
$this->logger->info("Request is not authorized to access the metrics endpoint.");
if (! $this->requestAuthorizationService->isAuthorized($request, $secret)) {
$this->logger->info('Request is not authorized to access the metrics endpoint.');
throw new HttpException(403);
}

Expand Down
6 changes: 4 additions & 2 deletions src/MobiMamoConnector.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace MobilisticsGmbH\MamoConnector;

use Shopware\Core\Framework\Plugin;

class MobiMamoConnector extends Plugin
{
public const PLUGIN_IDENTIFIER = "MobiMamoConnector";
public const PLUGIN_IDENTIFIER = 'MobiMamoConnector';
}
8 changes: 5 additions & 3 deletions src/Service/RequestAuthorizationService.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace MobilisticsGmbH\MamoConnector\Service;

use Symfony\Component\HttpFoundation\Request;
Expand All @@ -8,10 +10,10 @@ class RequestAuthorizationService
{
public function isAuthorized(Request $request, string $secret): bool
{
$requestSecret = $request->query->get("secret");
if (!is_string($requestSecret)) {
$requestSecret = $request->query->get('secret');
if (! is_string($requestSecret)) {
return false;
}
return $requestSecret === $secret;
}
}
}
8 changes: 5 additions & 3 deletions tests/RequestAuthorizationServiceTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace MobilisticsGmbH\MamoConnector\Tests;

use MobilisticsGmbH\MamoConnector\Service\RequestAuthorizationService;
Expand All @@ -8,12 +10,12 @@

class RequestAuthorizationServiceTest extends TestCase
{
private const TESTING_SECRET = "dummy-testing-secret";
private const TESTING_SECRET = 'dummy-testing-secret';

public function testAuthorizedRequestWhenSecretMatches(): void
{
$request = new Request([
"secret" => self::TESTING_SECRET,
'secret' => self::TESTING_SECRET,
]);

$service = new RequestAuthorizationService();
Expand All @@ -24,7 +26,7 @@ public function testAuthorizedRequestWhenSecretMatches(): void
public function testRequestIsUnauthorizedWhenSecretDoesNotMatch(): void
{
$request = new Request([
"secret" => self::TESTING_SECRET. "-differs",
'secret' => self::TESTING_SECRET . '-differs',
]);

$service = new RequestAuthorizationService();
Expand Down

0 comments on commit 4ebd0b8

Please sign in to comment.