Created
January 23, 2015 17:52
-
-
Save maxenglander/d7a887e78e54cc6ebc14 to your computer and use it in GitHub Desktop.
Code Sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Dal_Content extends Dal_Abstract { | |
const COLLECTION_NAME = 'content'; | |
private $dal_key_content; | |
/** | |
* @param Dal_Key_Content $dal_key_content | |
* @param mixed $db | |
*/ | |
public function __construct(Dal_Key_Content $dal_key_content, $db=self::DEFAULT_DB_CLASS) { | |
parent::__construct($db, true); | |
$this->dal_key_content = $dal_key_content; | |
} | |
private function createQueryBySid($client_id, $sid) { | |
return array('client_id' => $client_id, '_id' => new MongoId($sid)); | |
} | |
private function createQueryByUrl($client_id, $url) { | |
return array('client_id' => $client_id, 'url' => $url); | |
} | |
public function getCollectionName() { | |
return self::COLLECTION_NAME; | |
} | |
public function getDalKeyContent() { | |
return $this->dal_key_content; | |
} | |
/** | |
* @param int $client_id | |
* @param string $sid | |
* @param array $options | |
* @return array|null The matching content document, or null if not found | |
*/ | |
public function findOneBySid($client_id, $sid, array $options = array()) { | |
Util_Validate::clientId($client_id); | |
Util_Validate::mongoIdString($sid); | |
return $this->findOne(self::createQueryBySid($client_id, $sid), $options); | |
} | |
/** | |
* @param int $client_id | |
* @param string $url | |
* @param array $options | |
* @return array|null The matching content document, or null if not found | |
*/ | |
public function findOneByUrl($client_id, $url, array $options = array()) { | |
Util_Validate::clientId($client_id); | |
$clean_url = Util_Validate::cleanAndValidateUrl($url); | |
$sid = $this->dal_key_content->findSidByUrl($client_id, $clean_url); | |
if(!$sid) { | |
return null; | |
} | |
return $this->findOneBySid($client_id, $sid, $options); | |
} | |
/** | |
* @param int $client_id | |
* @param string $sid | |
* @param array $update | |
* @param array $options | |
* | |
* @deprecated since version 1.1 | |
*/ | |
public function updateBySid($client_id, $sid, array $update, array $options = array()) { | |
Util_Validate::clientId($client_id); | |
Util_Validate::mongoIdString($sid); | |
$this->update(self::createQueryBySid($client_id, $sid), $update, $options); | |
} | |
/** | |
* @param int $client_id | |
* @param array $query | |
* @param array $update | |
* @param array $options | |
* | |
* @deprecated since version 1.1 | |
*/ | |
public function updateByUrl($client_id, $url, array $update, array $options = array()) { | |
Util_Validate::clientId($client_id); | |
$clean_url = Util_Validate::cleanAndValidateUrl($url); | |
$sid = $this->dal_key_content->findSidByUrl($client_id, $clean_url); | |
if($sid) { | |
$this->update(self::createQueryBySid($client_id, $sid), $update, $options); | |
} | |
} | |
/** | |
* @param int $client_id | |
* @param string $sid | |
* @param array $update | |
* @param array $options | |
*/ | |
private function upsertBySid($client_id, $sid, array $update, array $options = array()) { | |
Util_Validate::clientId($client_id); | |
Util_Validate::mongoIdString($sid); | |
$this->upsert(self::createQueryBySid($client_id, $sid), $update, $options); | |
} | |
/** | |
* @param int $client_id | |
* @param string $url | |
* @param array $update | |
* @param array $options | |
*/ | |
public function upsertByUrl($client_id, $url, array $update, array $options = array()) { | |
Util_Validate::clientId($client_id); | |
$clean_url = Util_Validate::cleanAndValidateUrl($url); | |
$sid = $this->dal_key_content->findOrCreateSidByKeyAtomic($client_id, 'url', $clean_url); | |
$update['$setOnInsert']['url'] = $clean_url; | |
$this->upsertBySid($client_id, "$sid", $update, $options); | |
return "$sid"; | |
} | |
private function removeContentAndKeys($client_id, array $content, $reason, array $options = array()) { | |
$this->removeOne(self::createQueryBySid($client_id, (string)$content['_id']), $reason, $options); | |
if(isset($content['url'])) { | |
$this->dal_key_content->removeOneByUrl($client_id, $content['url'], $reason, $options); | |
} | |
} | |
/** | |
* @param MongoId $mongo_id | |
* @param string $reason | |
* @param array $options | |
*/ | |
public function removeOneByMongoId(MongoId $mongo_id, $reason, array $options = array()){ | |
$this->removeOne(array('_id' => $mongo_id), $reason, $options); | |
$this->dal_key_content->removeBySid($mongo_id, $reason, $options); | |
} | |
/** | |
* @param int $client_id | |
* @param string $sid | |
* @param string $reason | |
* @param array $options | |
*/ | |
public function removeOneBySid($client_id, $sid, $reason, array $options = array()) { | |
Util_Validate::clientId($client_id); | |
Util_Validate::mongoIdString($sid); | |
$content = $this->findOneBySid($client_id, $sid); | |
if(!$content) { | |
return; | |
} | |
$this->removeContentAndKeys($client_id, $content, $reason, $options); | |
} | |
/** | |
* @param int $client_id | |
* @param string $url | |
* @param string $reason | |
* @param array $options | |
*/ | |
public function removeOneByUrl($client_id, $url, $reason, array $options = array()) { | |
Util_Validate::clientId($client_id); | |
$clean_url = Util_Validate::cleanAndValidateUrl($url); | |
$content = $this->findOneByUrl($client_id, $url); | |
if(!$content) { | |
return; | |
} | |
$this->removeContentAndKeys($client_id, $content, $reason, $options); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment