-
-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Port from 2.4 management of Obj states and State Groups
- Loading branch information
gggeek
committed
Oct 16, 2016
1 parent
7785c80
commit 07cf23d
Showing
14 changed files
with
796 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Kaliop\eZMigrationBundle\API\Collection; | ||
|
||
/** | ||
* @todo add phpdoc to suggest typehinting | ||
*/ | ||
class ObjectStateGroupCollection extends AbstractCollection | ||
{ | ||
protected $allowedClass = 'eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
<?php | ||
|
||
namespace Kaliop\eZMigrationBundle\Core\Executor; | ||
|
||
use Kaliop\eZMigrationBundle\API\Collection\ObjectStateGroupCollection; | ||
use Kaliop\eZMigrationBundle\Core\Matcher\ObjectStateGroupMatcher; | ||
|
||
class ObjectStateGroupManager extends RepositoryExecutor | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
protected $supportedStepTypes = array('object_state_group'); | ||
|
||
/** | ||
* @var ObjectStateGroupMatcher | ||
*/ | ||
protected $objectStateGroupMatcher; | ||
|
||
/** | ||
* @param ObjectStateGroupMatcher $objectStateGroupMatcher | ||
*/ | ||
public function __construct(ObjectStateGroupMatcher $objectStateGroupMatcher) | ||
{ | ||
$this->objectStateGroupMatcher = $objectStateGroupMatcher; | ||
} | ||
|
||
/** | ||
* Handle the create step of object state group migrations | ||
* | ||
* @todo add support for flexible defaultLanguageCode | ||
*/ | ||
protected function create() | ||
{ | ||
foreach(array('names', 'identifier') as $key) { | ||
if (!isset($this->dsl[$key])) { | ||
throw new \Exception("The '$key' key is missing in a object state group creation definition"); | ||
} | ||
} | ||
|
||
$objectStateService = $this->repository->getObjectStateService(); | ||
|
||
$objectStateGroupCreateStruct = $objectStateService->newObjectStateGroupCreateStruct($this->dsl['identifier']); | ||
$objectStateGroupCreateStruct->defaultLanguageCode = self::DEFAULT_LANGUAGE_CODE; | ||
|
||
foreach ($this->dsl['names'] as $languageCode => $name) { | ||
$objectStateGroupCreateStruct->names[$languageCode] = $name; | ||
} | ||
if (isset($this->dsl['descriptions'])) { | ||
foreach ($this->dsl['descriptions'] as $languageCode => $description) { | ||
$objectStateGroupCreateStruct->descriptions[$languageCode] = $description; | ||
} | ||
} | ||
|
||
$objectStateGroup = $objectStateService->createObjectStateGroup($objectStateGroupCreateStruct); | ||
|
||
$this->setReferences($objectStateGroup); | ||
|
||
return $objectStateGroup; | ||
} | ||
|
||
/** | ||
* Handle the update step of object state group migrations | ||
* | ||
* @todo add support for defaultLanguageCode | ||
*/ | ||
protected function update() | ||
{ | ||
$objectStateService = $this->repository->getObjectStateService(); | ||
|
||
$groupsCollection = $this->matchObjectStateGroups('update'); | ||
|
||
if (count($groupsCollection) > 1 && isset($this->dsl['references'])) { | ||
throw new \Exception("Can not execute Object State Group update because multiple groups match, and a references section is specified in the dsl. References can be set when only 1 state group matches"); | ||
} | ||
|
||
if (count($groupsCollection) > 1 && isset($this->dsl['identifier'])) { | ||
throw new \Exception("Can not execute Object State Group update because multiple groups match, and an identifier is specified in the dsl."); | ||
} | ||
|
||
foreach ($groupsCollection as $objectStateGroup) { | ||
//$objectStateGroup = $objectStateService->loadObjectStateGroup($this->dsl['id']); | ||
|
||
$objectStateGroupUpdateStruct = $objectStateService->newObjectStateGroupUpdateStruct(); | ||
|
||
if (isset($this->dsl['identifier'])) { | ||
$objectStateGroupUpdateStruct->identifier = $this->dsl['identifier']; | ||
} | ||
if (isset($this->dsl['names'])) { | ||
foreach ($this->dsl['names'] as $languageCode => $name) { | ||
$objectStateGroupUpdateStruct->names[$languageCode] = $name; | ||
} | ||
} | ||
if (isset($this->dsl['descriptions'])) { | ||
foreach ($this->dsl['descriptions'] as $languageCode => $description) { | ||
$objectStateGroupUpdateStruct->descriptions[$languageCode] = $description; | ||
} | ||
} | ||
$objectStateGroup = $objectStateService->updateObjectStateGroup($objectStateGroup, $objectStateGroupUpdateStruct); | ||
|
||
$this->setReferences($objectStateGroup); | ||
} | ||
|
||
return $groupsCollection; | ||
} | ||
|
||
/** | ||
* Handle the delete step of object state group migrations | ||
*/ | ||
protected function delete() | ||
{ | ||
$groupsCollection = $this->matchObjectStateGroups('delete'); | ||
|
||
$objectStateService = $this->repository->getObjectStateService(); | ||
|
||
foreach ($groupsCollection as $objectStateGroup) { | ||
$objectStateService->deleteObjectStateGroup($objectStateGroup); | ||
} | ||
|
||
return $groupsCollection; | ||
} | ||
|
||
/** | ||
* @param string $action | ||
* @return ObjectStateGroupCollection | ||
* @throws \Exception | ||
*/ | ||
protected function matchObjectStateGroups($action) | ||
{ | ||
if (!isset($this->dsl['match'])) { | ||
throw new \Exception("A match condition is required to $action an ObjectStateGroup."); | ||
} | ||
|
||
$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->objectStateGroupMatcher->match($match); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function setReferences($objectStateGroup) | ||
{ | ||
if (!array_key_exists('references', $this->dsl)) { | ||
return false; | ||
} | ||
|
||
foreach ($this->dsl['references'] as $reference) { | ||
switch ($reference['attribute']) { | ||
case 'object_state_group_id': | ||
case 'id': | ||
$value = $objectStateGroup->id; | ||
break; | ||
case 'object_state_group_identifier': | ||
case 'identifier': | ||
$value = $objectStateGroup->id; | ||
break; | ||
default: | ||
throw new \InvalidArgumentException('Object State Group Manager does not support setting references for attribute ' . $reference['attribute']); | ||
} | ||
|
||
$this->referenceResolver->addReference($reference['identifier'], $value); | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
<?php | ||
|
||
namespace Kaliop\eZMigrationBundle\Core\Executor; | ||
|
||
use Kaliop\eZMigrationBundle\Core\Matcher\ObjectStateGroupMatcher; | ||
use Kaliop\eZMigrationBundle\Core\Matcher\ObjectStateMatcher; | ||
use Kaliop\eZMigrationBundle\API\Collection\ObjectStateCollection; | ||
use Kaliop\eZMigrationBundle\API\KeyMatcherInterface; | ||
|
||
class ObjectStateManager extends RepositoryExecutor | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
protected $supportedStepTypes = array('object_state'); | ||
|
||
/** | ||
* @var ObjectStateMatcher | ||
*/ | ||
protected $objectStateMatcher; | ||
|
||
/** | ||
* @var ObjectStateGroupMatcher | ||
*/ | ||
protected $objectStateGroupMatcher; | ||
|
||
/** | ||
* @param ObjectStateMatcher $objectStateMatcher | ||
* @param ObjectStateGroupMatcher $objectStateGroupMatcher | ||
*/ | ||
public function __construct(ObjectStateMatcher $objectStateMatcher, ObjectStateGroupMatcher $objectStateGroupMatcher) | ||
{ | ||
$this->objectStateMatcher = $objectStateMatcher; | ||
$this->objectStateGroupMatcher = $objectStateGroupMatcher; | ||
} | ||
|
||
/** | ||
* Handle the create step of object state migrations. | ||
* | ||
* @throws \Exception | ||
*/ | ||
protected function create() | ||
{ | ||
foreach(array('object_state_group', 'names', 'identifier') as $key) { | ||
if (!isset($this->dsl[$key])) { | ||
throw new \Exception("The '$key' key is missing in a object state creation definition"); | ||
} | ||
} | ||
|
||
if (!count($this->dsl['names'])) { | ||
throw new \Exception('No object state names have been defined. Need to specify at least one to create the state.'); | ||
} | ||
|
||
$objectStateService = $this->repository->getObjectStateService(); | ||
|
||
$objectStateGroupId = $this->dsl['object_state_group']; | ||
$objectStateGroupId = $this->referenceResolver->resolveReference($objectStateGroupId); | ||
$objectStateGroup = $this->objectStateGroupMatcher->matchOneByKey($objectStateGroupId); | ||
|
||
$objectStateCreateStruct = $objectStateService->newObjectStateCreateStruct($this->dsl['identifier']); | ||
$objectStateCreateStruct->defaultLanguageCode = self::DEFAULT_LANGUAGE_CODE; | ||
|
||
foreach ($this->dsl['names'] as $languageCode => $name) { | ||
$objectStateCreateStruct->names[$languageCode] = $name; | ||
} | ||
if (isset($this->dsl['descriptions'])) { | ||
foreach ($this->dsl['descriptions'] as $languageCode => $description) { | ||
$objectStateCreateStruct->descriptions[$languageCode] = $description; | ||
} | ||
} | ||
|
||
$objectState = $objectStateService->createObjectState($objectStateGroup, $objectStateCreateStruct); | ||
|
||
$this->setReferences($objectState); | ||
|
||
return $objectState; | ||
} | ||
|
||
/** | ||
* Handle the update step of object state migrations. | ||
* | ||
* @throws \Exception | ||
*/ | ||
protected function update() | ||
{ | ||
$stateCollection = $this->matchObjectStates('update'); | ||
|
||
if (count($stateCollection) > 1 && array_key_exists('references', $this->dsl)) { | ||
throw new \Exception("Can not execute Object State update because multiple states match, and a references section is specified in the dsl. References can be set when only 1 state matches"); | ||
} | ||
|
||
if (count($stateCollection) > 1 && isset($this->dsl['identifier'])) { | ||
throw new \Exception("Can not execute Object State update because multiple states match, and an identifier is specified in the dsl."); | ||
} | ||
|
||
$objectStateService = $this->repository->getObjectStateService(); | ||
|
||
foreach ($stateCollection as $state) { | ||
$objectStateUpdateStruct = $objectStateService->newObjectStateUpdateStruct(); | ||
|
||
if (isset($this->dsl['identifier'])) { | ||
$objectStateUpdateStruct->identifier = $this->dsl['identifier']; | ||
} | ||
if (isset($this->dsl['names'])) { | ||
foreach ($this->dsl['names'] as $name) { | ||
$objectStateUpdateStruct->names[$name['languageCode']] = $name['name']; | ||
} | ||
} | ||
if (isset($this->dsl['descriptions'])) { | ||
foreach ($this->dsl['descriptions'] as $languageCode => $description) { | ||
$objectStateUpdateStruct->descriptions[$languageCode] = $description; | ||
} | ||
} | ||
$state = $objectStateService->updateObjectState($state, $objectStateUpdateStruct); | ||
|
||
$this->setReferences($state); | ||
} | ||
|
||
return $stateCollection; | ||
} | ||
|
||
/** | ||
* Handle the deletion step of object state migrations. | ||
*/ | ||
protected function delete() | ||
{ | ||
$stateCollection = $this->matchObjectStates('delete'); | ||
|
||
$objectStateService = $this->repository->getObjectStateService(); | ||
|
||
foreach ($stateCollection as $state) { | ||
$objectStateService->deleteObjectState($state); | ||
} | ||
|
||
return $stateCollection; | ||
} | ||
|
||
/** | ||
* @param string $action | ||
* @return ObjectStateCollection | ||
* @throws \Exception | ||
*/ | ||
private function matchObjectStates($action) | ||
{ | ||
if (!isset($this->dsl['match'])) { | ||
throw new \Exception("A match Condition is required to $action an object state."); | ||
} | ||
|
||
$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->objectStateMatcher->match($match); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function setReferences($objectState) | ||
{ | ||
if (!array_key_exists('references', $this->dsl)) { | ||
return false; | ||
} | ||
|
||
foreach ($this->dsl['references'] as $reference) { | ||
switch ($reference['attribute']) { | ||
case 'object_state_id': | ||
case 'id': | ||
$value = $objectState->id; | ||
break; | ||
default: | ||
throw new \InvalidArgumentException('Object State Manager does not support setting references for attribute ' . $reference['attribute']); | ||
} | ||
|
||
$this->referenceResolver->addReference($reference['identifier'], $value); | ||
} | ||
|
||
return true; | ||
} | ||
} |
Oops, something went wrong.