/**
  * add an apikey to be used in imports
  * @deprecated keys should be added through users interface
  * @param array $options
  * @return void
  */
 public function key_add(array $options)
 {
     if (isset($options[0]) && !empty($options[0]) && isset($options[1]) && !empty($options[1])) {
         // Load the key if it exists (we might already know it)
         $key = Kingboard_EveApiKey::getByUserId($options[0]);
         if (!is_null($key)) {
             $key['apikey'] = $options[1];
             $key->save();
             $this->cli->positive("key updated");
         } else {
             $key = new Kingboard_EveApiKey();
             $key['userid'] = $options[0];
             $key['apikey'] = $options[1];
             $key->save();
             $this->cli->positive("key saved");
         }
     } else {
         $this->cli->error('two parameters needed (userid, apikey)');
     }
 }
 /**
  * purge all keys from the database that have more than <amount> failcounters
  * @param array $options array(amount)
  * @return
  */
 public function key_purge(array $options)
 {
     if (!isset($options[0]) || empty($options[0])) {
         $this->cli->error('fail value required');
         return;
     }
     $criteria = array('failed' => array('$gt' => (int) $options[0]));
     $keys = Kingboard_EveApiKey::find($criteria);
     foreach ($keys as $key) {
         $this->cli->message("purging {$key['userid']}");
         $key->delete();
     }
 }
 public function api_import(array $options)
 {
     $this->cli->message("import running");
     $newkills = 0;
     $oldkills = 0;
     $errors = 0;
     $keys = Kingboard_EveApiKey::find();
     foreach ($keys as $key) {
         $pheal = new Pheal($key['userid'], $key['apikey']);
         $pheal->scope = "account";
         try {
             foreach ($pheal->Characters()->characters as $char) {
                 try {
                     $this->cli->message('trying corp import on ' . $char->name . "...");
                     $pheal->scope = 'corp';
                     $kills = $pheal->Killlog(array('characterID' => $char->characterID))->kills;
                 } catch (PhealAPIException $e) {
                     $this->cli->message('corp failed, trying char import now..');
                     $pheal->scope = 'char';
                     try {
                         $kills = $pheal->Killlog(array('characterID' => $char->characterID))->kills;
                     } catch (PhealAPIException $e) {
                         continue;
                     }
                 }
                 $this->cli->message("fetch done, parsing now");
                 $kakp = new Kingboard_ApiKillParser();
                 $info = $kakp->parseKills($kills);
                 $oldkills += $info['oldkills'];
                 $newkills += $info['newkills'];
                 $errors += $info['errors'];
             }
         } catch (PhealApiException $e) {
             if (!isset($key['failed'])) {
                 $key->failed = 0;
             }
             $key->failed++;
             $key->save();
         } catch (PhealException $pe) {
             $this->cli->message("PhealException caught, auch!");
             continue;
         }
     }
     $totalkills = $oldkills + $newkills;
     $this->cli->message("found {$totalkills} kills, {$oldkills} where allready in database, {$newkills} added ( errors: {$errors})");
 }