yet another github api refactoring
[pharext/pharext.org] / app / Controller / Github.php
index 49b14885be7eb986f88e88e73a884ac0a67ee961..164f3205134070d8464ca6927382151f9f050594 100644 (file)
@@ -8,7 +8,7 @@ use app\Session;
 use app\Web;
 
 use http\QueryString;
-use http\Url;
+use http\Header;
 
 abstract class Github implements Controller
 {
@@ -26,12 +26,25 @@ abstract class Github implements Controller
         * @var \app\Session
         */
        protected $session;
-
-       function __construct(Web $app, API $github) {
+       
+       function __construct(Web $app, API $github, Session $session) {
                $this->app = $app;
                $this->github = $github;
-               $this->app->getView()->addData(["location" => "github"]);
-               $this->app->getView()->registerFunction("check", [$this, "checkRepoHook"]);
+               $this->session = $session;
+               $this->app->getView()->addData(compact("session") + [
+                       "location" => "github", 
+                       "title" => "Github"
+               ]);
+               $this->app->getView()->registerFunction("check", [$github, "checkRepoHook"]);
+               
+               if (($header = $this->app->getRequest()->getHeader("Cache-Control", Header::class))) {
+                       $params = $header->getParams();
+                       if (!empty($params["no-cache"])) {
+                               $this->github->setMaxAge(0);
+                       } elseif (!empty($params["max-age"])) {
+                               $this->github->setMaxAge($params["max-age"]["value"]);
+                       }
+               }
        }
 
        protected function checkToken() {
@@ -44,35 +57,4 @@ abstract class Github implements Controller
                ]));
                return false;
        }
-
-       function setSession(Session $session) {
-               $this->session = $session;
-       }
-
-       function checkRepoHook($repo) {
-               if ($repo->hooks) {
-                       foreach ($repo->hooks as $hook) {
-                               if ($hook->name === "web" && $hook->config->url === $this->github->getConfig()->hook->url) {
-                                       return true;
-                               }
-                       }
-               }
-               return false;
-       }
-
-       function createLinkGenerator($links) {
-               return function($which) use($links) {
-                       if (!isset($links[$which])) {
-                               if ($which !== "next" || !isset($links["last"])) {
-                                       return null;
-                               } else {
-                                       $which = "last";
-                               }
-                       }
-                       $url = new Url($links[$which], null, 0);
-                       $qry = new QueryString($url->query);
-                       return $qry->getInt("page", 1);
-               };
-       }
-
 }