Skip to content

Commit

Permalink
Decouple and deprecate NotificationBundle
Browse files Browse the repository at this point in the history
  • Loading branch information
eerison committed May 18, 2022
1 parent f1bbad0 commit 1ac060d
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 7 deletions.
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
],
"homepage": "https://docs.sonata-project.org/projects/SonataPageBundle",
"require": {
"php": "^7.3 || ^8.0",
"php": "^7.4 || ^8.0",
"cocur/slugify": "^3.0 || ^4.0",
"doctrine/doctrine-bundle": "^1.12.3 || ^2.0",
"doctrine/persistence": "^1.3.4 || ^2.0",
Expand Down Expand Up @@ -92,7 +92,10 @@
}
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"phpstan/extension-installer": true
}
},
"extra": {
"branch-alias": {
Expand Down
7 changes: 4 additions & 3 deletions src/Command/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
use Sonata\NotificationBundle\Backend\BackendInterface;
use Sonata\PageBundle\CmsManager\CmsPageManager;
use Sonata\PageBundle\CmsManager\DecoratorStrategyInterface;
use Sonata\PageBundle\Entity\SnapshotManager;
use Sonata\PageBundle\Listener\ExceptionListener;
use Sonata\PageBundle\Model\PageManagerInterface;
use Sonata\PageBundle\Model\Site;
use Sonata\PageBundle\Model\SiteManagerInterface;
use Sonata\PageBundle\Model\SnapshotManagerInterface;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
Expand Down Expand Up @@ -91,19 +93,18 @@ public function getErrorListener()
* @param string $mode
*
* @return BackendInterface
* @deprecated This method will be removed on release 4.x
*/
public function getNotificationBackend($mode)
{
trigger_deprecation('sonata-project/page-bundle', '3.27.0', 'This "getNotificationBackend" will be removed on release 4.x.');
if ('async' === $mode) {
return $this->getContainer()->get('sonata.notification.backend');
}

return $this->getContainer()->get('sonata.notification.backend.runtime');
}

/**
* @return array
*/
protected function getSites(InputInterface $input)
{
$parameters = [];
Expand Down
1 change: 1 addition & 0 deletions src/Consumer/CreateSnapshotConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* @final since sonata-project/page-bundle 3.26
* @deprecated it'll be removed on version 4.x
*/
class CreateSnapshotConsumer implements ConsumerInterface
{
Expand Down
16 changes: 16 additions & 0 deletions src/Service/CreateSnapshotsFromPageInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Sonata\PageBundle\Service;

use Sonata\PageBundle\Model\PageInterface;
use Sonata\PageBundle\Model\SiteInterface;
use Sonata\PageBundle\Model\SnapshotInterface;

interface CreateSnapshotsFromPageInterface
{
/**
* @param iterable<PageInterface> $pages
* @return iterable<SnapshotInterface>
*/
public function createFromPages(iterable $pages): iterable;
}
64 changes: 64 additions & 0 deletions src/Service/CreateSnapshotsService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Sonata\PageBundle\Service;

use Sonata\PageBundle\Model\PageInterface;
use Sonata\PageBundle\Model\PageManagerInterface;
use Sonata\PageBundle\Model\SiteInterface;
use Sonata\PageBundle\Model\SnapshotInterface;
use Sonata\PageBundle\Model\SnapshotManagerInterface;
use Sonata\PageBundle\Model\TransformerInterface;

final class CreateSnapshotsService implements CreateSnapshotsFromPageInterface
{
protected SnapshotManagerInterface $snapshotManager;

protected PageManagerInterface $pageManager;

protected TransformerInterface $transformer;

public function __construct(
SnapshotManagerInterface $snapshotManager,
PageManagerInterface $pageManager,
TransformerInterface $transformer
) {
$this->snapshotManager = $snapshotManager;
$this->pageManager = $pageManager;
$this->transformer = $transformer;
}

/**
* @param iterable<PageInterface> $pages
* @return iterable<SnapshotInterface>
*/
public function createFromPages(iterable $pages): iterable
{
$entityManager = $this->snapshotManager->getEntityManager();

// start a transaction
$entityManager->beginTransaction();

foreach ($pages as $page) {
yield $this->createByPage($page);
}

// commit the changes
$entityManager->commit();
}

protected function createByPage(PageInterface $page): SnapshotInterface
{
// creating snapshot
$snapshot = $this->transformer->create($page);

// update the page status
$page->setEdited(false);
$this->pageManager->save($page);

// save the snapshot
$this->snapshotManager->save($snapshot);
$this->snapshotManager->enableSnapshots([$snapshot]);

return $snapshot;
}
}
4 changes: 2 additions & 2 deletions tests/Model/PageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public function testSlugify(): void
static::assertSame(Page::slugify('S§!@@#$#$alut'), 's-alut');
static::assertSame(Page::slugify('Symfony2'), 'symfony2');
static::assertSame(Page::slugify('test'), 'test');
static::assertSame(Page::slugify('c\'est bientôt l\'été'), 'c-est-bientot-l-ete');
static::assertSame(Page::slugify(urldecode('%2Fc\'est+bientôt+l\'été')), 'c-est-bientot-l-ete');
static::assertSame(Page::slugify('c\'est bientôt l\'été'), 'c-est-bientt-l-t');
static::assertSame(Page::slugify(urldecode('%2Fc\'est+bientôt+l\'été')), 'c-est-bientt-l-t');
}

public function testHeader(): void
Expand Down

0 comments on commit 1ac060d

Please sign in to comment.