public function dump(Graph $graph, $file, $format = PlantUML::FORMAT_TXT)
    {
        $format = $format ?: self::FORMAT_TXT;

        try {
            $content = implode(PHP_EOL, $graph->toArray()) . PHP_EOL;

            if (self::FORMAT_UML === $format) {
                $url = sprintf('http://www.plantuml.com/plantuml/uml/%s', $this->urlEncode($content));

                if (false !== fwrite($file, $url . PHP_EOL)) {
                    return fclose($file);
                }

                return false;
            }

            if (self::FORMAT_TXT === $format) {
                if (false !== fwrite($file, $content)) {
                    return fclose($file);
                }

                return false;
            }

            if (in_array($format, [self::FORMAT_PNG, self::FORMAT_SVG, self::FORMAT_ATXT, self::FORMAT_UTXT], true)) {
                if (null === $this->java) {
                    return false;
                }

                $prefix = sys_get_temp_dir() . '/' . uniqid();
                $txtPath = $prefix . '.txt';
                $pngPath = $prefix . '.' . $format;

                $clean = function () use ($txtPath, $pngPath) {
                    $this->fs->remove([$txtPath, $pngPath]);
                };

                $this->fs->dumpFile($txtPath, $content);

                $builder = new ProcessBuilder();
                $builder
                    ->add($this->java)
                    ->add('-jar')
                    ->add(__DIR__ . '/../Resources/lib/plantuml.1.2017.19.jar')
                    ->add($txtPath);

                if (self::FORMAT_SVG === $format) {
                    $builder->add('-tsvg');
                }

                if (self::FORMAT_ATXT === $format) {
                    $builder->add('-txt');
                }

                if (self::FORMAT_UTXT === $format) {
                    $builder->add('-utxt');
                }

                $plantUml = $builder->getProcess();

                $plantUml->run();

                if ($plantUml->isSuccessful()) {
                    if (false !== $png = fopen($pngPath, 'r')) {
                        if (0 !== stream_copy_to_stream($png, $file)) {
                            $clean();

                            return fclose($file);
                        }
                    }
                } else {
                    $this->logger->error($plantUml->getErrorOutput());
                }

                $clean();

                return false;
            }

            return false;
        } catch (\Exception $e) {
            return false;
        }
    }