Exemplo n.º 1
0
 /**
  * Retrieves an instance of the GitHub object
  *
  * @param   \Joomla\Application\AbstractApplication  $app          Application object
  * @param   boolean                                  $useBot       Flag to use a bot account.
  * @param   string                                   $botUser      The bot account user name.
  * @param   string                                   $botPassword  The bot account password.
  *
  * @return  GitHub
  *
  * @since   1.0
  * @throws  \RuntimeException
  */
 public static function getInstance($app, $useBot = false, $botUser = '', $botPassword = '')
 {
     $options = new Registry();
     // Check if we're in the web application and a token exists
     if ($app instanceof \JTracker\Application) {
         $session = $app->getSession();
         $token = $session->get('gh_oauth_access_token');
     } else {
         $token = false;
     }
     // If a token is active in the session (web app), and we haven't been instructed to use a bot account, use that for authentication
     if ($token && !$useBot) {
         $options->set('gh.token', $token);
     } else {
         // Check if credentials are supplied
         if ($botUser && $botPassword) {
             $user = $botUser;
             $password = $botPassword;
         } else {
             // Check for support for multiple accounts
             $accounts = $app->get('github.accounts');
             if ($accounts) {
                 $user = isset($accounts[0]->username) ? $accounts[0]->username : null;
                 $password = isset($accounts[0]->password) ? $accounts[0]->password : null;
                 // Store the other accounts
                 $options->set('api.accounts', $accounts);
             } else {
                 // Support for a single account
                 $user = $app->get('github.username');
                 $password = $app->get('github.password');
             }
         }
         // Add the username and password to the options object if both are set
         if ($user && $password) {
             // Set the options from the first account
             $options->set('api.username', $user);
             $options->set('api.password', $password);
         }
     }
     // The cURL extension is required to properly work.
     $transport = HttpFactory::getAvailableDriver($options, array('curl'));
     // Check if we *really* got a cURL transport...
     if (!$transport instanceof Curl) {
         throw new \RuntimeException('Please enable cURL.');
     }
     $http = new Http($options, $transport);
     // Instantiate the object
     return new Github($options, $http);
 }