Esempio n. 1
0
 public function __invoke(ConvertJsonToCSV $command)
 {
     $logger = ChapterLogger::fromChapterCommand($command, $this->doneBackend);
     $inputFile = $command->inputFile();
     $outputFile = $command->outputFile();
     if (!file_exists($inputFile)) {
         $logger->error('{file} does not exist', ['file' => $inputFile]);
         return;
     }
     $jsonData = json_decode(file_get_contents($inputFile), true);
     if (null === $jsonData) {
         $logger->error('{file} contains invalid json. {json_error} error was returned', ['file' => $inputFile, 'json_error' => json_last_error()]);
         return;
     }
     $csv = new SplFileObject($outputFile, 'w');
     if (!is_array($jsonData) || count($jsonData) === 0) {
         $logger->error('No data found in {file}', ['file' => $inputFile]);
         return;
     }
     $header = array_keys($jsonData[0]);
     $csv->fputcsv($header);
     foreach ($jsonData as $entry) {
         $csv->fputcsv(array_values($entry));
     }
     $logger->info('CSV {file} was created', ['file' => $outputFile, 'success' => true]);
 }
 /**
  * @test
  */
 public function it_pipes_middleware_and_executes_the_pipeline()
 {
     $chapterCommand = $this->prophesize(Message::class);
     $chapterCommand->metadata()->willReturn([Metadata::STORY_CHAPTER => Uuid::uuid4()->toString() . '____1', Metadata::STORY_NAME => 'Test Story']);
     $chapterCommand->messageName()->willReturn('Chapter Command');
     $chapterCommand->uuid()->willReturn(Uuid::uuid4());
     $doneBackend = $this->prophesize(DoneBackend::class);
     $chapterLogger = ChapterLogger::fromChapterCommand($chapterCommand->reveal(), $doneBackend->reveal());
     $workflow = new Workflow();
     $workflow->pipe(function (Message $chapterCommand, array $userData, ChapterLogger $chapterLogger, callable $next) {
         $user = UserDictionary::fromNativeValue($userData);
         return $next($chapterCommand, $user, $chapterLogger);
     });
     $workflow->pipe(function (Message $chapterCommand, UserDictionary $user, ChapterLogger $chapterLogger, callable $next) {
         return $next($chapterCommand, $user->property('address')->type(), $chapterLogger);
     });
     $userData = ['id' => 1, 'name' => 'Alex', 'address' => ['street' => 'Main Street', 'streetNumber' => 10, 'zip' => '12345', 'city' => 'Test City']];
     $addressData = $workflow($chapterCommand->reveal(), $userData, $chapterLogger, function (Message $chapterCommand, AddressDictionary $address, ChapterLogger $chapterLogger) {
         return Func::to_data($address);
     });
     $this->assertEquals(['street' => 'Main Street', 'streetNumber' => 10, 'zip' => '12345', 'city' => 'Test City'], $addressData);
 }
Esempio n. 3
0
 public function __invoke(MergeJsonFiles $command)
 {
     $logger = ChapterLogger::fromChapterCommand($command, $this->doneBackend);
     if (!is_dir($command->jsonFilesDirectory())) {
         $logger->error('{directory} is not a directory', ['directory' => $command->jsonFilesDirectory()]);
         return;
     }
     if (!is_writable($command->jsonFilesDirectory())) {
         $logger->error('{directory} is not writable', ['directory' => $command->jsonFilesDirectory()]);
         return;
     }
     $toFilePath = $command->jsonFilesDirectory() . DIRECTORY_SEPARATOR . $command->toFilename();
     //Remove old file if exists
     if (file_exists($toFilePath)) {
         unlink($toFilePath);
     }
     $directory = new \DirectoryIterator($command->jsonFilesDirectory());
     $data = [];
     $filePattern = $command->filePattern();
     foreach ($directory as $file) {
         /** @var $file \SplFileInfo */
         if ($file->isDot()) {
             continue;
         }
         if (!preg_match($filePattern, $file->getFilename())) {
             continue;
         }
         $path = $command->jsonFilesDirectory() . DIRECTORY_SEPARATOR . $file->getFilename();
         $content = file_get_contents($path);
         if (empty($content)) {
             $logger->warning('{file} skipped as it does not contain content', ['file' => $path]);
             continue;
         }
         $data[] = json_decode($content, true);
     }
     file_put_contents($toFilePath, json_encode($data));
     $logger->info('{file_count} files merged into {to_filename}', ['success' => true, 'file_count' => count($data), 'to_file_path' => $toFilePath]);
 }