public function parseKills($kills)
 {
     $oldkills = 0;
     $newkills = 0;
     $errors = 0;
     $lastID = 0;
     $lastIntID = 0;
     foreach ($kills as $kill) {
         try {
             // this needs to be run before exit of loop, otherwise having all kills of this run
             // will cause the lastID not being updated
             if (!is_null($kill->killID) && $kill->killID > 0) {
                 $lastID = $kill->killID;
             }
             if (!is_null(@$kill->killInternalID) && @$kill->killInternalID > 0) {
                 $lastIntID = $kill->killInternalID;
             }
             /*if(!is_null(Kingboard_Kill::getByKillId($kill->killID)))
               {
                   $oldkills++;
                   continue;
               }*/
             $killdata = array("killID" => $kill->killID, "solarSystemID" => $kill->solarSystemID, "location" => array("solarSystem" => Kingboard_EveSolarSystem::getBySolarSystemId($kill->solarSystemID)->itemName, "security" => Kingboard_EveSolarSystem::getBySolarSystemId($kill->solarSystemID)->security, "region" => Kingboard_EveSolarSystem::getBySolarSystemId($kill->solarSystemID)->Region['itemName']), "killTime" => new MongoDate(strtotime($kill->killTime)), "moonID" => $kill->moonID, "victim" => array("characterID" => $this->ensureCharacterID($kill->victim->characterID, $kill->victim->characterName), "characterName" => $kill->victim->characterName, "corporationID" => $this->ensureCorporationID($kill->victim->corporationID, $kill->victim->corporationName), "corporationName" => $kill->victim->corporationName, "allianceID" => (int) $kill->victim->allianceID, "allianceName" => $kill->victim->allianceName, "factionID" => (int) $kill->victim->factionID, "factionName" => $kill->victim->factionName, "damageTaken" => $kill->victim->damageTaken, "shipTypeID" => (int) $kill->victim->shipTypeID, "shipType" => Kingboard_EveItem::getByItemId($kill->victim->shipTypeID)->typeName));
             $killdata['attackers'] = array();
             foreach ($kill->attackers as $attacker) {
                 $killdata['attackers'][] = array("characterID" => $attacker->characterID, "characterName" => $attacker->characterName, "entityType" => Kingboard_Helper_EntityType::getEntityTypeByEntityId((int) $attacker->characterID), "corporationID" => $this->ensureCorporationID($attacker->corporationID, $attacker->corporationName), "corporationName" => $attacker->corporationName, "allianceID" => (int) $attacker->allianceID, "allianceName" => $attacker->allianceName, "factionID" => (int) $attacker->factionID, "factionName" => $attacker->factionName, "securityStatus" => $attacker->securityStatus, "damageDone" => $attacker->damageDone, "finalBlow" => $attacker->finalBlow, "weaponTypeID" => (int) $attacker->weaponTypeID, "weaponType" => Kingboard_EveItem::getByItemId($attacker->weaponTypeID)->typeName, "shipTypeID" => (int) $attacker->shipTypeID, "shipType" => Kingboard_EveItem::getByItemId($attacker->shipTypeID)->typeName);
             }
             $killdata['items'] = array();
             if (isset($kill->items) && !is_null($kill->items)) {
                 foreach ($kill->items as $item) {
                     $killdata['items'][] = $this->ParseItem($item);
                 }
             }
             $hash = Kingboard_KillmailHash_IdHash::getByData($killdata);
             $killdata['idHash'] = (string) $hash;
             if (is_null(Kingboard_Kill::getInstanceByIdHash($killdata['idHash']))) {
                 $killObject = new Kingboard_Kill();
                 $killObject->injectDataFromMail($killdata);
                 $killObject->save();
                 $newkills++;
             } else {
                 $oldkills++;
             }
         } catch (Kingboard_KillmailParser_KillmailErrorException $e) {
             $errors++;
         }
     }
     return array('oldkills' => $oldkills, 'newkills' => $newkills, 'lastID' => $lastID, 'lastIntID' => $lastIntID, 'errors' => $errors);
 }
 /**
  * Parse the different parts of a killmail into
  * a multidimensional array which can be further processed
  *
  * @param string $mail
  */
 public function parse($mail)
 {
     $this->plainMail = $mail;
     $victimActive = true;
     $involved = false;
     $destroyed = false;
     $dropped = false;
     $tokens = Kingboard_KillmailParser_Factory::findTokensForMail($mail);
     $currentAttacker = -1;
     // Clean the mail and explode by line
     $lines = explode(chr(10), $mail);
     $ids = new Kingboard_KillmailParser_IdFinder();
     $lastMainItem = null;
     reset($lines);
     while (count($lines) > 0) {
         $plainLine = array_shift($lines);
         try {
             // If it has a name token and a slash, we presume the contents
             // after the slash is the corporation name
             // This is done for NPC names and their corps, and a slash
             // is no valid item name, yet is valid in a killmail
             $strTools = Kingboard_Helper_String::getInstance();
             if ($strTools->stripos($tokens->name(), $plainLine) !== false && $strTools->strpos('/', $plainLine) !== false) {
                 $parts = explode('/', $plainLine);
                 $plainLine = array_shift($parts);
                 $corp = $tokens->corp() . implode('/', $parts);
                 // If the final blow token is present, move it from the corp name to attacker name
                 if ($strTools->stripos($tokens->finalBlow(), $corp)) {
                     $plainLine .= ' ' . $tokens->finalBlow();
                     $corp = str_replace($tokens->finalBlow(), '', $corp);
                 }
                 array_unshift($lines, $corp);
                 reset($lines);
                 unset($parts, $corp);
             }
             $line = new Kingboard_KillmailParser_Line($plainLine, $tokens);
             switch ($line->getType()) {
                 case Kingboard_KillmailParser_Line::TYPE_TIME:
                     $this->killTime = $line->getValue();
                     break;
                 case Kingboard_KillmailParser_Line::TYPE_NAME:
                     if ($involved) {
                         $currentAttacker++;
                         $this->attackers[$currentAttacker] = array('characterName' => '', 'characterID' => 0, 'typeID' => 0, 'corporationName' => '', 'corporationID' => 0, 'allianceName' => '', 'allianceID' => 0, 'shipType' => '', 'shipTypeID' => 0, 'factionName' => '', 'factionID' => 0, 'weaponType' => '', 'weaponTypeID' => 0, 'finalBlow' => false, 'securityStatus' => 0.0);
                         $this->attackers[$currentAttacker]['finalBlow'] = $line->hasFinalBlow();
                         $this->attackers[$currentAttacker]['characterName'] = $line->getValue();
                         $this->attackers[$currentAttacker]['characterID'] = $ids->getCharacterId($line->getValue());
                         $this->attackers[$currentAttacker]['entityType'] = Kingboard_Helper_EntityType::getEntityTypeByEntityId($this->attackers[$currentAttacker]['characterID']);
                         if ($this->attackers[$currentAttacker]['entityType'] == Kingboard_Helper_EntityType::ENTITY_NPC) {
                             $this->attackers[$currentAttacker]['shipTypeID'] = $this->attackers[$currentAttacker]['characterID'];
                             $this->attackers[$currentAttacker]['shipType'] = $this->attackers[$currentAttacker]['characterName'];
                             $this->attackers[$currentAttacker]['characterID'] = 0;
                             $this->attackers[$currentAttacker]['characterName'] = '';
                         }
                     } else {
                         $this->victim['characterName'] = $line->getValue();
                         $this->victim['characterID'] = $ids->getCharacterId($line->getValue());
                     }
                     break;
                 case Kingboard_KillmailParser_Line::TYPE_ALLIANCE:
                     if ($victimActive) {
                         $record =& $this->victim;
                     } elseif ($involved) {
                         $record =& $this->attackers[$currentAttacker];
                     }
                     if (!$line->isEmpty()) {
                         $record['allianceName'] = $line->getValue();
                         $record['allianceID'] = $ids->getAllianceId($line->getValue());
                     }
                     unset($record);
                     break;
                 case Kingboard_KillmailParser_Line::TYPE_CORP:
                     if ($victimActive) {
                         $record =& $this->victim;
                     } elseif ($involved) {
                         $record =& $this->attackers[$currentAttacker];
                     }
                     if (!$line->isEmpty()) {
                         $record['corporationID'] = $ids->getCorporationId($line->getValue());
                         $record['corporationName'] = $line->getValue();
                     }
                     unset($record);
                     break;
                 case Kingboard_KillmailParser_Line::TYPE_DAMAGE:
                     if ($victimActive) {
                         $this->victim['damageTaken'] = $line->getValue();
                     } elseif ($involved) {
                         $this->attackers[$currentAttacker]['damageDone'] = $line->getValue();
                     }
                     break;
                 case Kingboard_KillmailParser_Line::TYPE_FACTION:
                     if ($victimActive) {
                         $record =& $this->victim;
                     } elseif ($involved) {
                         $record =& $this->attackers[$currentAttacker];
                     }
                     if (!$line->isEmpty()) {
                         $record['factionID'] = $ids->getFactionId($line->getValue());
                         $record['factionName'] = $line->getValue();
                     }
                     unset($record);
                     break;
                 case Kingboard_KillmailParser_Line::TYPE_ITEM:
                     $flag = 0;
                     if ($line->isDrone()) {
                         $flag = 87;
                     } elseif ($line->isCargo()) {
                         $flag = 5;
                     }
                     $item = array('typeID' => $ids->getItemId($line->getValue()), 'typeName' => $line->getValue(), 'qtyDropped' => $dropped ? $line->getQty() : 0, 'qtyDestroyed' => !$dropped ? $line->getQty() : 0, 'flag' => $flag);
                     if ($line->inContainer()) {
                         if (!isset($this->items[$lastMainItem])) {
                             throw new Kingboard_KillmailParser_KillmailErrorException('Container for item not found');
                         }
                         if (!isset($this->items[$lastMainItem]['items'])) {
                             $this->items[$lastMainItem]['items'] = array();
                         }
                         $this->items[$lastMainItem]['items'][] = $item;
                     } else {
                         $lastMainItem = count($this->items);
                         $this->items[] = $item;
                     }
                     unset($item, $flag);
                     break;
                 case Kingboard_KillmailParser_Line::TYPE_SECURITY:
                     $value = (double) $line->getValue();
                     if ($victimActive) {
                         $this->location['security'] = $value;
                     } elseif ($involved) {
                         $this->attackers[$currentAttacker]['securityStatus'] = $value;
                     }
                     unset($value);
                     break;
                 case Kingboard_KillmailParser_Line::TYPE_SHIP:
                     if ($victimActive) {
                         $record =& $this->victim;
                     } elseif ($involved) {
                         $record =& $this->attackers[$currentAttacker];
                     }
                     if (!$line->isEmpty()) {
                         $record['shipType'] = $line->getValue();
                         $record['shipTypeID'] = $ids->getItemId($line->getValue());
                     }
                     unset($record);
                     break;
                 case Kingboard_KillmailParser_Line::TYPE_SWITCH_ATTACKERS:
                     $victimActive = false;
                     $involved = true;
                     break;
                 case Kingboard_KillmailParser_Line::TYPE_SWITCH_DROPPED:
                     $dropped = true;
                     break;
                 case Kingboard_KillmailParser_Line::TYPE_SYSTEM:
                     $this->location['solarSystemID'] = $ids->getSolarSystemId($line->getValue());
                     $this->location['solarSystemName'] = $line->getValue();
                     break;
                 case Kingboard_KillmailParser_Line::TYPE_MOON:
                     if (!$line->isEmpty()) {
                         $system = Kingboard_EveSolarSystem::getInstanceByCriteria(array('Moons.itemName' => $line->getValue()));
                         if ($system) {
                             $moons = $system->Moons;
                             foreach ($moons as $moon) {
                                 if ($moon['itemName'] == $line->getValue()) {
                                     $this->location['moon'] = $line->getValue();
                                     $this->location['moonID'] = $moon['itemID'];
                                     break;
                                 }
                             }
                         }
                     }
                     break;
                 case Kingboard_KillmailParser_Line::TYPE_WEAPON:
                     $this->attackers[$currentAttacker]['weaponTypeID'] = $ids->getItemId($line->getValue());
                     $this->attackers[$currentAttacker]['weaponType'] = $line->getValue();
                     break;
             }
         } catch (Kingboard_KillmailParser_KillmailErrorException $e) {
             $this->errors[] = $e->getMessage();
             continue;
         }
         // Save errors for log / output
         if ($line->hasError()) {
             $this->errors[] = $line->getError();
         }
     }
     // Now we need to clean up the items array
     $this->items = $this->cleanItemsArray($this->items);
     return $this;
 }
 /**
  *
  * @dataProvider entityIdsProvider
  */
 public function testGetEntityTypeByEntityId($id, $expected)
 {
     $actual = Kingboard_Helper_EntityType::getEntityTypeByEntityId($id);
     $this->assertEquals($expected, $actual, 'Failed asserting that ID ' . $id . ' is of type ' . $expected . ', type ' . $actual . ' detected');
 }