/** * Create a .fit file and write data to it. * @param \Fit\Data $data * @param string $filepath * @return string The filepath of the file that was created * @throws \Fit\Exception */ public function writeData(\Fit\Data $data, $filepath = false) { if ($filepath === false) { $filepath = tempnam('/tmp', 'fit'); } if (false === $filepath) { \Fit\Exception::create(1001); } if (false === ($file = @fopen($filepath, 'wb'))) { \Fit\Exception::create(1001); } $this->writer = new \Zend_Io_Writer($file); try { $this->writeFileHeader()->writeTheRecords($data)->writeFileClosure(); } catch (\Exception $e) { $this->writer->close(); unlink($filepath); throw $e; } $this->writer->close(); return $filepath; }