public function delete($stateId)
{
// Get the object state first as we need group ID
// to reorder the priorities and reassign content to another state in the group
$objectState = $this->load($stateId);
$this->objectStateGateway->deleteObjectState($objectState->id);
$remainingStates = $this->loadObjectStates($objectState->groupId);
if (empty($remainingStates)) {
// If there are no more states in the group, just remove the state links
$this->objectStateGateway->deleteObjectStateLinks($objectState->id);
return;
}
$priority = 0;
foreach ($remainingStates as $remainingState) {
$this->objectStateGateway->updateObjectStatePriority($remainingState->id, $priority);
++$priority;
}
$remainingStates = $this->loadObjectStates($objectState->groupId);
$this->objectStateGateway->updateObjectStateLinks($objectState->id, current($remainingStates)->id);
}