<?php
/**
 * @copyright  2013 Roberto Perpuly
 *
 * This source may not be redistributed without prior consent.  Access to the 
 * source does not imply that you have consent to redistribute either this source
 * code or the generated binary. See LICENSE file for details.
 */


class Triumph_SourceTable extends Zend_Db_Table_Abstract {

	protected $_name = 'sources';
	

	/**
	 * @return the primary key of the source directory
	 */
	public function getOrSave($sourceDir) {
		
		/// make sure that sourceDir ends with the separator to make sure
		// only the correct entries are selected / saved
		$sourceDir = \opstring\ensure_ends_with($sourceDir, DIRECTORY_SEPARATOR);
		
		$select = $this->select()->where('directory = ?', $sourceDir);
		$stmt = $select->query(Zend_Db::FETCH_ASSOC);
		if ($row = $stmt->fetch()) {
			return $row['source_id'];
		}
		return $this->insert(array(
			'directory' => $sourceDir
		));
		
	}

}
