/**
  * Saves a app.
  *
  * @param PushNotifications_AppModel $app
  *
  * @throws \Exception
  *
  * @return bool
  */
 public function saveApp(PushNotifications_AppModel $app)
 {
     if ($app->id) {
         $appRecord = PushNotifications_AppRecord::model()->findById($app->id);
         if (!$appRecord) {
             throw new Exception(Craft::t('No app exists with the ID “{id}”', array('id' => $app->id)));
         }
     } else {
         $appRecord = new PushNotifications_AppRecord();
     }
     $appRecord->name = $app->name;
     $appRecord->handle = $app->handle;
     $appRecord->platforms = $app->platforms;
     $appRecord->commands = $app->commands;
     $appRecord->validate();
     $app->addErrors($appRecord->getErrors());
     if (!$app->hasErrors()) {
         $transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null;
         try {
             // Save it!
             $appRecord->save(false);
             // Now that we have a app ID, save it on the model
             if (!$app->id) {
                 $app->id = $appRecord->id;
             }
             // Might as well update our cache of the app while we have it.
             $this->_appsById[$app->id] = $app;
             if ($transaction !== null) {
                 $transaction->commit();
             }
         } catch (\Exception $e) {
             if ($transaction !== null) {
                 $transaction->rollback();
             }
             throw $e;
         }
         return true;
     } else {
         return false;
     }
 }