/**
  * @param ISummit $summit
  * @param int $eb_attendee_id
  * @param int $member_id
  * @return ISummitAttendee
  */
 public function matchEventbriteAttendee(ISummit $summit, $eb_attendee_id, $member_id)
 {
     $attendee_repository = $this->attendee_repository;
     $eventbrite_attendee_repository = new SapphireEventbriteAttendeeRepository();
     $member_repository = $this->member_repository;
     return $this->tx_service->transaction(function () use($summit, $eb_attendee_id, $member_id, $attendee_repository, $eventbrite_attendee_repository, $member_repository) {
         $eb_attendee = $eventbrite_attendee_repository->getByAttendeeId($eb_attendee_id);
         if (is_null($eb_attendee)) {
             throw new NotFoundEntityException('Attendee', sprintf(' id %s', $eb_attendee_id));
         }
         $member = $member_repository->getById($member_id);
         if (is_null($member)) {
             throw new NotFoundEntityException('Member', sprintf(' id %s', $member_id));
         }
         $attendee = $attendee_repository->getByMemberAndSummit($member_id, $summit->getIdentifier());
         if (!$attendee) {
             $attendee = new SummitAttendee();
             $attendee->MemberID = $member_id;
             $attendee->SummitID = $summit->getIdentifier();
             $attendee->write();
         }
         list($eb_attendees, $count) = $eventbrite_attendee_repository->getByEmail($eb_attendee->Email);
         foreach ($eb_attendees as $eb_ticket) {
             $attendee_ticket = SummitAttendeeTicket::get()->where("ExternalAttendeeId = " . $eb_ticket->ExternalAttendeeId)->first();
             if (!$attendee_ticket) {
                 $attendee_ticket = new SummitAttendeeTicket();
                 $ticket_type = SummitTicketType::get()->where("ExternalId = " . $eb_ticket->ExternalTicketClassId)->first();
                 $external_event = EventbriteEvent::get()->where("ID = " . $eb_ticket->EventbriteOrderId)->first();
                 $attendee_ticket->ExternalOrderId = $external_event->ExternalOrderId;
                 $attendee_ticket->ExternalAttendeeId = $eb_ticket->ExternalAttendeeId;
                 $attendee_ticket->TicketTypeID = $ticket_type ? $ticket_type->ID : 0;
             }
             $attendee_ticket->OwnerID = $attendee->ID;
             $attendee_ticket->write();
         }
         return $attendee;
     });
 }
 /**
  * @return void
  */
 public function run()
 {
     $api = new EventbriteRestApi();
     $api->setCredentials(array('token' => EVENTBRITE_PERSONAL_OAUTH2_TOKEN));
     if (!isset($_GET['summit_external_id'])) {
         echo "you must provide a summit_external_id!" . PHP_EOL;
         return -1;
     }
     $repository = new SapphireSummitRepository();
     $summit = $repository->getByExternalEventId($_GET['summit_external_id']);
     if (is_null($summit)) {
         echo "summit not found!" . PHP_EOL;
         return -1;
     }
     SapphireTransactionManager::getInstance()->transaction(function () use($summit, $api) {
         $page = 1;
         $process = true;
         do {
             $response = $api->getOrdersBySummit($summit, $page);
             if (!isset($response['pagination'])) {
                 break;
             }
             if (!isset($response['orders'])) {
                 break;
             }
             $page_info = $response['pagination'];
             $page_count = $page_info['page_count'];
             $orders = $response['orders'];
             echo sprintf("processing page %s of %s", $page, $page_count) . PHP_EOL;
             foreach ($orders as $order) {
                 $uri = $order['resource_uri'];
                 $api_event = EventbriteEvent::get()->filter('ApiUrl', $uri)->first();
                 if (is_null($api_event)) {
                     $api_event = new EventbriteEvent();
                     $api_event->ApiUrl = $uri;
                     $api_event->EventType = 'ORDER_PLACED';
                     $api_event->FinalStatus = $order['status'];
                 }
                 $api_event->ExternalOrderId = $order['id'];
                 $api_event->SummitID = $summit->getIdentifier();
                 $api_event->write();
                 DB::query("DELETE FROM EventbriteAttendee WHERE EventbriteOrderID = {$api_event->ID};");
                 foreach ($order['attendees'] as $attendee) {
                     $profile = $attendee['profile'];
                     $costs = $attendee['costs'];
                     $db_attendee = new EventbriteAttendee();
                     $db_attendee->FirstName = $profile['first_name'];
                     $db_attendee->LastName = $profile['last_name'];
                     $db_attendee->Email = $profile['email'];
                     $db_attendee->EventbriteOrderID = $api_event->ID;
                     $db_attendee->Price = $costs['base_price']['major_value'];
                     $db_attendee->ExternalAttendeeId = $attendee['id'];
                     $db_attendee->ExternalTicketClassId = $attendee['ticket_class_id'];
                     $db_attendee->Status = $attendee['status'];
                     $db_attendee->write();
                 }
             }
             ++$page;
             if ($page > $page_count) {
                 $process = false;
             }
         } while ($process);
     });
 }