Skip to content

Commit

Permalink
Check allowed_children and getPossibleChildTypes in fixture generator (
Browse files Browse the repository at this point in the history
  • Loading branch information
jverdeyen authored and Kristof Jochmans committed Aug 9, 2016
1 parent 62d83db commit 7705e7c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
39 changes: 36 additions & 3 deletions src/Kunstmaan/FixturesBundle/Builder/PageBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
use Kunstmaan\NodeBundle\Entity\NodeTranslation;
use Kunstmaan\NodeBundle\Entity\NodeVersion;
use Kunstmaan\NodeBundle\Entity\StructureNode;
use Kunstmaan\NodeBundle\Helper\PagesConfiguration;
use Kunstmaan\NodeBundle\Helper\Services\ACLPermissionCreatorService;
use Kunstmaan\UtilitiesBundle\Helper\Slugifier;
use Kunstmaan\UtilitiesBundle\Helper\ClassLookup;
use Kunstmaan\PagePartBundle\Entity\PageTemplateConfiguration;
use Kunstmaan\UtilitiesBundle\Helper\ClassLookup;
use Kunstmaan\UtilitiesBundle\Helper\Slugifier;

class PageBuilder implements BuilderInterface
{
Expand All @@ -25,11 +26,17 @@ class PageBuilder implements BuilderInterface
private $populator;
private $slugifier;

/**
* @var PagesConfiguration
*/
private $pagesConfiguration;

public function __construct(
EntityManager $em,
ACLPermissionCreatorService $aclPermissionCreatorService,
Populator $populator,
Slugifier $slugifier
Slugifier $slugifier,
PagesConfiguration $pagesConfiguration
) {
$this->manager = $em;
$this->nodeRepo = $em->getRepository('KunstmaanNodeBundle:Node');
Expand All @@ -38,6 +45,7 @@ public function __construct(
$this->aclPermissionCreatorService = $aclPermissionCreatorService;
$this->populator = $populator;
$this->slugifier = $slugifier;
$this->pagesConfiguration = $pagesConfiguration;
}

public function canBuild(Fixture $fixture)
Expand Down Expand Up @@ -170,8 +178,15 @@ private function createRootNode($page, $language, $internalName, $fixtureParams)
isset($fixtureParams['hidden_from_nav']) ? $fixtureParams['hidden_from_nav'] : false
);
$parent = $this->getParentNode($fixtureParams, $language);

if ($parent instanceof Node) {
$rootNode->setParent($parent);

if (!$this->canHaveChild($parent->getRefEntityName(), get_class($page))) {
throw new \Exception(
sprintf('A %s can\'t have a %s as child. Forgot to add in allowed_children or getPossibleChildTypes?', $parent->getRefEntityName(), get_class($page))
);
}
}

return $rootNode;
Expand Down Expand Up @@ -229,4 +244,22 @@ private static function incrementString($string, $append = '-v')
return $string . $append . '1';
}
}

/**
* @param string $parentPageClass
* @param string $childPageClass
* @return bool
*/
private function canHaveChild($parentPageClass, $childPageClass)
{
$childTypes = $this->pagesConfiguration->getPossibleChildTypes($parentPageClass);

foreach ($childTypes as $childType) {
if ($childType['class'] == $childPageClass) {
return true;
}
}

return false;
}
}
1 change: 1 addition & 0 deletions src/Kunstmaan/FixturesBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ services:
- '@kunstmaan_node.acl_permission_creator_service'
- '@kunstmaan_fixtures.populator.populator'
- '@kunstmaan_utilities.slugifier'
- '@kunstmaan_node.pages_configuration'
tags:
- { name: kunstmaan_fixtures.builder, alias: PageBuilder }

Expand Down

0 comments on commit 7705e7c

Please sign in to comment.