Пример #1
0
 /**
  * Tracks the occurrence of a custom event with additional dimensions.
  * Avos will store a data point at the time of invocation with the given
  * event name.
  *
  * Dimensions will allow segmentation of the occurrences of this custom
  * event. Keys and values should be strings, and will throw
  * otherwise.
  *
  * To track a user signup along with additional metadata, consider the
  * following:
  * <pre>
  * $dimensions = array(
  *  'gender' => 'm',
  *  'source' => 'web',
  *  'dayType' => 'weekend'
  * );
  * AVAnalytics::track('signup', $dimensions);
  * </pre>
  *
  * There is a default limit of 4 dimensions per event tracked.
  *
  * @param string $name The name of the custom event
  * @param array $dimensions The dictionary of segment information
  *
  * @throws \Exception
  * @return mixed
  */
 public static function track($name, $dimensions = array())
 {
     $name = trim($name);
     if (strlen($name) === 0) {
         throw new Exception('A name for the custom event must be provided.');
     }
     foreach ($dimensions as $key => $value) {
         if (!is_string($key) || !is_string($value)) {
             throw new Exception('Dimensions expected string keys and values.');
         }
     }
     return AVClient::_request('POST', '/events/' . $name, null, static::_toSaveJSON($dimensions));
 }
Пример #2
0
 /**
  * Save Object and unsaved children within.
  *
  * @param $target
  * @param bool $useMasterKey Whether to use the Master Key.
  *
  * @return null
  *
  * @throws AVException
  */
 private static function deepSave($target, $useMasterKey = false)
 {
     $unsavedChildren = array();
     $unsavedFiles = array();
     static::findUnsavedChildren($target, $unsavedChildren, $unsavedFiles);
     $sessionToken = null;
     if (AVUser::getCurrentUser()) {
         $sessionToken = AVUser::getCurrentUser()->getSessionToken();
     }
     foreach ($unsavedFiles as &$file) {
         $file->save();
     }
     $objects = array();
     // Get the set of unique objects among the children.
     foreach ($unsavedChildren as &$obj) {
         if (!in_array($obj, $objects, true)) {
             $objects[] = $obj;
         }
     }
     $remaining = $objects;
     while (count($remaining) > 0) {
         $batch = array();
         $newRemaining = array();
         foreach ($remaining as $key => &$object) {
             if (count($batch) > 40) {
                 $newRemaining[] = $object;
                 continue;
             }
             if ($object->canBeSerialized()) {
                 $batch[] = $object;
             } else {
                 $newRemaining[] = $object;
             }
         }
         $remaining = $newRemaining;
         if (count($batch) === 0) {
             throw new Exception("Tried to save a batch with a cycle.");
         }
         $requests = array();
         foreach ($batch as $obj) {
             $json = $obj->getSaveJSON();
             $method = 'POST';
             $path = '/classes/' . $obj->getClassName();
             if ($obj->getObjectId()) {
                 $path .= '/' . $obj->getObjectId();
                 $method = 'PUT';
             }
             $requests[] = array('method' => $method, 'path' => $path, 'body' => $json);
         }
         if (count($requests) === 1) {
             $req = $requests[0];
             $result = AVClient::_request($req['method'], $req['path'], $sessionToken, json_encode($req['body']), $useMasterKey);
             $batch[0]->mergeAfterSave($result);
         } else {
             $result = AVClient::_request('POST', '/batch', $sessionToken, json_encode(array("requests" => $requests)), $useMasterKey);
             $errorCollection = array();
             foreach ($batch as $key => &$obj) {
                 if (isset($result[$key]['success'])) {
                     $obj->mergeAfterSave($result[$key]['success']);
                 } else {
                     if (isset($result[$key]['error'])) {
                         $response = $result[$key];
                         $error = $response['error']['error'];
                         $code = isset($response['error']['code']) ? $response['error']['code'] : -1;
                         $errorCollection[] = array('error' => $error, 'code' => $code, 'object' => $obj);
                     } else {
                         $errorCollection[] = array('error' => 'Unknown error in batch save.', 'code' => -1, 'object' => $obj);
                     }
                 }
             }
             if (count($errorCollection)) {
                 throw new AVAggregateException("Errors during batch save.", $errorCollection);
             }
         }
     }
 }