Skip to content

Commit

Permalink
Add section manager; improve error messages and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
gggeek committed Feb 12, 2017
1 parent af52c9b commit e1b19f5
Show file tree
Hide file tree
Showing 12 changed files with 299 additions and 41 deletions.
1 change: 0 additions & 1 deletion Core/Executor/ContentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,6 @@ protected function getUser($userKey)

/**
* Sets references to certain content attributes.
* The Content Manager currently supports setting references to object_id and location_id
*
* @param \eZ\Publish\API\Repository\Values\Content\Content|ContentCollection $content
* @throws \InvalidArgumentException When trying to set a reference to an unsupported attribute
Expand Down
2 changes: 1 addition & 1 deletion Core/Executor/ContentTypeGroupManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ protected function setReferences($object)
$value = $object->identifier;
break;
default:
throw new \InvalidArgumentException('Content Manager does not support setting references for attribute ' . $reference['attribute']);
throw new \InvalidArgumentException('Content Type Group Manager does not support setting references for attribute ' . $reference['attribute']);
}

$this->referenceResolver->addReference($reference['identifier'], $value);
Expand Down
2 changes: 1 addition & 1 deletion Core/Executor/ContentTypeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ protected function setReferences($contentType)

if ($contentType instanceof ContentTypeCollection) {
if (count($contentType) > 1) {
throw new \InvalidArgumentException('ContentType Manager does not support setting references for creating/updating of multiple content types');
throw new \InvalidArgumentException('Content Type Manager does not support setting references for creating/updating of multiple content types');
}
$contentType = reset($contentType);
}
Expand Down
9 changes: 4 additions & 5 deletions Core/Executor/LanguageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class LanguageManager extends RepositoryExecutor
protected $supportedActions = array('create', 'delete');

/**
* Handle the content create migration action
* Handle the language create migration action
*/
protected function create()
{
Expand Down Expand Up @@ -75,8 +75,7 @@ protected function delete()
}

