Skip to content

Commit

Permalink
Merge branch 'b-7.0.x-improve_exceptions_docs-OXDEV-7647' into b-7.0.…
Browse files Browse the repository at this point in the history
…x-prototype-OXDEV-7325

# Conflicts:
#	tests/Unit/Infrastructure/ThemeSettingRepositorySettersTest.php
  • Loading branch information
MarcelOxid committed Dec 7, 2023
2 parents eff9882 + 1f02d60 commit 16c6b6e
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 62 deletions.
13 changes: 7 additions & 6 deletions src/Setting/Infrastructure/ShopSettingRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Doctrine\DBAL\Result;
use OxidEsales\EshopCommunity\Internal\Framework\Config\Dao\ShopConfigurationSettingDaoInterface;
use OxidEsales\EshopCommunity\Internal\Framework\Config\DataObject\ShopConfigurationSetting;
use OxidEsales\EshopCommunity\Internal\Framework\Config\Utility\ShopSettingEncoderInterface;
use OxidEsales\EshopCommunity\Internal\Framework\Database\QueryBuilderFactoryInterface;
use OxidEsales\EshopCommunity\Internal\Transition\Utility\BasicContextInterface;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\Enum\FieldType;
Expand All @@ -25,8 +24,7 @@ final class ShopSettingRepository implements ShopSettingRepositoryInterface
public function __construct(
private BasicContextInterface $basicContext,
private QueryBuilderFactoryInterface $queryBuilderFactory,
protected ShopSettingEncoderInterface $shopSettingEncoder,
protected ShopConfigurationSettingDaoInterface $configurationSettingDao,
private ShopConfigurationSettingDaoInterface $configurationSettingDao,
) {
}

Expand Down Expand Up @@ -113,7 +111,7 @@ public function getAssocCollection(string $name): array
return $this->getArrayFromSettingValue($setting);
}

