refactored addWatchedDir to use early return style

This commit is contained in:
Rudi Grinberg 2012-11-03 01:16:57 -04:00
parent 5d3aa68c72
commit 9c4f2e3298
1 changed files with 31 additions and 32 deletions

View File

@ -246,50 +246,49 @@ SQL;
{ {
$res = self::addDir($p_path, "watched", $userAddedWatchedDir, $nestedWatch); $res = self::addDir($p_path, "watched", $userAddedWatchedDir, $nestedWatch);
if ($res['code'] == 0) { if ($res['code'] != 0) { return $res; }
//convert "linked" files (Airtime <= 1.8.2) to watched files. //convert "linked" files (Airtime <= 1.8.2) to watched files.
$propel_link_dir = CcMusicDirsQuery::create() $propel_link_dir = CcMusicDirsQuery::create()
->filterByType('link') ->filterByType('link')
->findOne();
//see if any linked files exist.
if (isset($propel_link_dir)) {
//newly added watched directory object
$propel_new_watch = CcMusicDirsQuery::create()
->filterByDirectory(Application_Common_OsPath::normpath($p_path)."/")
->findOne(); ->findOne();
//see if any linked files exist. //any files of the deprecated "link" type.
if (isset($propel_link_dir)) { $link_files = CcFilesQuery::create()
->setFormatter(ModelCriteria::FORMAT_ON_DEMAND)
->filterByDbDirectory($propel_link_dir->getId())
->find();
//newly added watched directory object $newly_watched_dir = $propel_new_watch->getDirectory();
$propel_new_watch = CcMusicDirsQuery::create()
->filterByDirectory(Application_Common_OsPath::normpath($p_path)."/")
->findOne();
//any files of the deprecated "link" type. foreach ($link_files as $link_file) {
$link_files = CcFilesQuery::create() $link_filepath = $link_file->getDbFilepath();
->setFormatter(ModelCriteria::FORMAT_ON_DEMAND)
->filterByDbDirectory($propel_link_dir->getId())
->find();
$newly_watched_dir = $propel_new_watch->getDirectory(); //convert "link" file into a watched file.
if ((strlen($newly_watched_dir) < strlen($link_filepath)) && (substr($link_filepath, 0, strlen($newly_watched_dir)) === $newly_watched_dir)) {
foreach ($link_files as $link_file) { //get the filepath path not including the watched directory.
$link_filepath = $link_file->getDbFilepath(); $sub_link_filepath = substr($link_filepath, strlen($newly_watched_dir));
//convert "link" file into a watched file. $link_file->setDbDirectory($propel_new_watch->getId());
if ((strlen($newly_watched_dir) < strlen($link_filepath)) && (substr($link_filepath, 0, strlen($newly_watched_dir)) === $newly_watched_dir)) { $link_file->setDbFilepath($sub_link_filepath);
$link_file->save();
//get the filepath path not including the watched directory.
$sub_link_filepath = substr($link_filepath, strlen($newly_watched_dir));
$link_file->setDbDirectory($propel_new_watch->getId());
$link_file->setDbFilepath($sub_link_filepath);
$link_file->save();
}
} }
} }
$data = array();
$data["directory"] = $p_path;
Application_Model_RabbitMq::SendMessageToMediaMonitor("new_watch", $data);
} }
$data = array();
$data["directory"] = $p_path;
Application_Model_RabbitMq::SendMessageToMediaMonitor("new_watch", $data);
return $res; return $res;
} }