/**
  * Handle return of twitter authentication.
  *
  * @throws UserException
  */
 public function callback()
 {
     $di = Di::getInstance();
     $requestToken = $di->slim->request->get('oauth_token');
     $sessionOauthToken = $di->session->get('oauth_token');
     $requestSecret = $di->session->get('oauth_token_secret');
     if ($requestToken !== $sessionOauthToken) {
         throw new UserException('Unable to signin using twitter, try later');
     }
     $oauthVerifier = $di->slim->request->get('oauth_verifier');
     // Fetching access token using authorize request token
     $di->twitterOAuth->setOauthToken($requestToken, $requestSecret);
     $accessToken = $di->twitterOAuth->oauth('oauth/access_token', ['oauth_verifier' => $oauthVerifier]);
     $di->session->delete('oauth_token');
     $di->session->delete('oauth_token_secret');
     if (empty($accessToken['user_id']) || empty($accessToken['screen_name']) || empty($accessToken['oauth_token']) || empty($accessToken['oauth_token_secret'])) {
         throw new UserException('Unable to signin using twitter, try later');
     }
     $twitterUserId = $accessToken['user_id'];
     $twitterScreenName = $accessToken['screen_name'];
     $oauthToken = $accessToken['oauth_token'];
     $oauthTokenSecret = $accessToken['oauth_token_secret'];
     $di->twitterOAuth->setOauthToken($oauthToken, $oauthTokenSecret);
     // Check if access token works
     $credentials = $di->twitterOAuth->get('account/verify_credentials');
     $user = UserModel::getCollection(['twitterId' => $twitterUserId], 0, 1);
     if (empty($user)) {
         // Create user
         $user = UserModel::get();
         $user->name = $twitterScreenName;
         $user->twitterId = $twitterUserId;
         $user->twitterToken = $oauthToken;
         $user->twitterTokenSecret = $oauthTokenSecret;
         $user->save();
         // Create source
         $source = TwitterSourceModel::get();
         $source->method = 'user';
         $source->accessTokenKey = $oauthToken;
         $source->accessTokenSecret = $oauthTokenSecret;
         $source->save();
         // Associate user to source
         $source->associate($user);
         // Start source indexer
         $source->startIndexer();
     } else {
         $user = $user[0];
         if ($user->name !== $twitterScreenName || $user->twitterToken !== $oauthToken || $user->twitterTokenSecret !== $oauthTokenSecret) {
             $user->name = $twitterScreenName;
             $user->twitterToken = $oauthToken;
             $user->twitterTokenSecret = $oauthTokenSecret;
             $user->save();
         }
     }
     $this->authentify($user);
     $di->slim->redirect('/home', 302);
 }
 public function testAssociateUser()
 {
     $user = User::get();
     $user->name = 'John doe';
     $user->save();
     $source = TwitterSource::get();
     $source->method = 'user';
     $source->accessTokenKey = 'aTk';
     $source->accessTokenSecret = 'aTks';
     $source->save();
     $source->associate($user);
 }