Minor code cleanup

This commit is contained in:
2021-04-28 20:17:37 +02:00
parent f51e306cda
commit aff231bcb6
8 changed files with 37 additions and 40 deletions

View File

@@ -4,6 +4,9 @@ namespace Domain\Model;
class MemoryConfiguration
{
private const MAX_MOTHERBOARD_SLOTS = 8;
private const OVERCLOCKING_VERIFIED = ['v', '2'];
public int $moduleSize;
public int $totalSize;
public string $totalSizeHr;
@@ -26,7 +29,7 @@ class MemoryConfiguration
)
{
$this->moduleSize = filter_var($this->moduleSizeHr, FILTER_SANITIZE_NUMBER_INT);
$this->overclockingVerified = in_array($this->overclockingSupport, ['v', '2'], true);
$this->overclockingVerified = in_array($this->overclockingSupport, self::OVERCLOCKING_VERIFIED, true);
$this->dualChannelOverclockingVerified = $this->overclockingSupport === '2';
$this->deriveAdditionalFields();
@@ -41,7 +44,7 @@ class MemoryConfiguration
private function findTotalSize(): void
{
// Initially 8 times the module size
$this->totalSize = 8 * $this->moduleSize;
$this->totalSize = self::MAX_MOTHERBOARD_SLOTS * $this->moduleSize;
// Work our way down to the module size...
while (!str_contains($this->module, $this->totalSize) && $this->totalSize > $this->moduleSize) {

View File

@@ -4,8 +4,12 @@ namespace Domain\Normalizer\Decorator;
use Domain\Normalizer\Normalizer;
abstract class NormalizerDecorator implements Normalizer
abstract class Decorator implements Normalizer
{
abstract protected function getAdditionalHeaders(): array;
abstract public function makePass(array $data): array;
private function __construct(
private Normalizer $parent
)
@@ -17,10 +21,6 @@ abstract class NormalizerDecorator implements Normalizer
return new static($normalizer);
}
abstract protected function getAdditionalHeaders(): array;
abstract public function makePass(array $normalized): array;
final public function getHeaders(): array
{
return array_merge($this->parent->getHeaders(), $this->getAdditionalHeaders());

View File

@@ -2,19 +2,19 @@
namespace Domain\Normalizer\Decorator;
class PricewatchDecorator extends NormalizerDecorator
class TweakersPricewatchUrl extends Decorator
{
public function makePass(array $normalized): array
public function makePass(array $data): array
{
$module = $normalized['module'] ?? '-';
$module = $data['module'] ?? '-';
$module = $this->selectSignificantTerms($module);
$normalized['pricewatch_url'] = sprintf(
$data['pricewatch_url'] = sprintf(
'https://tweakers.net/pricewatch/zoeken/?keyword=%s',
rawurlencode($module)
);
return $normalized;
return $data;
}
protected function getAdditionalHeaders(): array

View File

@@ -2,12 +2,9 @@
namespace Domain\Normalizer;
use Domain\Normalizer\Decorator\NormalizerDecorator;
interface Normalizer
{
public function getHeaders(): array;
public function normalize(object $object): array;
}

View File

@@ -17,15 +17,14 @@ class CsvEncoder implements StringEncoder
public function encode(Collection $items): string
{
$items = $items->values();
$buffer = implode($this->columnSeparator, $this->normalizer->getHeaders());
$buffer .= $this->rowSeparator;
$items->each(function (object $object) use (&$buffer) {
$items->values()->each(function (object $object) use (&$buffer) {
$normalized = $this->normalizer->normalize($object);
$buffer .= collect($normalized)
->map([CsvEncoder::class, 'formatBoolean'])
->map([CsvEncoder::class, 'formatEmpty'])
->map([static::class, 'formatBoolean'])
->map([static::class, 'formatEmpty'])
->implode($this->columnSeparator);
$buffer .= $this->rowSeparator;
});

View File

@@ -7,6 +7,8 @@ use Illuminate\Support\Collection;
class JsonEncoder implements StringEncoder
{
private const FLAGS = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_LINE_TERMINATORS | JSON_BIGINT_AS_STRING | JSON_THROW_ON_ERROR;
public function __construct(
private Normalizer $normalizer
)
@@ -15,16 +17,9 @@ class JsonEncoder implements StringEncoder
public function encode(Collection $items): string
{
return json_encode(
[
'count' => $items->count(),
'items' => $items->map([$this->normalizer, 'normalize'])->values()->toArray(),
],
JSON_UNESCAPED_SLASHES
| JSON_UNESCAPED_UNICODE
| JSON_UNESCAPED_LINE_TERMINATORS
| JSON_BIGINT_AS_STRING
| JSON_THROW_ON_ERROR
);
return json_encode([
'count' => $items->count(),
'items' => $items->map([$this->normalizer, 'normalize'])->values()->toArray(),
], self::FLAGS);
}
}