public function updateSection(Section $section, SectionUpdateStruct $sectionUpdateStruct)
{
if ($sectionUpdateStruct->name !== null && !is_string($sectionUpdateStruct->name)) {
throw new InvalidArgumentValue('name', $section->name, 'Section');
}
if ($sectionUpdateStruct->identifier !== null && !is_string($sectionUpdateStruct->identifier)) {
throw new InvalidArgumentValue('identifier', $section->identifier, 'Section');
}
if ($this->repository->canUser('section', 'edit', $section) !== true) {
throw new UnauthorizedException('section', 'edit');
}
if ($sectionUpdateStruct->identifier !== null) {
try {
$existingSection = $this->loadSectionByIdentifier($sectionUpdateStruct->identifier);
// Allowing identifier update only for the same section
if ($existingSection->id != $section->id) {
throw new InvalidArgumentException('sectionUpdateStruct', 'section with specified identifier already exists');
}
} catch (APINotFoundException $e) {
// Do nothing
}
}
$loadedSection = $this->loadSection($section->id);
$this->repository->beginTransaction();
try {
$spiSection = $this->sectionHandler->update($loadedSection->id, $sectionUpdateStruct->name ?: $loadedSection->name, $sectionUpdateStruct->identifier ?: $loadedSection->identifier);
$this->repository->commit();
} catch (Exception $e) {
$this->repository->rollback();
throw $e;
}
return $this->buildDomainSectionObject($spiSection);
}