public function saveAsFile($diagram, $savePath) {
    if(is_dir($savePath)) {
      $filePath = $savePath.'/'.$this->cleanFileName($diagram->id).'.bpmn';
    } else {
      $filePath = './'.$this->cleanFileName($diagram->id).'.bpmn';
    }

    $handle = fopen($filePath, 'c+');
    ftruncate($handle, filesize($filePath));
    fwrite($handle, $diagram->bpmn20Xml);
    fclose($handle);

    return $filePath;
  }