private function process_notification()
 {
     //  Create notification item
     //
     $payload = Payload::PayloadJSON(array('alert' => 'process_notification() notification.', 'sound' => 'default'), array('metadata' => 'extra stuff'));
     $notificationItem = new APNsNotificationItem('38592f807b0e8d0dc284b6e9711c61504d04a988af9b626d83c56f6704904c4a', $payload, 'notificationidentifier', null, null);
     //  Create notification
     //
     $notification = new APNsNotification(APNS_NOTIFICATION_COMMAND_PUSH, APNS_NOTIFICATION_FRAME_LENGTH, array($notificationItem));
     //  Send notification
     //
     $this->APNsManager->sendNotification($notification);
 }
 public static function attend(Inputter $inputter, JSONOutputter $outputter)
 {
     //	List Created
     //
     //  Constants
     //
     //  Verify input
     //
     $required_keys = array('attending_user_identification' => '', 'event_identification' => '', 'attending_status' => '');
     $inputter->validate_input($required_keys, null);
     //  Validate input
     //
     $validate_error = null;
     UniversallyUniqueIdentifier::propertyIsValid('rawIdentifier', $inputter->variables_array['attending_user_identification'], $validate_error);
     UniversallyUniqueIdentifier::propertyIsValid('rawIdentifier', $inputter->variables_array['event_identification'], $validate_error);
     if (isset($validate_error)) {
         $outputter->print_error($validate_error);
     }
     //      Attending status
     //
     $inputter->variables_array['attending_status'] = intval($inputter->variables_array['attending_status']);
     if ($inputter->variables_array['attending_status'] < 0 || $inputter->variables_array['attending_status'] > 2) {
         // Throw error, status is out of bounds
         //
         $error = Error::withDomain(PRIVATE_EVENTS_REST_CONTROLLER_ERROR_DOMAIN, PRIVATE_EVENTS_REST_CONTROLLER_ERROR_CODE_ENTITY_DOES_NOT_EXIST, $attending_status . ' is out of bounds.');
         $outputter->print_error($error);
     }
     //		Identification should exist
     //
     $client = new Everyman\Neo4j\Client('events.sb04.stations.graphenedb.com', 24789);
     $client->getTransport()->setAuth('Events', '3TP9LHROhv8LIcGmbYzq');
     $query_string_check_object = 'MATCH (object:AttendingUser) ' . 'WHERE object.' . UNIVERSALLY_UNIQUE_OBJECT_KEY_IDENTIFICATION . " = '" . $inputter->variables_array['attending_user_identification'] . "' " . 'RETURN object';
     $query_check_object = new Everyman\Neo4j\Cypher\Query($client, $query_string_check_object);
     $result_check_object = $query_check_object->getResultSet();
     if (!isset($result_check_object[0]['object'])) {
         //	No user exists
         //
         $outputter->print_error(Error::withDomain(PRIVATE_EVENTS_REST_CONTROLLER_ERROR_DOMAIN, PRIVATE_EVENTS_REST_CONTROLLER_ERROR_CODE_ENTITY_DOES_NOT_EXIST, $attending_user_identification . ' should be an existing user identification.'));
     }
     //		Identification should exist
     //
     $client = new Everyman\Neo4j\Client('events.sb04.stations.graphenedb.com', 24789);
     $client->getTransport()->setAuth('Events', '3TP9LHROhv8LIcGmbYzq');
     $query_string_check_object = 'MATCH (object:Event) ' . 'WHERE object.' . UNIVERSALLY_UNIQUE_OBJECT_KEY_IDENTIFICATION . " = '" . $inputter->variables_array['event_identification'] . "' " . 'RETURN object';
     $query_check_object = new Everyman\Neo4j\Cypher\Query($client, $query_string_check_object);
     $result_check_object = $query_check_object->getResultSet();
     if (!isset($result_check_object[0]['object'])) {
         //	No user exists
         //
         $outputter->print_error(Error::withDomain(PRIVATE_EVENTS_REST_CONTROLLER_ERROR_DOMAIN, PRIVATE_EVENTS_REST_CONTROLLER_ERROR_CODE_ENTITY_DOES_NOT_EXIST, $event_identification . ' should be an existing event identification.'));
     }
     //		Attending user's cannot attend their own events
     //
     $client = new Everyman\Neo4j\Client('events.sb04.stations.graphenedb.com', 24789);
     $client->getTransport()->setAuth('Events', '3TP9LHROhv8LIcGmbYzq');
     $query_string_check_object = 'MATCH (creator:CreatingUser)-[relationship:' . CREATINGUSER_RELATIONSHIP_NAME_EVENT . ']->(event:Event) ' . 'WHERE creator.' . UNIVERSALLY_UNIQUE_OBJECT_KEY_IDENTIFICATION . " = '" . $inputter->variables_array['attending_user_identification'] . "' AND event." . UNIVERSALLY_UNIQUE_OBJECT_KEY_IDENTIFICATION . " = '" . $inputter->variables_array['event_identification'] . "' " . 'RETURN relationship';
     $query_check_object = new Everyman\Neo4j\Cypher\Query($client, $query_string_check_object);
     $result_check_object = $query_check_object->getResultSet();
     if (isset($result_check_object[0]) && isset($result_check_object[0]['relationship'])) {
         //	Throw error, cannot attend own event
         //
         $outputter->print_error(Error::withDomain(PRIVATE_EVENTS_REST_CONTROLLER_ERROR_DOMAIN, PRIVATE_EVENTS_REST_CONTROLLER_ERROR_CODE_ENTITY_DOES_NOT_EXIST, 'Cannot attend own event.'));
     }
     //	Events
     //
     $query_string = 'MATCH (attendingUser:AttendingUser), (event:Event)-[:' . EVENT_RELATIONSHIP_NAME_CREATOR . ']->(creatingUser:CreatingUser) ' . 'WHERE attendingUser.' . UNIVERSALLY_UNIQUE_OBJECT_KEY_IDENTIFICATION . " = '" . $inputter->variables_array['attending_user_identification'] . "' AND event." . UNIVERSALLY_UNIQUE_OBJECT_KEY_IDENTIFICATION . " = '" . $inputter->variables_array['event_identification'] . "' " . 'RETURN attendingUser, event, creatingUser';
     $query = new Everyman\Neo4j\Cypher\Query($client, $query_string);
     $result = $query->getResultSet();
     if ($result->count() <= 0) {
         $outputter->print_error(Error::withDomain(PRIVATE_EVENTS_REST_CONTROLLER_ERROR_DOMAIN, PRIVATE_EVENTS_REST_CONTROLLER_ERROR_CODE_ENTITY_DOES_NOT_EXIST, 'No results returned.'));
     }
     $attendingUser = $result[0]['attendingUser'];
     $event = $result[0]['event'];
     $creatingUser = $result[0]['creatingUser'];
     //  Get relationship
     //
     $query_string = 'OPTIONAL MATCH (attendingUser:AttendingUser)-[relationship:' . ATTENDINGUSER_RELATIONSHIP_NAME_RSVP . ']->(event:Event) ' . 'WHERE attendingUser.' . UNIVERSALLY_UNIQUE_OBJECT_KEY_IDENTIFICATION . " = '" . $inputter->variables_array['attending_user_identification'] . "' AND event." . UNIVERSALLY_UNIQUE_OBJECT_KEY_IDENTIFICATION . " = '" . $inputter->variables_array['event_identification'] . "' " . 'RETURN relationship';
     $query = new Everyman\Neo4j\Cypher\Query($client, $query_string);
     $result = $query->getResultSet();
     if ($result->count() <= 0) {
         $outputter->print_error(Error::withDomain(PRIVATE_EVENTS_REST_CONTROLLER_ERROR_DOMAIN, PRIVATE_EVENTS_REST_CONTROLLER_ERROR_CODE_ENTITY_DOES_NOT_EXIST, 'No results returned.'));
     }
     $relationship = $result[0]['relationship'];
     if (!isset($relationship)) {
         //  Create relationship as it's not created yet
         //
         $relationship = $client->makeRelationship()->setStartNode($attendingUser)->setEndNode($event)->setType(ATTENDINGUSER_RELATIONSHIP_NAME_RSVP)->setProperty(ATTENDINGUSER_RELATIONSHIP_NAME_RSVP_PROPERTY_NAME_ATTENDING_STATUS, $inputter->variables_array['attending_status'])->save();
     }
     if (!isset($relationship)) {
         $error = Error::withDomain(PRIVATE_EVENTS_REST_CONTROLLER_ERROR_DOMAIN, PRIVATE_EVENTS_REST_CONTROLLER_ERROR_CODE_ENTITY_DOES_NOT_EXIST, 'Problem attending event');
         $outputter->print_error($error);
     } else {
         $relationship->setProperty(ATTENDINGUSER_RELATIONSHIP_NAME_RSVP_PROPERTY_NAME_ATTENDING_STATUS, $inputter->variables_array['attending_status'])->save();
     }
     //   4) Send notifications
     //
     if ($inputter->variables_array['attending_status'] == ATTENDINGUSER_RELATIONSHIP_NAME_RSVP_PROPERTY_VALUE_ATTENDING_STATUS_ATTENDING) {
         $name = $attendingUser->getProperty(CREATINGUSER_KEY_FIRST_NAME);
         if (!isset($name)) {
             $name = $attendingUser->getProperty(CREATINGUSER_KEY_NAME);
         }
         $identifiers = $creatingUser->getProperty(USER_KEY_NOTIFICATION_DEVICE_IDENTIFIERS);
         if (isset($identifiers) && is_array($identifiers) && count($identifiers) > 0) {
             foreach ($identifiers as $identifier) {
                 //  Create notification item
                 //
                 $payload = Payload::PayloadJSON(array('alert' => $name . " is attending you're event.", 'sound' => 'default'), array());
                 $notificationItem = new APNsNotificationItem($identifier, $payload, 'notificationIdentifier', null, null);
                 //  Create notification
                 //
                 $notification = new APNsNotification(APNS_NOTIFICATION_COMMAND_PUSH, APNS_NOTIFICATION_FRAME_LENGTH, array($notificationItem));
                 //  Send notification
                 //
                 $inputter->notificationController->APNsManager->sendNotification($notification);
             }
         }
     }
     //  5) Print data
     //
     $outputter->print_data(array(array('attending_status' => $inputter->variables_array['attending_status'])));
 }
 public static function create(Inputter $inputter, JSONOutputter $outputter)
 {
     //	Create
     //
     //	1) Test all data is available and valid
     //
     //		Availability
     //
     $required_dictionary = array('user_identification' => '', 'user_to_follow_identification' => '');
     $inputter->validate_input($required_dictionary, null);
     //		Valid
     //
     //			Identifiers
     //
     $validation_error = null;
     UniversallyUniqueObject::propertyIsValid(UNIVERSALLY_UNIQUE_OBJECT_KEY_IDENTIFICATION, $inputter->variables_array['user_identification'], $validation_error);
     UniversallyUniqueObject::propertyIsValid(UNIVERSALLY_UNIQUE_OBJECT_KEY_IDENTIFICATION, $inputter->variables_array['user_to_follow_identification'], $validation_error);
     if (isset($validation_error)) {
         $outputter->print_error($validation_error);
     }
     //          Different
     //
     if ($inputter->variables_array['user_identification'] === $inputter->variables_array['user_to_follow_identification']) {
         $outputter->print_error(Error::withDomain(PRIVATE_EVENTS_REST_CONTROLLER_ERROR_DOMAIN, PRIVATE_EVENTS_REST_CONTROLLER_ERROR_CODE_INPUT_NOT_UNIQUE, 'Users cannot follow themselves.'));
     }
     //	2) Check request we want to process CAN go ahead
     //
     //		Identification should exist
     //
     $client = new Everyman\Neo4j\Client('events.sb04.stations.graphenedb.com', 24789);
     $client->getTransport()->setAuth('Events', '3TP9LHROhv8LIcGmbYzq');
     $query_string_check_object = 'MATCH (object:AttendingUser) ' . 'WHERE object.' . UNIVERSALLY_UNIQUE_OBJECT_KEY_IDENTIFICATION . " = '" . $inputter->variables_array['user_identification'] . "' " . 'RETURN object';
     $query_check_object = new Everyman\Neo4j\Cypher\Query($client, $query_string_check_object);
     $result_check_object = $query_check_object->getResultSet();
     if (!isset($result_check_object[0]['object'])) {
         //	No user exists
         //
         $outputter->print_error(Error::withDomain(PRIVATE_EVENTS_REST_CONTROLLER_ERROR_DOMAIN, PRIVATE_EVENTS_REST_CONTROLLER_ERROR_CODE_ENTITY_DOES_NOT_EXIST, 'user_identification should be an existing user identification.'));
     }
     $client = new Everyman\Neo4j\Client('events.sb04.stations.graphenedb.com', 24789);
     $client->getTransport()->setAuth('Events', '3TP9LHROhv8LIcGmbYzq');
     $query_string_check_object = 'MATCH (object:CreatingUser) ' . 'WHERE object.' . UNIVERSALLY_UNIQUE_OBJECT_KEY_IDENTIFICATION . " = '" . $inputter->variables_array['user_to_follow_identification'] . "' " . 'RETURN object';
     $query_check_object = new Everyman\Neo4j\Cypher\Query($client, $query_string_check_object);
     $result_check_object = $query_check_object->getResultSet();
     if (!isset($result_check_object[0]['object'])) {
         //	No user_to_follow_identification exists
         //
         $outputter->print_error(Error::withDomain(PRIVATE_EVENTS_REST_CONTROLLER_ERROR_DOMAIN, PRIVATE_EVENTS_REST_CONTROLLER_ERROR_CODE_ENTITY_DOES_NOT_EXIST, 'user_to_follow_identification should be an existing user identification.'));
     }
     //		Connection might exist
     //
     $client = new Everyman\Neo4j\Client('events.sb04.stations.graphenedb.com', 24789);
     $client->getTransport()->setAuth('Events', '3TP9LHROhv8LIcGmbYzq');
     $query_string_check_connection = 'OPTIONAL MATCH (user:AttendingUser)-[following:' . ATTENDINGUSER_RELATIONSHIP_NAME_FOLLOW . ']->(user_to_follow:CreatingUser)-[follower: ' . CREATINGUSER_RELATIONSHIP_NAME_FOLLOWER . ']->(user) ' . 'WHERE user.' . UNIVERSALLY_UNIQUE_OBJECT_KEY_IDENTIFICATION . " = '" . $inputter->variables_array['user_identification'] . "' AND user_to_follow." . UNIVERSALLY_UNIQUE_OBJECT_KEY_IDENTIFICATION . " = '" . $inputter->variables_array['user_to_follow_identification'] . "' " . 'RETURN following, follower';
     $query_check_connection = new Everyman\Neo4j\Cypher\Query($client, $query_string_check_connection);
     $result_check_connection = $query_check_connection->getResultSet();
     if (isset($result_check_connection[0]['following']) || isset($result_check_connection[0]['follower'])) {
         //	At least one object exists
         //
         $outputter->print_error(Error::withDomain(PRIVATE_EVENTS_REST_CONTROLLER_ERROR_DOMAIN, PRIVATE_EVENTS_REST_CONTROLLER_ERROR_CODE_RELATIONSHIP_EXISTS, 'Connection already exists between users.'));
     }
     //	3) We've got the go ahead to process the request as it's intended - create a follow connection
     //
     //	Get objects
     //
     $client = new Everyman\Neo4j\Client('events.sb04.stations.graphenedb.com', 24789);
     $client->getTransport()->setAuth('Events', '3TP9LHROhv8LIcGmbYzq');
     $query_string_users = 'MATCH (user:AttendingUser), (user_to_follow:CreatingUser) ' . 'WHERE user.' . UNIVERSALLY_UNIQUE_OBJECT_KEY_IDENTIFICATION . " = '" . $inputter->variables_array['user_identification'] . "' AND user_to_follow." . UNIVERSALLY_UNIQUE_OBJECT_KEY_IDENTIFICATION . " = '" . $inputter->variables_array['user_to_follow_identification'] . "' " . 'RETURN user, user_to_follow';
     $query_users = new Everyman\Neo4j\Cypher\Query($client, $query_string_users);
     $result_users = $query_users->getResultSet();
     $user = $result_users[0]['user'];
     $user_to_follow = $result_users[0]['user_to_follow'];
     //  Add relationship
     //
     $user->relateTo($user_to_follow, ATTENDINGUSER_RELATIONSHIP_NAME_FOLLOW)->save();
     $user_to_follow->relateTo($user, CREATINGUSER_RELATIONSHIP_NAME_FOLLOWER)->save();
     //  4) Send a notification to the new following user
     //
     //  Send a notification to all of the devices of the following user telling them they have a new follower
     //
     $name = $user->getProperty(CREATINGUSER_KEY_FIRST_NAME);
     if (!isset($name)) {
         $name = $user->getProperty(CREATINGUSER_KEY_NAME);
     }
     $identifiers = $user_to_follow->getProperty(USER_KEY_NOTIFICATION_DEVICE_IDENTIFIERS);
     if (isset($identifiers) && is_array($identifiers) && count($identifiers) > 0) {
         foreach ($identifiers as $identifier) {
             //  Create notification item
             //
             $payload = Payload::PayloadJSON(array('alert' => $name . ' has started following you.', 'sound' => 'default'), array('metadata' => 'extra stuff'));
             $notificationItem = new APNsNotificationItem($identifier, $payload, 'notificationIdentifier', null, null);
             //  Create notification
             //
             $notification = new APNsNotification(APNS_NOTIFICATION_COMMAND_PUSH, APNS_NOTIFICATION_FRAME_LENGTH, array($notificationItem));
             //  Send notification
             //
             $inputter->notificationController->APNsManager->sendNotification($notification);
         }
     }
     //	5) Output results
     //
     //	Print data
     //
     $outputter->print_data(array(array("user" => AttendingUser::printer_dictionary($user), "user_to_follow" => CreatingUser::printer_dictionary($user_to_follow))));
 }