public function testFlattensADto()
 {
     // Create Metadata definition helper
     $config = new ConfigProxy(array("metadatafields" => array('saml20_idp' => array('foo:bar:baz' => array('supported' => array(1))))));
     $metadataDefinitionHelper = new MetadataDefinitionHelper($config);
     // Mock metadata dto
     $nestedMetadata = array('foo' => array('bar' => array('baz' => 1)));
     $metadataDtoDisassembler = new MetadataTreeFlattener($metadataDefinitionHelper);
     $expectedFlatCollection = array('foo:bar:baz' => 1);
     $this->assertEquals($expectedFlatCollection, $metadataDtoDisassembler->flatten($nestedMetadata, 'saml20-idp'));
 }
 /**
  * Creates a new connection and/or revision from a data transfer object.
  *
  * @param ConnectionDto $dto
  * @return Connection
  * @throws DBALException
  * @throws Exception
  * @throws ConnectionExistsException
  */
 public function save(ConnectionDto $dto, $ignoreMissingDefinition = false)
 {
     $entityManager = $this->entityManager;
     $entityManager->getConnection()->beginTransaction();
     $connection = $this->createConnection($dto->name, $dto->type, $dto->id);
     // Create new revision
     $connection->update($this->metadataDefinitionHelper, $dto->name, $dto->type, $dto->parentRevisionNr, $dto->revisionNote, $dto->state, $dto->expirationDate, $dto->metadataUrl, $dto->allowAllEntities, $dto->arpAttributes, $dto->manipulationCode, $dto->isActive, $dto->notes);
     // Update connection and new revision
     $entityManager->persist($connection);
     $entityManager->flush($connection);
     $latestRevision = $connection->getLatestRevision();
     foreach ($this->disassembleConnectionReferences($dto->allowedConnections) as $referencedConnection) {
         $latestRevision->allowConnection($referencedConnection);
     }
     foreach ($this->disassembleConnectionReferences($dto->blockedConnections) as $referencedConnection) {
         $latestRevision->blockConnection($referencedConnection);
     }
     foreach ($this->disassembleConnectionReferences($dto->disableConsentConnections) as $referencedConnection) {
         $latestRevision->disableConsentForConnection($referencedConnection);
     }
     // Update connection and new revision
     $entityManager->persist($connection);
     try {
         $entityManager->flush($connection);
     } catch (DBALException $ex) {
         $pdoException = $ex->getPrevious();
         if (!$pdoException instanceof PDOException) {
             if ($pdoException->getCode() == 23000) {
                 throw new ConnectionExistsException($pdoException->getMessage());
             }
         }
         throw $ex;
     }
     // Store metadata
     $flatMetadata = array();
     if ($dto->metadata) {
         $flatMetadata = $this->metadataTreeFlattener->flatten($dto->metadata, $dto->type, $ignoreMissingDefinition);
     }
     $latestRevision = $connection->getLatestRevision();
     foreach ($flatMetadata as $key => $value) {
         // Note that empty values are no longer saved
         if ($value === null || $value === '') {
             continue;
         }
         $metadataRecord = new Metadata($latestRevision, $key, $value);
         $entityManager->persist($metadataRecord);
     }
     $entityManager->flush();
     $entityManager->getConnection()->commit();
     $entityManager->clear();
     return $this->findById($connection->getId());
 }