yet another github api refactoring
[pharext/pharext.org] / app / Controller / Github / Callback.php
index ac2ce7b481d2bca63e680769e55833708c8b04e8..646d782f20d52c59676efdbd23a72a39ddd3c5ba 100644 (file)
@@ -3,32 +3,62 @@
 namespace app\Controller\Github;
 
 use app\Controller\Github;
+use app\Github\API;
+use app\Model\Accounts;
+use app\Session;
+use app\Web;
 
 class Callback extends Github
 {
+       /**
+        * @var Accounts
+        */
+       private $accounts;
+       
+       function __construct(Web $app, API $github, Session $session, Accounts $accounts) {
+               parent::__construct($app, $github, $session);
+               $this->accounts = $accounts;
+       }
+       
        function __invoke(array $args = null) {
                if ($this->app->getRequest()->getQuery("error")) {
                        $this->app->getView()->addData([
                                "error" => $this->app->getRequest()->getQuery("error_description")
                        ]);
                } else {
-                       try {
-                               $this->github->fetchToken(
-                                       $this->app->getRequest()->getQuery("code"),
-                                       $this->app->getRequest()->getQuery("state"),
-                                       function($json) {
-                                               $this->github->setToken($json->access_token);
-                               })->send();
-                               if (isset($this->session->returnto)) {
-                                       $this->app->redirect($this->session->returnto);
-                               } else {
-                                       $this->app->redirect(
-                                               $this->app->getBaseUrl()->mod("./github"));
-                               }
-                       } catch (\app\Github\Exception $exception) {
-                               $this->app->getView()->addData(compact("exception"));
+                       $this->github->fetchToken(
+                               $this->app->getRequest()->getQuery("code"),
+                               $this->app->getRequest()->getQuery("state"),
+                               function($token) {
+                                       $this->github->setToken($token->access_token);
+                                       $this->github->readAuthUser($this->createUserCallback($token));
+                       })->send();
+                       if (isset($this->session->returnto)) {
+                               $returnto = $this->session->returnto;
+                               unset($this->session->returnto);
+                               $this->app->redirect($returnto);
+                       } else {
+                               $this->app->redirect(
+                                       $this->app->getBaseUrl()->mod("./github"));
                        }
                }
                $this->app->display("github/callback");
        }
+       
+       function createUserCallback($token) {
+               return function($user) use($token) {
+                       $tx = $this->accounts->getConnection()->startTransaction();
+                       
+                       if (!($account = $this->accounts->byOAuth("github", $token->access_token, $user->login))) {
+                               $account = $this->accounts->createOAuthAccount("github", $token->access_token, $user->login);
+                       }
+                       $account->updateToken("github", $token->access_token, $token);
+                       $owner = $account->updateOwner("github", $user->login, $user);
+                       
+                       $tx->commit();
+                       
+                       $this->session->account = $account->account->get();
+                       $this->session->github = (object) $owner->export();
+               };
+       }
 }