/**
* Sets references to certain content attributes.
* The Content Manager currently supports setting references to object_id and location_id
* Sets references to certain language attributes.
*
* @param \eZ\Publish\API\Repository\Values\Content\Language|LanguageCollection $language
* @throws \InvalidArgumentException When trying to set a reference to an unsupported attribute
Expand All @@ -90,7 +89,7 @@ protected function setReferences($language)

if ($language instanceof LanguageCollection) {
if (count($language) > 1) {
throw new \InvalidArgumentException('Content Manager does not support setting references for creating/updating of multiple languages');
throw new \InvalidArgumentException('Language Manager does not support setting references for creating/updating of multiple languages');
}
$language = reset($language);
}
Expand All @@ -103,7 +102,7 @@ protected function setReferences($language)
$value = $language->id;
break;
default:
throw new \InvalidArgumentException('Content Manager does not support setting references for attribute ' . $reference['attribute']);
throw new \InvalidArgumentException('Language Manager does not support setting references for attribute ' . $reference['attribute']);
}

$this->referenceResolver->addReference($reference['identifier'], $value);
Expand Down
4 changes: 2 additions & 2 deletions Core/Executor/LocationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ protected function update()
$locationCollection = $this->matchLocations('update');

if (count($locationCollection) > 1 && isset($this->dsl['references'])) {
throw new \Exception("Can not execute Location update because multiple contents match, and a references section is specified in the dsl. References can be set when only 1 content matches");
throw new \Exception("Can not execute Location update because multiple locations match, and a references section is specified in the dsl. References can be set when only 1 location matches");
}

if (count($locationCollection) > 1 && isset($this->dsl['swap_with_location'])) {
throw new \Exception("Can not execute Location update because multiple contents match, and a swap_with_location is specified in the dsl.");
throw new \Exception("Can not execute Location update because multiple locations match, and a swap_with_location is specified in the dsl.");
}

// support legacy tag: parent_location_id
Expand Down
165 changes: 165 additions & 0 deletions Core/Executor/SectionManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<?php

namespace Kaliop\eZMigrationBundle\Core\Executor;

use Kaliop\eZMigrationBundle\API\Collection\SectionCollection;
use Kaliop\eZMigrationBundle\Core\Matcher\SectionMatcher;

/**
* Implements the actions for managing (create/update/delete) section in the system through
* migrations and abstracts away the eZ Publish Public API.
*/
class SectionManager extends RepositoryExecutor
{
protected $supportedStepTypes = array('section');

/** @var SectionMatcher $sectionMatcher */
protected $sectionMatcher;

/**
* @param SectionMatcher $sectionMatcher
*/
public function __construct(SectionMatcher $sectionMatcher)
{
$this->sectionMatcher = $sectionMatcher;
}

/**
* Handle the section create migration action
*/
protected function create()
{
$sectionService = $this->repository->getSectionService();

$sectionCreateStruct = $sectionService->newSectionCreateStruct();

$sectionCreateStruct->identifier = $this->dsl['identifier'];
$sectionCreateStruct->name = $this->dsl['name'];

$section = $sectionService->createSection($sectionCreateStruct);

$this->setReferences($section);

return $section;
}

/**
* Handle the section update migration action
*/
protected function update()
{
$sectionCollection = $this->matchSections('update');

if (count($sectionCollection) > 1 && array_key_exists('references', $this->dsl)) {
throw new \Exception("Can not execute Section update because multiple types match, and a references section is specified in the dsl. References can be set when only 1 section matches");
}

$sectionService = $this->repository->getSectionService();
foreach ($sectionCollection as $key => $section) {
$sectionUpdateStruct = $sectionService->newSectionUpdateStruct();

if (isset($this->dsl['identifier'])) {
$sectionUpdateStruct->identifier = $this->dsl['identifier'];
}
if (isset($this->dsl['identifier'])) {
$sectionUpdateStruct->name = $this->dsl['name'];
}

$section = $sectionService->updateSection($section, $sectionUpdateStruct);

$sectionCollection[$key] = $section;
}

$this->setReferences($sectionCollection);

return $sectionCollection;
}

/**
* Handle the section delete migration action
*/
protected function delete()
{
$sectionCollection = $this->matchSections('delete');

$sectionService = $this->repository->getSectionService();

foreach ($sectionCollection as $section) {
$sectionService->deleteSection($section);
}

return $sectionCollection;
}

/**
* @param string $action
* @return SectionCollection
* @throws \Exception
*/
protected function matchSections($action)
{
if (!isset($this->dsl['match'])) {
throw new \Exception("A match condition is required to $action a section.");
}

$match = $this->dsl['match'];

// convert the references passed in the match
foreach ($match as $condition => $values) {
if (is_array($values)) {
foreach ($values as $position => $value) {
$match[$condition][$position] = $this->referenceResolver->resolveReference($value);
}
} else {
$match[$condition] = $this->referenceResolver->resolveReference($values);
}
}

return $this->sectionMatcher->match($match);
}

/**
* Sets references to certain section attributes.
*
* @param \eZ\Publish\API\Repository\Values\Content\Section|SectionCollection $section
* @throws \InvalidArgumentException When trying to set a reference to an unsupported attribute
* @return boolean
*/
protected function setReferences($section)
{
if (!array_key_exists('references', $this->dsl)) {
return false;
}

if ($section instanceof SectionCollection) {
if (count($section) > 1) {
throw new \InvalidArgumentException('Section Manager does not support setting references for creating/updating of multiple sections');
}
$section = reset($section);
}

foreach ($this->dsl['references'] as $reference) {

switch ($reference['attribute']) {
case 'section_id':
case 'id':
$value = $section->id;
break;
case 'section_identifier':
case 'identifier':
$value = $section->identifier;
break;
case 'section_name':
case 'name':
$value = $section->name;
break;
default:
throw new \InvalidArgumentException('Section Manager does not support setting references for attribute ' . $reference['attribute']);
}

$this->referenceResolver->addReference($reference['identifier'], $value);
}

return true;
}
}
2 changes: 1 addition & 1 deletion Core/Executor/TagManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ protected function setReferences($object)
$value = $object->id;
break;
default:
throw new \InvalidArgumentException('Content Type Manager does not support setting references for attribute ' . $reference['attribute']);
throw new \InvalidArgumentException('Tag Manager does not support setting references for attribute ' . $reference['attribute']);
}

$this->referenceResolver->addReference($reference['identifier'], $value);
Expand Down
65 changes: 37 additions & 28 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ parameters:
ez_migration_bundle.executor.content_type_group_manager.class: Kaliop\eZMigrationBundle\Core\Executor\ContentTypeGroupManager
ez_migration_bundle.executor.location_manager.class: Kaliop\eZMigrationBundle\Core\Executor\LocationManager
ez_migration_bundle.executor.role_manager.class: Kaliop\eZMigrationBundle\Core\Executor\RoleManager
ez_migration_bundle.executor.section_manager.class: Kaliop\eZMigrationBundle\Core\Executor\SectionManager
ez_migration_bundle.executor.tag_manager.class: Kaliop\eZMigrationBundle\Core\Executor\TagManager
ez_migration_bundle.executor.user_group_manager.class: Kaliop\eZMigrationBundle\Core\Executor\UserGroupManager
ez_migration_bundle.executor.user_manager.class: Kaliop\eZMigrationBundle\Core\Executor\UserManager
Expand Down Expand Up @@ -182,16 +183,6 @@ services:
tags:
- { name: ez_migration_bundle.executor }

