/**
  * Create service
  *
  * @param ServiceLocatorInterface $serviceLocator
  * @throws \InvalidArgumentException
  * @return FileTypeAdapterManager
  */
 public function createService(ServiceLocatorInterface $serviceLocator)
 {
     $config = $serviceLocator->get('config');
     if (!array_key_exists('prooph.link.fileconnector', $config)) {
         throw new \InvalidArgumentException('Missing prooph.link.fileconnector root config key');
     }
     if (!is_array($config['prooph.link.fileconnector'])) {
         throw new \InvalidArgumentException("Config for prooph.link.fileconnector must be an array");
     }
     if (!array_key_exists('file_types', $config['prooph.link.fileconnector'])) {
         throw new \InvalidArgumentException('Missing file_types in prooph.link.fileconnector config');
     }
     $fileTypeAdapters = new FileTypeAdapterManager(new Config($config['prooph.link.fileconnector']['file_types']));
     $fileTypeAdapters->setServiceLocator($serviceLocator);
     return $fileTypeAdapters;
 }
 /**
  * @param WorkflowMessage $workflowMessage
  * @throws \InvalidArgumentException
  */
 private function processData(WorkflowMessage $workflowMessage)
 {
     $metadata = $workflowMessage->metadata();
     if (isset($metadata[self::META_LOCATION]) && !isset($metadata[self::META_PATH])) {
         $metadata[self::META_PATH] = $this->locationTranslator->getPathFor($metadata[self::META_LOCATION]);
     }
     if (!isset($metadata[self::META_FILENAME_TEMPLATE])) {
         throw new \InvalidArgumentException("Missing filename_pattern in metadata");
     }
     if (!isset($metadata[self::META_FILE_TYPE])) {
         throw new \InvalidArgumentException("Missing file_type in metadata");
     }
     if (!isset($metadata[self::META_PATH])) {
         throw new \InvalidArgumentException("Missing path in metadata");
     }
     $metadata[self::META_PATH] = $this->sanitizePath($metadata[self::META_PATH]);
     if (!is_dir($metadata[self::META_PATH])) {
         throw new \InvalidArgumentException(sprintf('Directory %s is invalid', $metadata[self::META_PATH]));
     }
     if (!is_writable($metadata[self::META_PATH])) {
         throw new \InvalidArgumentException(sprintf('Directory %s is not writable', $metadata[self::META_PATH]));
     }
     $type = $workflowMessage->payload()->toType();
     $fileTypeAdapter = $this->fileTypeAdapters->get($metadata[self::META_FILE_TYPE]);
     if ($type->description()->nativeType() === NativeType::COLLECTION && isset($metadata[self::META_WRITE_MULTI_FILES]) && $metadata[self::META_WRITE_MULTI_FILES]) {
         foreach ($type as $index => $item) {
             $this->writeTypeToFile($item, $metadata, $fileTypeAdapter, $index);
         }
     } else {
         $this->writeTypeToFile($type, $metadata, $fileTypeAdapter);
     }
     return $workflowMessage->answerWithDataProcessingCompleted($metadata);
 }
 /**
  * @test
  */
 public function it_provides_a_json_type_adapter_when_specifying_json_as_file_type()
 {
     $jsonAdapter = $this->fileTypeAdapters->get('json');
     $this->assertInstanceOf('Prooph\\Link\\FileConnector\\Service\\FileTypeAdapter\\JsonTypeAdapter', $jsonAdapter);
 }