public function collectAndWriteCoverage($args)
    {
        if ($this->valid($args)) {
            extract($args);

            // Check for exist and valid hook
            if (isset($hook) && $hook instanceof HookInterface) {
                $this->hook = $hook;
                unset($hook);
            }

            // Get the realpath coverage directory
            $coverage_dir = realpath($coverage_dir);
            $coverage_file = $coverage_dir.DIRECTORY_SEPARATOR.self::COVERAGE_FILE;
            $coverage_output = $coverage_dir.DIRECTORY_SEPARATOR.self::COVERAGE_OUTPUT;

            // Get the coverage information
            if (is_dir($coverage_dir) && is_file($coverage_file)) {
                // Build the coverage xml object
                $xml = file_get_contents($coverage_file);
                $coverage = new SimpleXMLElement($xml);

                // Prepare the coveralls payload
                $data = $this->collect($coverage, $args);

                // Write the coverage output
                $this->printer->out('Writing coverage output...');
                file_put_contents($coverage_output, json_encode($data->all(), JSON_NUMERIC_CHECK));
            }
        }
    }