/**
  * Save harvested records to disk and return the end date.
  *
  * @param object $records SimpleXML records.
  *
  * @return int
  */
 public function write($records)
 {
     // Array for tracking successfully harvested IDs:
     $harvestedIds = [];
     // Date of most recent record encountered:
     $endDate = 0;
     $this->strategy->beginWrite();
     // Loop through the records:
     foreach ($records as $record) {
         // Die if the record is missing its header:
         if (empty($record->header)) {
             throw new \Exception('Unexpected missing record header.');
         }
         // Get the ID of the current record:
         $id = $this->extractID($record);
         // Save the current record, either as a deleted or as a regular file:
         $attribs = $record->header->attributes();
         if (strtolower($attribs['status']) == 'deleted') {
             $this->strategy->addDeletedRecord($id);
         } else {
             $recordXML = $this->recordFormatter->format($id, $record);
             $this->strategy->addRecord($id, $recordXML);
             $harvestedIds[] = $id;
         }
         // If the current record's date is newer than the previous end date,
         // remember it for future reference:
         $date = $this->normalizeDate($record->header->datestamp);
         if ($date && $date > $endDate) {
             $endDate = $date;
         }
     }
     $this->strategy->endWrite();
     $this->writeHarvestedIdsLog($harvestedIds);
     return $endDate;
 }
 /**
  * Test exception when metadata is missing.
  *
  * @return void
  *
  * @expectedException        Exception
  * @expectedExceptionMessage Unexpected missing record metadata.
  */
 public function testMissingMetadataException()
 {
     $formatter = new RecordXmlFormatter();
     $formatter->format('foo', simplexml_load_string('<empty />'));
 }