/** * {@inheritdoc} */ public function save($data, $userId, $locale, $patch = false) { if ($this->getProperty($data, 'id')) { $categoryEntity = $this->findById($this->getProperty($data, 'id')); } else { $categoryEntity = $this->categoryRepository->createNew(); } // set user properties if userId is set. else these properties are set by the UserBlameSubscriber on save. if ($user = $userId ? $this->userRepository->findUserById($userId) : null) { if (!$categoryEntity->getCreator()) { $categoryEntity->setCreator($user); } $categoryEntity->setChanger($user); } $categoryWrapper = $this->getApiObject($categoryEntity, $locale); if (!$patch || $this->getProperty($data, 'name')) { $translationEntity = $this->findOrCreateCategoryTranslation($categoryEntity, $categoryWrapper, $locale); $translationEntity->setTranslation($this->getProperty($data, 'name', null)); } if (!$patch || $this->getProperty($data, 'description')) { $translationEntity = $this->findOrCreateCategoryTranslation($categoryEntity, $categoryWrapper, $locale); $translationEntity->setDescription($this->getProperty($data, 'description', null)); } if (!$patch || $this->getProperty($data, 'medias')) { $translationEntity = $this->findOrCreateCategoryTranslation($categoryEntity, $categoryWrapper, $locale); $translationEntity->setMedias(array_map(function ($item) { return $this->em->getReference(Media::class, $item); }, $this->getProperty($data, 'medias', []))); } $key = $this->getProperty($data, 'key'); if (!$patch || $key) { $categoryWrapper->setKey($key); } if (!$patch || $this->getProperty($data, 'meta')) { $metaData = is_array($this->getProperty($data, 'meta')) ? $this->getProperty($data, 'meta') : []; $metaEntities = []; foreach ($metaData as $meta) { $metaEntity = $this->categoryMetaRepository->createNew(); $metaEntity->setId($this->getProperty($meta, 'id')); $metaEntity->setKey($this->getProperty($meta, 'key')); $metaEntity->setValue($this->getProperty($meta, 'value')); $metaEntity->setLocale($this->getProperty($meta, 'locale')); $metaEntities[] = $metaEntity; } $categoryWrapper->setMeta($metaEntities); } if (!$patch || $this->getProperty($data, 'parent')) { if ($this->getProperty($data, 'parent')) { $parentEntity = $this->findById($this->getProperty($data, 'parent')); } else { $parentEntity = null; } $categoryWrapper->setParent($parentEntity); } if (!$categoryWrapper->getName()) { throw new CategoryNameMissingException(); } $categoryEntity = $categoryWrapper->getEntity(); $this->em->persist($categoryEntity); try { $this->em->flush(); } catch (UniqueConstraintViolationException $e) { throw new CategoryKeyNotUniqueException($key); } return $categoryEntity; }