Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid getting the theme if we have it already #14894

Merged
merged 2 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions src/Sylius/Bundle/CoreBundle/Theme/ChannelBasedThemeContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,31 @@

final class ChannelBasedThemeContext implements ThemeContextInterface
{
private null|false|ThemeInterface $theme = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A separate property for boolean would be better IMO eg. $alreadyCalled.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree. false means "no theme", null means not set, and the ThemeInterface means we have one.

All 3 values are logical.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm I see the difference between the 3 of them but no theme also means nothing has been set yet, and not set means there is no theme so it's tricky at first glance – at least for me 😄

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Defaulting it to false is the closest thing PHP has to an is_initialized($this->prop); type of check. If it's false, the property hasn't been initialized yet and whatever code needs to seed it needs to run. Once it's null or an object, it's initialized.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ok with the current implementation if anybody asks me 😂


public function __construct(private ChannelContextInterface $channelContext, private ThemeRepositoryInterface $themeRepository)
{
}

/**
* @psalm-suppress InvalidReturnType
*/
public function getTheme(): ?ThemeInterface
{
try {
/** @var ChannelInterface $channel */
$channel = $this->channelContext->getChannel();
$themeName = $channel->getThemeName();

if (null === $themeName) {
if (false === $this->theme) {
try {
/** @var ChannelInterface $channel */
$channel = $this->channelContext->getChannel();
$themeName = $channel->getThemeName();
$this->theme = null === $themeName
? null
: $this->themeRepository->findOneByName($themeName)
;
} catch (ChannelNotFoundException|\Exception) {
return null;
}

return $this->themeRepository->findOneByName($themeName);
} catch (ChannelNotFoundException) {
return null;
} catch (\Exception) {
return null;
}

return $this->theme;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace spec\Sylius\Bundle\CoreBundle\Theme;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Sylius\Bundle\ThemeBundle\Context\ThemeContextInterface;
use Sylius\Bundle\ThemeBundle\Model\ThemeInterface;
use Sylius\Bundle\ThemeBundle\Repository\ThemeRepositoryInterface;
Expand Down Expand Up @@ -53,7 +54,40 @@ function it_returns_null_if_channel_has_no_theme(
): void {
$channelContext->getChannel()->willReturn($channel);
$channel->getThemeName()->willReturn(null);
$themeRepository->findOneByName(null)->willReturn(null);
$themeRepository->findOneByName(Argument::any())->shouldNotBeCalled();

$this->getTheme()->shouldReturn(null);
}

function it_returns_previously_found_theme(
ChannelContextInterface $channelContext,
ThemeRepositoryInterface $themeRepository,
ThemeInterface $theme,
): void {
$object = $this->object->getWrappedObject();
$objectReflection = new \ReflectionObject($object);
$property = $objectReflection->getProperty('theme');
$property->setAccessible(true);
$property->setValue($object, $theme->getWrappedObject());

$channelContext->getChannel()->shouldNotBeCalled();
$themeRepository->findOneByName(Argument::any())->shouldNotBeCalled();

$this->getTheme()->shouldReturn($theme);
}

function it_returns_null_if_the_theme_was_not_found_previously(
ChannelContextInterface $channelContext,
ThemeRepositoryInterface $themeRepository,
): void {
$object = $this->object->getWrappedObject();
$objectReflection = new \ReflectionObject($object);
$property = $objectReflection->getProperty('theme');
$property->setAccessible(true);
$property->setValue($object, null);

$channelContext->getChannel()->shouldNotBeCalled();
$themeRepository->findOneByName(Argument::any())->shouldNotBeCalled();

$this->getTheme()->shouldReturn(null);
}
Expand Down