/** * @param string $id * @return Application */ public function getApplication($id = null) { if ($id) { self::$application = Application::model()->findByPk(new \MongoId($id)); } return self::$application; }
/** * Create default application. */ public function init() { if ($default = Application::model()->byName(self::DEFAULT_NAME)->find()) { $this->controller->data = $default; return; } $user = \User::model()->find(); $default = new Application(); $default->name = self::DEFAULT_NAME; $default->createAppSecret(); $default->owner_id = (int) $user->id; if (!$default->save()) { if ($default->hasErrors()) { throw new ApiException($default->getErrors(), 500); } else { throw new ApiException('SaveFailed', 500); } } $this->controller->data = $default; }
public function deleteApplication() { $required = array('id' => true, 'app_secret' => true); $params = $this->controller->getParams($required); /** * @var Application $application */ $application = Application::model()->findByPk(new \MongoId($params['id'])); if ($application->app_secret != $params['app_secret']) { throw new AccessDeniedApiException('InvalidAppSecret'); } if (!$application->delete()) { if ($application->hasErrors()) { throw new ApiException($application->getErrors(), 500); } else { throw new ApiException('DeleteFailed', 500); } } $this->controller->setMessage('Success'); }
private function afterAnswerCreate() { $channel = new NStreamChannel(); $channel->setName(Channels::QA); $builder = new NStreamPubBuilder(); $builder->setChannel($channel)->setType(Types::NEW_QUESTION_ANSWER)->setId($this->owner->id); $builder->send(); $apps = Application::model()->byChannel('qa_answers_channel')->findAll(); /** * @var Application $app; */ foreach ($apps as $app) { $gcm = new GcmSender($this->owner->authorId, $app->gcm_conf); $gcm->setType(Types::NEW_QUESTION_ANSWER)->setId((int) $this->owner->id)->setAppId($app->_id); $topic = new GcmTopic(); $topic->setChannel(Channels::QA_ANSWERS); $gcm->sendToTopic($topic); } if ($this->owner->question->authorId == $this->owner->authorId) { return; } $channel->setName(Channels::USER)->setId($this->owner->question->authorId); $builder->setChannel($channel); $builder->send(); foreach ($apps as $app) { $gcm = new GcmSender($this->owner->question->authorId, $app->gcm_conf); $gcm->setType(Types::NEW_QUESTION_ANSWER)->setId((int) $this->owner->question->id)->setAppId($app->_id); $topic = new GcmTopic(); $topic->setChannel(Channels::USER)->setId($this->owner->authorId); $gcm->sendToTopic($topic); } }