Skip to content

Commit

Permalink
Refactoring -> Add a SnapshotProxy Factory class
Browse files Browse the repository at this point in the history
  • Loading branch information
FabienD committed Nov 22, 2015
1 parent 3802747 commit 902eace
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 58 deletions.
11 changes: 2 additions & 9 deletions CmsManager/CmsSnapshotManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,14 @@ class CmsSnapshotManager extends BaseCmsPageManager
*/
protected $pages = array();

/**
* @var string
*/
protected $snapshotPageProxyClass;

/**
* @param SnapshotManagerInterface $snapshotManager
* @param TransformerInterface $transformer
* @param string $snapshotPageProxyClass Namespace of SnaphostPageProxy class
*/
public function __construct(SnapshotManagerInterface $snapshotManager, TransformerInterface $transformer, $snapshotPageProxyClass = 'Sonata\PageBundle\Model\SnapshotPageProxy')
public function __construct(SnapshotManagerInterface $snapshotManager, TransformerInterface $transformer)
{
$this->snapshotManager = $snapshotManager;
$this->transformer = $transformer;
$this->snapshotPageProxyClass = $snapshotPageProxyClass;
}

/**
Expand Down Expand Up @@ -146,7 +139,7 @@ protected function getPageBy(SiteInterface $site = null, $fieldName, $value)
throw new PageNotFoundException();
}

$page = new $this->snapshotPageProxyClass($this->snapshotManager, $this->transformer, $snapshot);
$page = $this->snapshotManager->createSnapShopPageProxy($this->transformer, $snapshot);

$this->pages[$id] = false;

Expand Down
64 changes: 27 additions & 37 deletions Entity/SnapshotManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
use Sonata\DatagridBundle\Pager\Doctrine\Pager;
use Sonata\DatagridBundle\ProxyQuery\Doctrine\ProxyQuery;
use Sonata\PageBundle\Model\PageInterface;
use Sonata\PageBundle\Model\SnapshotInterface;
use Sonata\PageBundle\Model\SnapshotManagerInterface;
use Sonata\PageBundle\Model\SnapshotPageProxyFactory;
use Sonata\PageBundle\Model\TransformerInterface;

/**
* This class manages SnapshotInterface persistency with the Doctrine ORM.
Expand All @@ -36,24 +39,24 @@ class SnapshotManager extends BaseEntityManager implements SnapshotManagerInterf
protected $templates = array();

/**
* @var string
* @var SnapshotPageProxyFactory
*/
protected $snapshotPageProxyClass;
protected $snapshotPageProxyFactory;

/**
* Constructor.
*
* @param string $class Namespace of entity class
* @param ManagerRegistry $registry An entity manager instance
* @param array $templates An array of templates
* @param string $snapshotPageProxyClass Namespace of SnapshotPageProxy class
* @param string $class Namespace of entity class
* @param ManagerRegistry $registry An entity manager instance
* @param array $templates An array of templates
* @param SnapshotPageProxyFactory $snapshotPageProxyFactory SnapshotPageProxy factory
*/
public function __construct($class, ManagerRegistry $registry, $templates = array(), $snapshotPageProxyClass = 'Sonata\PageBundle\Model\SnapshotPageProxy')
public function __construct($class, ManagerRegistry $registry, $templates = array(), SnapshotPageProxyFactory $snapshotPageProxyFactory)
{
parent::__construct($class, $registry);

$this->templates = $templates;
$this->snapshotPageProxyClass = $snapshotPageProxyClass;
$this->templates = $templates;
$this->snapshotPageProxyFactory = $snapshotPageProxyFactory;
}

/**
Expand Down Expand Up @@ -145,34 +148,6 @@ public function findEnableSnapshot(array $criteria)
return $query->getQuery()->getOneOrNullResult();
}

/**
* return a page with the given routeName.
*
* @param string $routeName
*
* @return PageInterface|false
*/
public function getPageByName($routeName)
{
$snapshots = $this->getEntityManager()->createQueryBuilder()
->select('s')
->from($this->class, 's')
->where('s.routeName = :routeName')
->setParameters(array(
'routeName' => $routeName,
))
->getQuery()
->execute();

$snapshot = count($snapshots) > 0 ? $snapshots[0] : false;

if ($snapshot) {
return new $this->snapshotPageProxy($this, $snapshot);
}

return false;
}

/**
* @param array $templates
*/
Expand Down Expand Up @@ -323,4 +298,19 @@ public function getPager(array $criteria, $page, $limit = 10, array $sort = arra

return $pager;
}

/**
* Create a snapShotPageProxy instance.
*
* @param TransformerInterface $transformer
* @param SnapshotInterface $snapshot
*
* @return SnapshotPageProxyInterface
*/
public function createSnapShopPageProxy(TransformerInterface $transformer, SnapshotInterface $snapshot)
{
return $this->snapshotPageProxyFactory
->create($this, $transformer, $snapshot)
;
}
}
10 changes: 2 additions & 8 deletions Entity/Transformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,19 @@ class Transformer implements TransformerInterface
*/
protected $children = array();

/**
* @var string
*/
protected $snapshotPageProxyClass;

/**
* @param SnapshotManagerInterface $snapshotManager
* @param PageManagerInterface $pageManager
* @param BlockManagerInterface $blockManager
* @param RegistryInterface $registry
* @param string $snapshotPageProxyClass Namespace of SnapshotPageProxy class
*/
public function __construct(SnapshotManagerInterface $snapshotManager, PageManagerInterface $pageManager, BlockManagerInterface $blockManager, RegistryInterface $registry, $snapshotPageProxyClass = 'Sonata\PageBundle\Model\SnapshotPageProxy')
public function __construct(SnapshotManagerInterface $snapshotManager, PageManagerInterface $pageManager, BlockManagerInterface $blockManager, RegistryInterface $registry)
{
$this->snapshotManager = $snapshotManager;
$this->pageManager = $pageManager;
$this->blockManager = $blockManager;
$this->registry = $registry;
$this->snapshotPageProxyClass = $snapshotPageProxyClass;
}

/**
Expand Down Expand Up @@ -288,7 +282,7 @@ public function getChildren(PageInterface $parent)
$pages = array();

foreach ($snapshots as $snapshot) {
$page = new $this->snapshotPageProxy($this->snapshotManager, $this, $snapshot);
$page = $this->snapshotManager->createSnapShopPageProxy($this, $snapshot);
$pages[$page->getId()] = $page;
}

Expand Down
10 changes: 10 additions & 0 deletions Model/SnapshotManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,14 @@ public function enableSnapshots(array $snapshots, \DateTime $date = null);
* @return int The number of deleted rows
*/
public function cleanup(PageInterface $page, $keep);

/**
* Create snapShotPageProxy instance.
*
* @param TransformerInterface $transformer
* @param SnapshotInterface $snapshot
*
* @return mixed
*/
public function createSnapShopPageProxy(TransformerInterface $transformer, SnapshotInterface $snapshot);
}
35 changes: 35 additions & 0 deletions Model/SnapshotPageProxyFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Sonata\PageBundle\Model;

class SnapshotPageProxyFactory
{
/*
* @var string
*/
public $snapShotPageProxyClass;

/**
* SnapshotPageProxyFactory constructor.
*
* @param string $snapShotPageProxyClass SnapShopPageProxy class name
*/
public function __construct($snapShotPageProxyClass)
{
$this->snapShotPageProxyClass = $snapShotPageProxyClass;
}

/**
* Create snapshot instance.
*
* @param SnapshotManagerInterface $manager
* @param TransformerInterface $transformer
* @param SnapshotInterface $snapshot
*
* @return SnapshotPageProxyInterface
*/
public function create(SnapshotManagerInterface $manager, TransformerInterface $transformer, SnapshotInterface $snapshot)
{
return new $this->snapShotPageProxyClass($manager, $transformer, $snapshot);
}
}
9 changes: 7 additions & 2 deletions Resources/config/orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@
<parameter key="sonata.page.block_interactor.class">Sonata\PageBundle\Entity\BlockInteractor</parameter>
<parameter key="sonata.page.transformer.class">Sonata\PageBundle\Entity\Transformer</parameter>
<parameter key="sonata.page.proxy.snapshot.class">Sonata\PageBundle\Model\SnapshotPageProxy</parameter>
<parameter key="sonata.page.proxy.snapshot.factory.class">Sonata\PageBundle\Model\SnapshotPageProxyFactory</parameter>
</parameters>

<services>
<service id="sonata.page.proxy.snapshot.factory" class="%sonata.page.proxy.snapshot.factory.class%">
<argument>%sonata.page.proxy.snapshot.class%</argument>
</service>

<service id="sonata.page.manager.page" class="%sonata.page.manager.page.class%">
<argument>%sonata.page.page.class%</argument>
<argument type="service" id="doctrine" />
Expand All @@ -26,7 +31,8 @@
<service id="sonata.page.manager.snapshot" class="%sonata.page.manager.snapshot.class%">
<argument>%sonata.page.snapshot.class%</argument>
<argument type="service" id="doctrine" />
<argument>%sonata.page.proxy.snapshot.class%</argument>
<argument />
<argument type="service" id="sonata.page.proxy.snapshot.factory" />
</service>

<service id="sonata.page.manager.block" class="%sonata.page.manager.block.class%">
Expand All @@ -49,7 +55,6 @@
<argument type="service" id="sonata.page.manager.page" />
<argument type="service" id="sonata.page.manager.block" />
<argument type="service" id="doctrine" />
<argument>%sonata.page.proxy.snapshot.class%</argument>
</service>

</services>
Expand Down
1 change: 0 additions & 1 deletion Resources/config/page.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@

<argument type="service" id="sonata.page.manager.snapshot" />
<argument type="service" id="sonata.page.transformer" />
<argument>%sonata.page.proxy.snapshot.class%</argument>
</service>

<service id="sonata.page.decorator_strategy" class="%sonata.page.decorator_strategy.class%">
Expand Down
7 changes: 6 additions & 1 deletion Tests/Entity/SnapshotManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ protected function getSnapshotManager($qbCallback)
$registry = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');
$registry->expects($this->any())->method('getManagerForClass')->will($this->returnValue($em));

return new SnapshotManager('Sonata\PageBundle\Entity\BaseSnapshot', $registry);
$snapshotProxyFactory = $this->getMockBuilder('Sonata\PageBundle\Model\SnapshotPageProxyFactory')
->setConstructorArgs(array('Sonata\PageBundle\Model\SnapshotPageProxy'))
->getMock()
;

return new SnapshotManager('Sonata\PageBundle\Entity\BaseSnapshot', $registry, array(), $snapshotProxyFactory);
}

public function testGetPager()
Expand Down

0 comments on commit 902eace

Please sign in to comment.