-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Vitalij Mik
committed
Jan 14, 2025
1 parent
de914c0
commit 4be9003
Showing
52 changed files
with
849 additions
and
430 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,35 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" | ||
bootstrap="./vendor/autoload.php" | ||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd" | ||
bootstrap="./tests/bootstrap/index.php" | ||
cacheResult="false" | ||
colors="true" | ||
executionOrder="random" | ||
resolveDependencies="true" | ||
> | ||
<php> | ||
<ini name="error_reporting" value="-1"/> | ||
<server name="KERNEL_CLASS" value="Shopware\Core\Kernel"/> | ||
<env name="APP_ENV" value="test"/> | ||
<env name="APP_DEBUG" value="1"/> | ||
<env name="SYMFONY_DEPRECATIONS_HELPER" value="weak"/> | ||
</php> | ||
|
||
<testsuites> | ||
<testsuite name="MolliePayments Tests"> | ||
<testsuite name="unit"> | ||
<directory>./tests/PHPUnit</directory> | ||
<directory>./tests/Unit</directory> | ||
</testsuite> | ||
<testsuite name="integration"> | ||
<directory>./tests/Integration</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<coverage> | ||
<coverage processUncoveredFiles="true"> | ||
<include> | ||
<directory>./src</directory> | ||
<directory suffix=".php">./src</directory> | ||
<directory suffix=".php">./shopware</directory> | ||
</include> | ||
</coverage> | ||
|
||
|
||
</phpunit> |
18 changes: 18 additions & 0 deletions
18
polyfill/Shopware/Core/Content/Flow/Dispatching/Aware/ScalarValuesAware.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Shopware\Core\Content\Flow\Dispatching\Aware; | ||
|
||
use Shopware\Core\Framework\Event\IsFlowEventAware; | ||
use Shopware\Core\Framework\Log\Package; | ||
|
||
#[Package('services-settings')] | ||
#[IsFlowEventAware] | ||
interface ScalarValuesAware | ||
{ | ||
public const STORE_VALUES = 'store_values'; | ||
|
||
/** | ||
* @return array<string, scalar|array<mixed>|null> | ||
*/ | ||
public function getValues(): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Mollie\Shopware\Component\Logger; | ||
|
||
use Doctrine\DBAL\Connection; | ||
use Mollie\Shopware\Component\Settings\AbstractSettingsService; | ||
use Monolog\Handler\AbstractHandler; | ||
use Monolog\Handler\RotatingFileHandler; | ||
use Monolog\Level; | ||
use Monolog\LogRecord; | ||
use Psr\Log\LogLevel; | ||
|
||
final class PluginSettingsHandler extends AbstractHandler | ||
{ | ||
private const LOG_CHANNEL = 'mollie'; | ||
private ?AbstractHandler $fileHandler = null; | ||
private Connection $connection; | ||
private string $filePath; | ||
private ?bool $connectedCache = null; | ||
|
||
private AbstractSettingsService $settingsService; | ||
|
||
/** | ||
* @param AbstractSettingsService $settingsService | ||
* @param Connection $connection | ||
* @param string $filePath | ||
* @param int|Level|string $level | ||
* @param bool $bubble | ||
*/ | ||
public function __construct(AbstractSettingsService $settingsService, Connection $connection, string $filePath, bool $bubble = true) | ||
{ | ||
parent::__construct(LogLevel::DEBUG, $bubble); | ||
$this->connection = $connection; | ||
$this->filePath = $filePath; | ||
$this->settingsService = $settingsService; | ||
} | ||
|
||
/** | ||
* We need to define types here because shopware 6.4 uses old monologger where LogRecord does not exists. | ||
* @param array|LogRecord $record | ||
* @return bool | ||
*/ | ||
public function handle($record): bool | ||
{ | ||
if ($this->isConnected() === false) { | ||
return false; | ||
} | ||
|
||
$channel = $record['channel'] ?? null; | ||
if ($channel !== self::LOG_CHANNEL) { | ||
return false; | ||
} | ||
|
||
|
||
if ($this->fileHandler === null) { | ||
$this->fileHandler = $this->initializeHandler(); | ||
} | ||
|
||
$this->fileHandler->handle($record); | ||
|
||
return $this->bubble === false; | ||
} | ||
|
||
private function isConnected(): bool | ||
{ | ||
if ($this->connectedCache !== null) { | ||
return $this->connectedCache; | ||
} | ||
|
||
$this->connectedCache = $this->connection->isConnected(); | ||
return $this->connectedCache; | ||
} | ||
|
||
private function initializeHandler(): AbstractHandler | ||
{ | ||
$loggerSettings = $this->settingsService->getLoggerSettings(); | ||
|
||
$logLevel = $loggerSettings->isDebugMode() ? LogLevel::DEBUG : LogLevel::INFO; | ||
$maxFiles = $loggerSettings->getLogFileDays(); | ||
return new RotatingFileHandler($this->filePath, $maxFiles, $logLevel); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Mollie\Shopware\Component\Logger; | ||
|
||
use Monolog\LogRecord; | ||
use Monolog\Processor\ProcessorInterface; | ||
use Symfony\Component\HttpFoundation\IpUtils; | ||
|
||
final class RecordAnonymizer implements ProcessorInterface | ||
{ | ||
private const URL_SLUG = '/payment/finalize-transaction'; | ||
/** | ||
* @param string $url | ||
* @return string | ||
*/ | ||
private function anonymize(string $url): string | ||
{ | ||
# we do not want to save the used tokens in our log file | ||
# also, those one time tokens are soooooo long. it's just annoying and fills disk space ;) | ||
if (strpos($url, self::URL_SLUG) !== false) { | ||
$parts = explode(self::URL_SLUG, $url); | ||
|
||
$url = str_replace($parts[1], '', $url); | ||
} | ||
|
||
return (string)$url; | ||
} | ||
/** | ||
* We need to define types here because shopware 6.4 uses old monologger where LogRecord does not exists. | ||
* @param array|LogRecord $record | ||
* @return array|LogRecord | ||
*/ | ||
public function __invoke($record) | ||
{ | ||
/** @phpstan-ignore-next-line */ | ||
if (isset($record['extra'])) { | ||
if (array_key_exists('ip', $record['extra'])) { | ||
# replace it with our anonymous IP | ||
$record['extra']['ip'] = IpUtils::anonymize(trim($record['extra']['ip'])); | ||
} | ||
|
||
if (array_key_exists('url', $record['extra'])) { | ||
$record['extra']['url'] = $this->anonymize($record['extra']['url']); | ||
} | ||
} | ||
return $record; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services | ||
https://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<services> | ||
|
||
<service id="Mollie\Shopware\Component\Logger\RecordAnonymizer"> | ||
<tag name="monolog.processor"/> | ||
</service> | ||
|
||
<service id="Monolog\Processor\WebProcessor"> | ||
<tag name="monolog.processor" channel="mollie"/> | ||
</service> | ||
|
||
<service id="Monolog\Processor\IntrospectionProcessor"> | ||
<tag name="monolog.processor" channel="mollie"/> | ||
</service> | ||
|
||
<service id="Mollie\Shopware\Component\Logger\PluginSettingsHandler"> | ||
<argument type="service" id="Mollie\Shopware\Component\Settings\SettingsService"/> | ||
<argument type="service" id="Doctrine\DBAL\Connection"/> | ||
<argument>%kernel.logs_dir%/mollie_%kernel.environment%.log</argument> | ||
</service> | ||
</services> | ||
</container> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Mollie\Shopware\Component\Settings; | ||
|
||
use Mollie\Shopware\Component\Settings\Struct\LoggerSettings; | ||
|
||
abstract class AbstractSettingsService | ||
{ | ||
abstract public function getDecorated(): AbstractSettingsService; | ||
abstract public function getLoggerSettings(?string $salesChannelId = null): LoggerSettings; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services | ||
https://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<services> | ||
|
||
<service id="Mollie\Shopware\Component\Settings\SettingsService"> | ||
<argument type="service" id="Shopware\Core\System\SystemConfig\SystemConfigService"/> | ||
</service> | ||
|
||
|
||
</services> | ||
</container> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Mollie\Shopware\Component\Settings; | ||
|
||
use Mollie\Shopware\Component\Settings\Struct\LoggerSettings; | ||
use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException; | ||
use Shopware\Core\System\SystemConfig\SystemConfigService; | ||
|
||
final class SettingsService extends AbstractSettingsService | ||
{ | ||
public const SYSTEM_CONFIG_DOMAIN = 'MolliePayments.config'; | ||
private const CACHE_KEY_LOGGER = 'logger'; | ||
private const CACHE_KEY_SHOPWARE = 'shopware'; | ||
|
||
private SystemConfigService $systemConfigService; | ||
|
||
private array $settingsCache = []; | ||
|
||
|
||
|
||
public function __construct(SystemConfigService $systemConfigService) | ||
{ | ||
$this->systemConfigService = $systemConfigService; | ||
} | ||
|
||
public function getDecorated(): AbstractSettingsService | ||
{ | ||
throw new DecorationPatternException(self::class); | ||
} | ||
|
||
public function getLoggerSettings(?string $salesChannelId = null): LoggerSettings | ||
{ | ||
$cacheKey = self::CACHE_KEY_LOGGER . '_' . ($salesChannelId ?? 'all'); | ||
|
||
if (isset($this->settingsCache[$cacheKey])) { | ||
return $this->settingsCache[$cacheKey]; | ||
} | ||
|
||
|
||
$shopwareSettings = $this->getShopwareSettings($salesChannelId); | ||
$loggerSettings = LoggerSettings::createFromShopwareArray($shopwareSettings); | ||
$this->settingsCache[$cacheKey] = $loggerSettings; | ||
|
||
return $loggerSettings; | ||
} | ||
|
||
private function getShopwareSettings(?string $salesChannelId = null): array | ||
{ | ||
$cacheKey = self::CACHE_KEY_SHOPWARE . '_' . ($salesChannelId ?? 'all'); | ||
|
||
if (isset($this->settingsCache[$cacheKey])) { | ||
return $this->settingsCache[$cacheKey]; | ||
} | ||
|
||
$shopwareSettingsArray = $this->systemConfigService->get(self::SYSTEM_CONFIG_DOMAIN, $salesChannelId); | ||
$this->settingsCache[$cacheKey] = $shopwareSettingsArray; | ||
return $shopwareSettingsArray; | ||
} | ||
} |
Oops, something went wrong.