/** * @param string $class * @param array $fields * @param array $relationships * @param array $metadata * @return ResourceEntityInterface * @throws EntityConflictExceptionInterface * @throws ValidationExceptionInterface */ public function create($class, array $fields, array $relationships = [], array $metadata = []) { $class = $this->metadataCache->getReflection($class); $params = array_merge($metadata, $relationships, $fields); $entity = Reflection::instance($class, $params); // Removes parameters used in the constructor of the entity. $this->cleanParams($class->getConstructor(), $fields, $relationships, $metadata); if ($class->implementsInterface(self::AUTHOR_IS_OWNER)) { $relationships['owner'] = $this->securityContext->getToken()->getUser(); } $this->setFields($class, $entity, $fields)->setRelationships($class, $entity, $relationships)->validate($entity); try { $this->em->persist($entity); $this->em->flush(); } catch (ORMException $e) { throw new PersistenceException(self::ERROR_COULD_NOT_CREATE); } return $entity; }