Skip to content

Commit

Permalink
Upping the DX a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Jul 1, 2024
1 parent 8d3d727 commit deb322e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 74 deletions.
49 changes: 14 additions & 35 deletions src/IndexScope/IndexScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,55 +8,34 @@

/**
* NOT final - this makes it easier for plugin users to override this class and provide their own scope for an index
*
* @immutable
*/
class IndexScope
{
/**
* The index that this scope applies to
*/
public Index $index;

public ?string $channelCode = null;

public ?string $localeCode = null;

public ?string $currencyCode = null;

public function __construct(Index $index)
{
$this->index = $index;
public function __construct(
/**
* The index that this scope applies to
*/
public readonly Index $index,
public readonly ?string $channelCode = null,
public readonly ?string $localeCode = null,
public readonly ?string $currencyCode = null,
) {
}

/**
* @return static
*/
public function withChannelCode(?string $channelCode): self
{
$obj = clone $this;
$obj->channelCode = $channelCode;

return $obj;
return new self($this->index, $channelCode, $this->localeCode, $this->currencyCode);
}

/**
* @return static
*/
public function withLocaleCode(?string $localeCode): self
{
$obj = clone $this;
$obj->localeCode = $localeCode;

return $obj;
return new self($this->index, $this->channelCode, $localeCode, $this->currencyCode);
}

/**
* @return static
*/
public function withCurrencyCode(?string $currencyCode): self
{
$obj = clone $this;
$obj->currencyCode = $currencyCode;

return $obj;
return new self($this->index, $this->channelCode, $this->localeCode, $currencyCode);
}
}
46 changes: 19 additions & 27 deletions src/Provider/IndexScope/ProductIndexScopeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,30 @@

final class ProductIndexScopeProvider implements IndexScopeProviderInterface
{
private ChannelContextInterface $channelContext;

private LocaleContextInterface $localeContext;

private CurrencyContextInterface $currencyContext;

private ChannelRepositoryInterface $channelRepository;

public function __construct(
ChannelContextInterface $channelContext,
LocaleContextInterface $localeContext,
CurrencyContextInterface $currencyContext,
ChannelRepositoryInterface $channelRepository,
private readonly ChannelContextInterface $channelContext,
private readonly LocaleContextInterface $localeContext,
private readonly CurrencyContextInterface $currencyContext,
private readonly ChannelRepositoryInterface $channelRepository,
) {
$this->channelContext = $channelContext;
$this->localeContext = $localeContext;
$this->currencyContext = $currencyContext;
$this->channelRepository = $channelRepository;
}

public function getAll(Index $index): iterable
{
/** @var ChannelInterface[] $channels */
$channels = $this->channelRepository->findAll();
$channels = $this->channelRepository->findBy([
'enabled' => true,
]);

foreach ($channels as $channel) {
foreach ($channel->getLocales() as $locale) {
foreach ($channel->getCurrencies() as $currency) {
yield (new IndexScope($index))
->withChannelCode($channel->getCode())
->withLocaleCode($locale->getCode())
->withCurrencyCode($currency->getCode())
;
yield new IndexScope(
index: $index,
channelCode: $channel->getCode(),
localeCode: $locale->getCode(),
currencyCode: $currency->getCode(),
);
}
}
}
Expand All @@ -69,11 +60,12 @@ public function getFromChannelAndLocaleAndCurrency(
string $localeCode = null,
string $currencyCode = null,
): IndexScope {
return (new IndexScope($index))
->withChannelCode($channelCode)
->withLocaleCode($localeCode)
->withCurrencyCode($currencyCode)
;
return new IndexScope(
index: $index,
channelCode: $channelCode,
localeCode: $localeCode,
currencyCode: $currencyCode,
);
}

public function supports(Index $index): bool
Expand Down
16 changes: 4 additions & 12 deletions src/Provider/IndexScope/TaxonIndexScopeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,10 @@

final class TaxonIndexScopeProvider implements IndexScopeProviderInterface
{
private RepositoryInterface $localeRepository;

private LocaleContextInterface $localeContext;

public function __construct(
RepositoryInterface $localeRepository,
LocaleContextInterface $localeContext,
private readonly RepositoryInterface $localeRepository,
private readonly LocaleContextInterface $localeContext,
) {
$this->localeRepository = $localeRepository;
$this->localeContext = $localeContext;
}

public function getAll(Index $index): iterable
Expand All @@ -31,7 +25,7 @@ public function getAll(Index $index): iterable
$locales = $this->localeRepository->findAll();

foreach ($locales as $locale) {
yield (new IndexScope($index))->withLocaleCode($locale->getCode());
yield new IndexScope(index: $index, localeCode: $locale->getCode());
}
}

Expand All @@ -50,9 +44,7 @@ public function getFromChannelAndLocaleAndCurrency(
string $localeCode = null,
string $currencyCode = null,
): IndexScope {
return (new IndexScope($index))
->withLocaleCode($localeCode)
;
return new IndexScope(index: $index, localeCode: $localeCode);
}

public function supports(Index $index): bool
Expand Down

0 comments on commit deb322e

Please sign in to comment.