예제 #1
0
파일: Developer.php 프로젝트: nevetS/flame
 /**
  * Saves user data to the Management API. This operates as both insert and
  * update.
  *
  * If user's email doesn't look valid (must contain @), a
  * ParameterException is thrown.
  *
  * @param bool|null $force_update
  *   If false, assume that this is a new instance.
  *   If true, assume that this is an update to an existing instance.
  *   If null, try an update, and if that fails, try an insert.
  * @param string|null $old_email
  *   If the developer's email has changed, this field must contain the
  *   previous email value.
  *
  * @throws \Apigee\Exceptions\ParameterException
  */
 public function save($force_update = false, $old_email = null)
 {
     // See if we need to brute-force this.
     if ($force_update === null) {
         try {
             $this->save(true, $old_email);
         } catch (ResponseException $e) {
             if ($e->getCode() == 404) {
                 // Update failed because dev doesn't exist.
                 // Try insert instead.
                 $this->save(false, $old_email);
             } else {
                 // Some other response error.
                 throw $e;
             }
         }
         return;
     }
     if (!$this->validateUser()) {
         $message = 'Developer requires valid-looking email address, firstName, lastName and userName.';
         throw new ParameterException($message);
     }
     if (empty($old_email)) {
         $old_email = $this->email;
     }
     $payload = array('email' => $this->email, 'userName' => $this->userName, 'firstName' => $this->firstName, 'lastName' => $this->lastName);
     if (count($this->attributes) > 0) {
         $payload['attributes'] = array();
         $i = 0;
         foreach ($this->attributes as $name => $value) {
             $i++;
             if ($i > self::MAX_ATTRIBUTE_COUNT && $this->pagingEnabled) {
                 // This truncation occurs silently; should we throw an exception?
                 break;
             }
             $payload['attributes'][] = array('name' => $name, 'value' => $value);
         }
     }
     $url = null;
     if ($force_update || $this->createdAt) {
         if ($this->developerId) {
             $payload['developerId'] = $this->developerId;
         }
         $url = rawurlencode($old_email);
     }
     // Save our desired status for later.
     $cached_status = $this->status;
     if ($force_update) {
         $this->put($url, $payload);
     } else {
         $this->post($url, $payload);
     }
     self::loadFromResponse($this, $this->responseObj);
     // We must cache the DebugData from the developer-save call so that
     // we can make it available to clients AFTER the "action" call below.
     $responseData = DebugData::toArray();
     // If status has changed, we must directly change it with a separate
     // POST call, because Edge will ignore a 'status' member in the
     // app-save payload.
     // We must also do this when creating a developer ex nihilo in order
     // to set initial status. Otherwise new developer will have default
     // status, which is generally 'approved'.
     $this->post($this->email . '?action=' . $cached_status);
     $this->status = $cached_status;
     // Restore DebugData from cached response.
     DebugData::fromArray($responseData);
 }