Skip to content

Commit

Permalink
Addressed some qodana warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
turbo-ele committed Jan 31, 2024
1 parent 91db3b3 commit a5836ea
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
31 changes: 18 additions & 13 deletions src/Resolver/Location/FindParentStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@

namespace Pimcore\Bundle\DataImporterBundle\Resolver\Location;

use Exception;
use Pimcore\Bundle\DataImporterBundle\Exception\InvalidConfigurationException;
use Pimcore\Bundle\DataImporterBundle\Exception\InvalidInputException;
use Pimcore\Bundle\DataImporterBundle\Tool\DataObjectLoader;
use Pimcore\Model\DataObject;
use Pimcore\Model\DataObject\AbstractObject;
use Pimcore\Model\DataObject\ClassDefinition;
use Pimcore\Model\Element\ElementInterface;

Expand Down Expand Up @@ -134,25 +136,28 @@ public function updateParent(ElementInterface $element, array $inputData): Eleme
}

if ($newParent) {
if ($newParent->getType() === DataObject::OBJECT_TYPE_VARIANT) {
if ($newParent->getType() === AbstractObject::OBJECT_TYPE_VARIANT) {
throw new InvalidInputException(
'The elements desired parent is a variant which cannot have any child elements'
);
}

// Check if element should be saved as a variant.
if (
$this->saveAsVariant
&& $element instanceof DataObject\Concrete
&& $element::class === $newParent::class
&& $element->getClass()->getAllowVariants()
&& !$element->hasChildren()
) {
/** @var DataObject\Concrete $element */
$element->setType(DataObject::OBJECT_TYPE_VARIANT);
try {
// Check if element should be saved as a variant.
if (
$this->saveAsVariant
&& $element instanceof DataObject\Concrete
&& $element::class === $newParent::class
&& !$element->hasChildren()
&& $element->getClass()->getAllowVariants()
) {
$element->setType(AbstractObject::OBJECT_TYPE_VARIANT);
}

return $element->setParent($newParent);
} catch (Exception) {
// Exception might be thrown by $element->getClass().
}

return $element->setParent($newParent);
}

return $element;
Expand Down
10 changes: 8 additions & 2 deletions src/Resolver/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use Pimcore\Bundle\DataImporterBundle\Resolver\Load\LoadStrategyInterface;
use Pimcore\Bundle\DataImporterBundle\Resolver\Location\LocationStrategyInterface;
use Pimcore\Bundle\DataImporterBundle\Resolver\Publish\PublishStrategyInterface;
use Pimcore\Model\DataObject;
use Pimcore\Model\DataObject\AbstractObject;
use Pimcore\Model\Element\ElementInterface;

class Resolver
Expand Down Expand Up @@ -136,6 +136,9 @@ public function loadElementByIdentifier($identifier): ?ElementInterface
return $this->getLoadingStrategy()->loadElementByIdentifier($identifier);
}

/**
* @throws InvalidInputException
*/
public function loadOrCreateAndPrepareElement(array $inputData, bool $createNew = true): ?ElementInterface
{
$element = $this->loadElement($inputData);
Expand All @@ -155,7 +158,10 @@ public function loadOrCreateAndPrepareElement(array $inputData, bool $createNew
$this->getLocationUpdateStrategy()->updateParent($element, $inputData);

// The parent of a variant cannot be changed anymore.
if ($oldParentId !== $element->getParentId() && $element->getType() === DataObject::OBJECT_TYPE_VARIANT) {
if (
$oldParentId !== $element->getParentId()
&& $element->getType() === AbstractObject::OBJECT_TYPE_VARIANT
) {
throw new InvalidInputException(
"Element with id `{$element->getId()}` is a variant and cannot change its parent anymore"
);
Expand Down

0 comments on commit a5836ea

Please sign in to comment.