ez_migration_bundle.executor.location_manager:
parent: ez_migration_bundle.executor.repository
class: '%ez_migration_bundle.executor.location_manager.class%'
arguments:
- '@ez_migration_bundle.content_matcher'
- '@ez_migration_bundle.location_matcher'
- '@ez_migration_bundle.helper.sort_converter'
tags:
- { name: ez_migration_bundle.executor }

ez_migration_bundle.executor.content_type_manager:
parent: ez_migration_bundle.executor.repository
class: '%ez_migration_bundle.executor.content_type_manager.class%'
Expand All @@ -211,22 +202,36 @@ services:
tags:
- { name: ez_migration_bundle.executor }

ez_migration_bundle.executor.user_manager:
ez_migration_bundle.executor.language_manager:
class: '%ez_migration_bundle.executor.language_manager.class%'
parent: ez_migration_bundle.executor.repository
class: '%ez_migration_bundle.executor.user_manager.class%'
tags:
- { name: ez_migration_bundle.executor }

ez_migration_bundle.executor.location_manager:
parent: ez_migration_bundle.executor.repository
class: '%ez_migration_bundle.executor.location_manager.class%'
arguments:
- '@ez_migration_bundle.user_matcher'
- '@ez_migration_bundle.user_group_matcher'
- '@ez_migration_bundle.content_matcher'
- '@ez_migration_bundle.location_matcher'
- '@ez_migration_bundle.helper.sort_converter'
tags:
- { name: ez_migration_bundle.executor }

ez_migration_bundle.executor.user_group_manager:
ez_migration_bundle.executor.object_state_manager:
class: '%ez_migration_bundle.executor.object_state_manager.class%'
parent: ez_migration_bundle.executor.repository
class: '%ez_migration_bundle.executor.user_group_manager.class%'
arguments:
- '@ez_migration_bundle.user_group_matcher'
- '@ez_migration_bundle.role_matcher'
- '@ez_migration_bundle.section_matcher'
- '@ez_migration_bundle.object_state_matcher'
- '@ez_migration_bundle.object_state_group_matcher'
tags:
- { name: ez_migration_bundle.executor }

ez_migration_bundle.executor.object_state_group_manager:
class: '%ez_migration_bundle.executor.object_state_group_manager.class%'
parent: ez_migration_bundle.executor.repository
arguments:
- '@ez_migration_bundle.object_state_group_matcher'
tags:
- { name: ez_migration_bundle.executor }

Expand All @@ -239,9 +244,11 @@ services:
tags:
- { name: ez_migration_bundle.executor }

ez_migration_bundle.executor.language_manager:
class: '%ez_migration_bundle.executor.language_manager.class%'
ez_migration_bundle.executor.section_manager:
class: '%ez_migration_bundle.executor.section_manager.class%'
parent: ez_migration_bundle.executor.repository
arguments:
- '@ez_migration_bundle.section_matcher'
tags:
- { name: ez_migration_bundle.executor }

Expand All @@ -254,20 +261,22 @@ services:
tags:
- { name: ez_migration_bundle.executor }

ez_migration_bundle.executor.object_state_manager:
class: '%ez_migration_bundle.executor.object_state_manager.class%'
ez_migration_bundle.executor.user_manager:
parent: ez_migration_bundle.executor.repository
class: '%ez_migration_bundle.executor.user_manager.class%'
arguments:
- '@ez_migration_bundle.object_state_matcher'
- '@ez_migration_bundle.object_state_group_matcher'
- '@ez_migration_bundle.user_matcher'
- '@ez_migration_bundle.user_group_matcher'
tags:
- { name: ez_migration_bundle.executor }

ez_migration_bundle.executor.object_state_group_manager:
class: '%ez_migration_bundle.executor.object_state_group_manager.class%'
ez_migration_bundle.executor.user_group_manager:
parent: ez_migration_bundle.executor.repository
class: '%ez_migration_bundle.executor.user_group_manager.class%'
arguments:
- '@ez_migration_bundle.object_state_group_matcher'
- '@ez_migration_bundle.user_group_matcher'
- '@ez_migration_bundle.role_matcher'
- '@ez_migration_bundle.section_matcher'
tags:
- { name: ez_migration_bundle.executor }

Expand Down
Loading

0 comments on commit e1b19f5

Please sign in to comment.