Example #1
0
 /**
  * This allows our application to log into the API so we can get the session
  * and corpToken to use in subsequent requests.
  *
  * @throws Exception
  * @return boolean
  */
 protected static function login()
 {
     $cache_id = 'bullhorn_token';
     $cache_token = wp_cache_get($cache_id, 'bullhorn');
     //	error_log( 'bullhorn_token from cache' . serialize($cache_token) );
     if (false === $cache_token) {
         if (false === self::refresh_token()) {
             return false;
         }
         $url = add_query_arg(array('version' => '*', 'access_token' => self::$api_access['access_token'], 'ttl' => 20), 'https://rest.bullhornstaffing.com/rest-services/login');
         $response = self::request($url);
         $body = json_decode($response['body']);
         // TODO: make to user freindly
         if (isset($body->errorMessage)) {
             $admin_email = get_option('admin_email');
             error_log('Login failed. With the message:' . $body->errorMessage);
             $headers = 'From: Bullhorn Plugin <' . $admin_email . '>';
             wp_mail($admin_email, 'Login failed please reconnect ', 'With the message: ' . $body->errorMessage, $headers);
             throw new Exception($body->errorMessage);
             return false;
         }
         if (isset($body->BhRestToken)) {
             self::$session = $body->BhRestToken;
             self::$url = $body->restUrl;
             wp_cache_add($cache_id, $body, 'bullhorn', MINUTE_IN_SECONDS * 8);
             //	error_log( 'bullhorn_token from call ' . serialize($body) );
             return true;
         }
     } else {
         if (!is_object($cache_token)) {
             $cache_token = json_decode($cache_token);
         }
         self::$session = $cache_token->BhRestToken;
         self::$url = $cache_token->restUrl;
         return true;
     }
     return false;
 }