Skip to content

Commit

Permalink
Performance improvement of SingleChannelContext
Browse files Browse the repository at this point in the history
  • Loading branch information
coldic3 committed Jun 2, 2023
1 parent a843241 commit c08715c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,9 @@ public function findAllWithBasicData(): iterable
->getResult(AbstractQuery::HYDRATE_ARRAY)
;
}

public function countAll(): int
{
return $this->count([]);
}
}
9 changes: 5 additions & 4 deletions src/Sylius/Component/Channel/Context/SingleChannelContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ public function __construct(private ChannelRepositoryInterface $channelRepositor

public function getChannel(): ChannelInterface
{
$channels = $this->channelRepository->findAll();
$channelsCount = $this->channelRepository->countAll();

if (1 !== count($channels)) {
if (1 !== $channelsCount) {
throw new ChannelNotFoundException();
}
$channel = reset($channels);
Assert::isInstanceOf($channel, ChannelInterface::class);

/** @var ChannelInterface $channel */
$channel = $this->channelRepository->findOneBy([]);

return $channel;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ public function findOneByCode(string $code): ?ChannelInterface;
public function findByName(string $name): iterable;

public function findAllWithBasicData(): iterable;

public function countAll(): int;
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,26 @@ function it_returns_a_channel_if_it_is_the_only_one_defined(
ChannelRepositoryInterface $channelRepository,
ChannelInterface $channel,
): void {
$channelRepository->findAll()->willReturn([$channel]);
$channelRepository->countAll()->willReturn(1);
$channelRepository->findOneBy([])->willReturn($channel);

$this->getChannel()->shouldReturn($channel);
}

function it_throws_a_channel_not_found_exception_if_there_are_no_channels_defined(
ChannelRepositoryInterface $channelRepository,
): void {
$channelRepository->findAll()->willReturn([]);
$channelRepository->countAll()->willReturn(0);
$channelRepository->findOneBy([])->shouldNotBeCalled();

$this->shouldThrow(ChannelNotFoundException::class)->during('getChannel');
}

function it_throws_a_channel_not_found_exception_if_there_are_many_channels_defined(
ChannelRepositoryInterface $channelRepository,
ChannelInterface $firstChannel,
ChannelInterface $secondChannel,
): void {
$channelRepository->findAll()->willReturn([$firstChannel, $secondChannel]);
$channelRepository->countAll()->willReturn(2);
$channelRepository->findOneBy([])->shouldNotBeCalled();

$this->shouldThrow(ChannelNotFoundException::class)->during('getChannel');
}
Expand Down

0 comments on commit c08715c

Please sign in to comment.