Skip to content

Commit

Permalink
Update page repository to use date in function and not in construct
Browse files Browse the repository at this point in the history
  • Loading branch information
maximehuran committed Dec 3, 2024
1 parent 0400724 commit 399cbe3
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 29 deletions.
23 changes: 5 additions & 18 deletions src/Repository/PageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,15 @@

namespace MonsieurBiz\SyliusCmsPagePlugin\Repository;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadata;
use DateTimeInterface;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\QueryBuilder;
use MonsieurBiz\SyliusCmsPagePlugin\Entity\PageInterface;
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
use Sylius\Calendar\Provider\DateTimeProviderInterface;
use Sylius\Component\Channel\Model\ChannelInterface;

class PageRepository extends EntityRepository implements PageRepositoryInterface
{
private DateTimeProviderInterface $dateTimeProvider;

public function __construct(
EntityManagerInterface $em,
ClassMetadata $class,
DateTimeProviderInterface $dateTimeProvider
) {
$this->dateTimeProvider = $dateTimeProvider;
parent::__construct($em, $class);
}

public function createListQueryBuilder(string $localeCode): QueryBuilder
{
return $this->createQueryBuilder('o')
Expand Down Expand Up @@ -62,14 +49,14 @@ public function existsOneByChannelAndSlug(ChannelInterface $channel, ?string $lo
return $count > 0;
}

public function existsOneEnabledByChannelAndSlug(ChannelInterface $channel, ?string $locale, string $slug): bool
public function existsOneEnabledByChannelAndSlug(ChannelInterface $channel, ?string $locale, string $slug, DateTimeInterface $dateTime): bool
{
$queryBuilder = $this->createQueryBuilderExistOne($channel, $locale, $slug);
$queryBuilder
->andWhere('p.enabled = true')
->andWhere('p.publishAt IS NULL OR p.publishAt <= :now')
->andWhere('p.unpublishAt IS NULL OR p.unpublishAt >= :now')
->setParameter('now', $this->dateTimeProvider->now())
->setParameter('now', $dateTime)
;

$count = (int) $queryBuilder
Expand All @@ -83,7 +70,7 @@ public function existsOneEnabledByChannelAndSlug(ChannelInterface $channel, ?str
/**
* @throws NonUniqueResultException
*/
public function findOneEnabledBySlugAndChannelCode(string $slug, string $localeCode, string $channelCode): ?PageInterface
public function findOneEnabledBySlugAndChannelCode(string $slug, string $localeCode, string $channelCode, DateTimeInterface $dateTime): ?PageInterface
{
return $this->createQueryBuilder('p')
->leftJoin('p.translations', 'translation')
Expand All @@ -94,7 +81,7 @@ public function findOneEnabledBySlugAndChannelCode(string $slug, string $localeC
->andWhere('p.enabled = true')
->andWhere('p.publishAt IS NULL OR p.publishAt <= :now')
->andWhere('p.unpublishAt IS NULL OR p.unpublishAt >= :now')
->setParameter('now', $this->dateTimeProvider->now())
->setParameter('now', $dateTime)
->setParameter('localeCode', $localeCode)
->setParameter('slug', $slug)
->setParameter('channelCode', $channelCode)
Expand Down
5 changes: 3 additions & 2 deletions src/Repository/PageRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace MonsieurBiz\SyliusCmsPagePlugin\Repository;

use DateTimeInterface;
use Doctrine\ORM\QueryBuilder;
use MonsieurBiz\SyliusCmsPagePlugin\Entity\PageInterface;
use Sylius\Component\Channel\Model\ChannelInterface;
Expand All @@ -24,7 +25,7 @@ public function createListQueryBuilder(string $localeCode): QueryBuilder;

public function existsOneByChannelAndSlug(ChannelInterface $channel, ?string $locale, string $slug, array $excludedPages = []): bool;

public function existsOneEnabledByChannelAndSlug(ChannelInterface $channel, ?string $locale, string $slug): bool;
public function existsOneEnabledByChannelAndSlug(ChannelInterface $channel, ?string $locale, string $slug, DateTimeInterface $dateTime): bool;

public function findOneEnabledBySlugAndChannelCode(string $slug, string $localeCode, string $channelCode): ?PageInterface;
public function findOneEnabledBySlugAndChannelCode(string $slug, string $localeCode, string $channelCode, DateTimeInterface $dateTime): ?PageInterface;
}
1 change: 1 addition & 0 deletions src/Resources/config/routing/shop.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ monsieurbiz_cms_page_show:
- $slug
- "expr:service('sylius.context.locale').getLocaleCode()"
- "expr:service('sylius.context.channel').getChannel().getCode()"
- "expr:service('Sylius\\\\Calendar\\\\Provider\\\\DateTimeProviderInterface').now()"
requirements:
slug: .+
condition: "not(context.getPathInfo() matches '`^%sylius.security.new_api_route%`') and context.checkPageSlug(request)"
10 changes: 3 additions & 7 deletions src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ services:
decorates: router.request_context
arguments: ['@MonsieurBiz\SyliusCmsPagePlugin\Routing\RequestContext.inner']

# Page Repository
monsieurbiz_cms_page.repository.page:
class: MonsieurBiz\SyliusCmsPagePlugin\Repository\PageRepository
arguments:
$em: '@doctrine.orm.default_entity_manager'
$class: '@=service("doctrine.orm.default_entity_manager").getClassMetadata("MonsieurBiz\\SyliusCmsPagePlugin\\Entity\\PageInterface")'
$dateTimeProvider: '@Sylius\Calendar\Provider\DateTimeProviderInterface'
Sylius\Calendar\Provider\DateTimeProviderInterface:
class: Sylius\Calendar\Provider\Calendar
public: true
13 changes: 11 additions & 2 deletions src/Routing/PageSlugConditionChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace MonsieurBiz\SyliusCmsPagePlugin\Routing;

use MonsieurBiz\SyliusCmsPagePlugin\Repository\PageRepositoryInterface;
use Sylius\Calendar\Provider\DateTimeProviderInterface;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Channel\Context\ChannelNotFoundException;
use Sylius\Component\Locale\Context\LocaleContextInterface;
Expand All @@ -35,17 +36,24 @@ final class PageSlugConditionChecker
*/
private $localeContext;

/**
* @var DateTimeProviderInterface
*/
private $dateTimeProvider;

/**
* PageSlugConditionChecker constructor.
*/
public function __construct(
PageRepositoryInterface $pageRepository,
ChannelContextInterface $channelContext,
LocaleContextInterface $localeContext
LocaleContextInterface $localeContext,
DateTimeProviderInterface $dateTimeProvider
) {
$this->pageRepository = $pageRepository;
$this->channelContext = $channelContext;
$this->localeContext = $localeContext;
$this->dateTimeProvider = $dateTimeProvider;
}

public function isPageSlug(string $slug): bool
Expand All @@ -54,7 +62,8 @@ public function isPageSlug(string $slug): bool
return $this->pageRepository->existsOneEnabledByChannelAndSlug(
$this->channelContext->getChannel(),
$this->localeContext->getLocaleCode(),
$slug
$slug,
$this->dateTimeProvider->now()
);
} catch (ChannelNotFoundException $channelNotFoundException) {
return false;
Expand Down

0 comments on commit 399cbe3

Please sign in to comment.