protected function getShopSetting(string $name): ShopConfigurationSetting
private function getShopSetting(string $name): ShopConfigurationSetting
{
return $this->configurationSettingDao->get($name, $this->basicContext->getCurrentShopId());
}
Expand Down Expand Up @@ -149,7 +147,7 @@ public function getSettingsList(): array
/**
* @throws WrongSettingTypeException
*/
public function checkSettingType(ShopConfigurationSetting $value, string $requiredType): void
private function checkSettingType(ShopConfigurationSetting $value, string $requiredType): void
{
if ($value->getType() !== $requiredType) {
throw new WrongSettingTypeException();
Expand All @@ -159,7 +157,7 @@ public function checkSettingType(ShopConfigurationSetting $value, string $requir
/**
* @throws WrongSettingValueException
*/
public function getArrayFromSettingValue(ShopConfigurationSetting $setting): array
private function getArrayFromSettingValue(ShopConfigurationSetting $setting): array
{
$value = $setting->getValue();

Expand Down Expand Up @@ -218,6 +216,9 @@ private function saveAsType(string $type, string $name, mixed $value): void
$this->configurationSettingDao->save($setting);
}

/**
* @throws WrongSettingTypeException
*/
private function validateOriginalSettingType(string $originalSettingName, string $type): void
{
$originalSetting = $this->getShopSetting($originalSettingName);
Expand Down
54 changes: 54 additions & 0 deletions src/Setting/Infrastructure/ShopSettingRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,92 @@

namespace OxidEsales\GraphQL\ConfigurationAccess\Setting\Infrastructure;

use OxidEsales\GraphQL\ConfigurationAccess\Setting\Exception\NoSettingsFoundForShopException;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\Exception\WrongSettingTypeException;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\Exception\WrongSettingValueException;

interface ShopSettingRepositoryInterface
{
/**
* @throws WrongSettingTypeException
* @throws WrongSettingValueException
*/
public function getInteger(string $name): int;

/**
* @throws WrongSettingTypeException
* @throws WrongSettingValueException
*/
public function getFloat(string $name): float;

/**
* @throws WrongSettingTypeException
* @throws WrongSettingValueException
*/
public function getBoolean(string $name): bool;

/**
* @throws WrongSettingTypeException
* @throws WrongSettingValueException
*/
public function getString(string $name): string;

/**
* @throws WrongSettingTypeException
* @throws WrongSettingValueException
*/
public function getSelect(string $name): string;

/**
* @throws WrongSettingTypeException
* @throws WrongSettingValueException
*/
public function getCollection(string $name): array;

/**
* @throws WrongSettingTypeException
* @throws WrongSettingValueException
*/
public function getAssocCollection(string $name): array;

/**
* @throws NoSettingsFoundForShopException
* @return array<string, string>
*/
public function getSettingsList(): array;

/**
* @throws WrongSettingTypeException
*/
public function saveIntegerSetting(string $name, int $value): void;

/**
* @throws WrongSettingTypeException
*/
public function saveFloatSetting(string $name, float $value): void;

/**
* @throws WrongSettingTypeException
*/
public function saveBooleanSetting(string $name, bool $value): void;

/**
* @throws WrongSettingTypeException
*/
public function saveStringSetting(string $name, string $value): void;

/**
* @throws WrongSettingTypeException
*/
public function saveSelectSetting(string $name, string $value): void;

/**
* @throws WrongSettingTypeException
*/
public function saveCollectionSetting(string $name, array $value): void;

/**
* @throws WrongSettingTypeException
*/
public function saveAssocCollectionSetting(string $name, array $value): void;
}
54 changes: 19 additions & 35 deletions src/Setting/Infrastructure/ThemeSettingRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,67 +147,42 @@ public function getSettingsList(string $themeId): array

public function saveIntegerSetting(string $name, int $value, string $themeId): void
{
$this->getInteger($name, $themeId);

$value = $this->shopSettingEncoder->encode(FieldType::NUMBER, $value);

$this->saveSettingValue($name, $themeId, (string)$value);
$this->saveSettingAsType(FieldType::NUMBER, $name, $themeId, $value);
}

public function saveFloatSetting(string $name, float $value, string $themeId): void
{
$this->getFloat($name, $themeId);

$value = $this->shopSettingEncoder->encode(FieldType::NUMBER, $value);

$this->saveSettingValue($name, $themeId, (string)$value);
$this->saveSettingAsType(FieldType::NUMBER, $name, $themeId, $value);
}

public function saveBooleanSetting(string $name, bool $value, string $themeId): void
{
$this->getBoolean($name, $themeId);

$value = $this->shopSettingEncoder->encode(FieldType::BOOLEAN, $value);

$this->saveSettingValue($name, $themeId, (string)$value);
$this->saveSettingAsType(FieldType::BOOLEAN, $name, $themeId, $value);
}

public function saveStringSetting(string $name, string $value, string $themeId): void
{
$this->getString($name, $themeId);

$value = $this->shopSettingEncoder->encode(FieldType::STRING, $value);

$this->saveSettingValue($name, $themeId, (string)$value);
$this->saveSettingAsType(FieldType::STRING, $name, $themeId, $value);
}

public function saveSelectSetting(string $name, string $value, string $themeId): void
{
$this->getSelect($name, $themeId);

$value = $this->shopSettingEncoder->encode(FieldType::SELECT, $value);

$this->saveSettingValue($name, $themeId, (string)$value);
$this->saveSettingAsType(FieldType::SELECT, $name, $themeId, $value);
}

public function saveCollectionSetting(string $name, array $value, string $themeId): void
{
$this->getCollection($name, $themeId);

$value = $this->shopSettingEncoder->encode(FieldType::ARRAY, $value);

$this->saveSettingValue($name, $themeId, (string)$value);
$this->saveSettingAsType(FieldType::ARRAY, $name, $themeId, $value);
}

public function saveAssocCollectionSetting(string $name, array $value, string $themeId): void
{
$this->getAssocCollection($name, $themeId);

$value = $this->shopSettingEncoder->encode(FieldType::ASSOCIATIVE_ARRAY, $value);

$this->saveSettingValue($name, $themeId, (string)$value);
$this->saveSettingAsType(FieldType::ASSOCIATIVE_ARRAY, $name, $themeId, $value);
}

/**
* @throws NoSettingsFoundForThemeException
*/
protected function getSettingValue(string $name, string $fieldType, string $theme): mixed
{
$queryBuilder = $this->queryBuilderFactory->create();
Expand Down Expand Up @@ -263,4 +238,13 @@ protected function saveSettingValue(string $name, string $themeId, string $value
)
);
}

protected function saveSettingAsType(string $settingType, string $name, string $themeId, mixed $value): void
{
$this->getSettingValue($name, $settingType, $themeId);

$value = $this->shopSettingEncoder->encode($settingType, $value);

$this->saveSettingValue($name, $themeId, (string)$value);
}
}
53 changes: 53 additions & 0 deletions src/Setting/Infrastructure/ThemeSettingRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,91 @@

namespace OxidEsales\GraphQL\ConfigurationAccess\Setting\Infrastructure;

use OxidEsales\GraphQL\ConfigurationAccess\Setting\Exception\NoSettingsFoundForThemeException;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\Exception\WrongSettingValueException;

interface ThemeSettingRepositoryInterface
{
/**
* @throws NoSettingsFoundForThemeException
* @throws WrongSettingValueException
*/
public function getInteger(string $name, string $themeId): int;

/**
* @throws NoSettingsFoundForThemeException
* @throws WrongSettingValueException
*/
public function getFloat(string $name, string $themeId): float;

/**
* @throws NoSettingsFoundForThemeException
* @throws WrongSettingValueException
*/
public function getBoolean(string $name, string $themeId): bool;

/**
* @throws NoSettingsFoundForThemeException
* @throws WrongSettingValueException
*/
public function getString(string $name, string $themeId): string;

/**
* @throws NoSettingsFoundForThemeException
* @throws WrongSettingValueException
*/
public function getSelect(string $name, string $themeId): string;

/**
* @throws NoSettingsFoundForThemeException
* @throws WrongSettingValueException
*/
public function getCollection(string $name, string $themeId): array;

/**
* @throws NoSettingsFoundForThemeException
* @throws WrongSettingValueException
*/
public function getAssocCollection(string $name, string $themeId): array;

/**
* @throws NoSettingsFoundForThemeException
* @return array<string, string>
*/
public function getSettingsList(string $themeId): array;

/**
* @throws NoSettingsFoundForThemeException
*/
public function saveIntegerSetting(string $name, int $value, string $themeId): void;

/**
* @throws NoSettingsFoundForThemeException
*/
public function saveFloatSetting(string $name, float $value, string $themeId): void;

/**
* @throws NoSettingsFoundForThemeException
*/
public function saveBooleanSetting(string $name, bool $value, string $themeId): void;

/**
* @throws NoSettingsFoundForThemeException
*/
public function saveStringSetting(string $name, string $value, string $themeId): void;

/**
* @throws NoSettingsFoundForThemeException
*/
public function saveSelectSetting(string $name, string $value, string $themeId): void;

/**
* @throws NoSettingsFoundForThemeException
*/
public function saveCollectionSetting(string $name, array $value, string $themeId): void;

/**
* @throws NoSettingsFoundForThemeException
*/
public function saveAssocCollectionSetting(string $name, array $value, string $themeId): void;
}
9 changes: 9 additions & 0 deletions src/Setting/Service/CollectionEncodingServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,18 @@

namespace OxidEsales\GraphQL\ConfigurationAccess\Setting\Service;

use OxidEsales\GraphQL\ConfigurationAccess\Setting\Exception\CollectionEncodingException;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\Exception\InvalidCollectionException;

interface CollectionEncodingServiceInterface
{
/**
* @throws CollectionEncodingException
*/
public function encodeArrayToString(array $collection): string;

/**
* @throws InvalidCollectionException
*/
public function decodeStringCollectionToArray(string $value): array;
}
6 changes: 0 additions & 6 deletions src/Setting/Service/JsonCollectionEncodingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@

class JsonCollectionEncodingService implements CollectionEncodingServiceInterface
{
/**
* @throws CollectionEncodingException
*/
public function encodeArrayToString(array $collection): string
{
$jsonValue = json_encode($collection);
Expand All @@ -27,9 +24,6 @@ public function encodeArrayToString(array $collection): string
return $jsonValue;
}

/**
* @throws InvalidCollectionException
*/
public function decodeStringCollectionToArray(string $value): array
{
if ($value === '') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

use Doctrine\DBAL\Connection;
use OxidEsales\EshopCommunity\Internal\Framework\Config\Dao\ShopConfigurationSettingDaoInterface;
use OxidEsales\EshopCommunity\Internal\Framework\Config\Utility\ShopSettingEncoderInterface;
use OxidEsales\EshopCommunity\Internal\Framework\Database\QueryBuilderFactoryInterface;
use OxidEsales\EshopCommunity\Internal\Transition\Utility\BasicContextInterface;
use OxidEsales\EshopCommunity\Tests\Integration\IntegrationTestCase;
Expand Down Expand Up @@ -87,7 +86,6 @@ public function getSutForShop(int $shopId): ShopSettingRepository
$sut = new ShopSettingRepository(
basicContext: $basicContext,
queryBuilderFactory: $this->get(QueryBuilderFactoryInterface::class),
shopSettingEncoder: $this->get(ShopSettingEncoderInterface::class),
configurationSettingDao: $this->get(ShopConfigurationSettingDaoInterface::class)
);

Expand Down
Loading

0 comments on commit 16c6b6e

Please sign in to comment.