public function testCanGetSession() { $session = new Model\Session(); $expectedArray = array("session_value" => "some_value"); $session->set("some_session", "some_value"); $this->assertEquals($expectedArray["session_value"], $session->get("some_session")); }
/** * Index action, the home page. * @return The showHome view */ public function index() { $session = new Session(); $auth = $session->get($this->auth->getTokenSessionName()); $context = array($this->view->getAuthField() => $auth, $this->view->getSearchField() => $this->search->getSearchFieldName()); return $this->view->showHome($context); }
/** * Logged in action, get's the session, current user, * those the user follow and activity * @param String $accessToken * @return View showLoggedIn */ public function loggedInUser($accessToken) { $user = $this->model->getLoggedInUser($accessToken); $session = new Session(); // If we can't find the user, just destroy it. if ($user === null) { $session->destroy($this->token); return; } $auth = $session->get($this->token); $followers = $this->followers->getFollowers($user->getLogin()); $events = null; // Get events if ($followers !== null) { foreach ($followers as $follower) { $events[] = $this->model->getUserActivity($follower["user"]); } } $context = array($this->view->getFollowersField() => $followers, $this->view->getUserField() => $user, $this->view->getEventField() => $events, $this->view->getAuthField() => $auth, $this->view->getSearchField() => $this->search->getSearchFieldName()); return $this->view->showLoggedIn($context); }
public function doRoute() { $klein = new Klein(); $this->config = new Config("../app/config/app.php"); $klein->respond("GET", $this->homePath, function () { $controller = new Home(new View\Home()); $controller->index(); }); $klein->respond("GET", $this->loginPath . "?", function ($request, $response) { $url = $this->auth->index(); $response->redirect($url); }); $klein->respond("GET", "/github-callback/?", function ($request, $response) { if ($request->code) { $this->auth->getToken($request->code); return $response->redirect($this->userPath); } return $response->redirect($this->homePath); }); $klein->respond("GET", $this->userPath . "?", function ($request, $response) { $session = new Session(); $token = $session->get($this->auth->getTokenSessionName()); if ($token) { return $this->auth->loggedInUser($token); } return $response->redirect($this->homePath); }); $klein->respond("GET", $this->logoutPath . "?", function ($request, $response) { $session = new Session(); $session->destroy($this->auth->getTokenSessionName()); return $response->redirect($this->homePath); }); $klein->with($this->usersPath, function () use($klein) { $klein->respond("GET", "/?", function ($request, $response) { $controller = new Users(); $controller->index($request->fetchFromID); }); $klein->respond("GET", "/[:username]/?", function ($request, $response) { $controller = new Users(); $controller->show($request->username); }); }); $klein->respond("GET", $this->searchPath . "?", function ($request, $response) { $controller = new Users(); $controller->search(); }); $klein->respond("GET", $this->followPath . "[:username]/?", function ($request, $response) { $controller = new Users(); $controller->follow($request->username); }); $klein->respond("GET", $this->unFollowPath . "[:username]/?", function ($request, $response) { $controller = new Users(); $controller->unFollow($request->username); }); $klein->respond("404", function ($request) { $page = $request->uri(); $controller = new Error(new View\Error()); $controller->pageNotFoundError($page); }); $klein->onError(function ($klein, $err_msg) { if ($this->config->get("DEBUG") === true) { throw new \Exception($err_msg); } else { $controller = new Error(new View\Error()); $controller->serverError(); error_log($err_msg, 3, $this->config->get("ERROR_LOG")); } }); $klein->dispatch(); }
/** * Action for start unfollowing a given user * @param String $user * @return Create Follower View */ public function unFollow($user) { $session = new Session(); $accessToken = $session->get($this->auth->getTokenSessionName()); if ($accessToken !== null) { $owner = $this->model->getLoggedInUser($accessToken)->getLogin(); $successMsg = $this->view->getSuccessMessage(); $errorMsg = $this->view->getErrorMessage(); if ($this->followers->removeFollower($owner, $user)) { $context = array($successMsg => "You no longer follow " . $user); } else { $context = array($errorMsg => "You do not follow " . $user . " so you cannot unfollow"); } return $this->view->createFollower($context); } return $this->errorView->showPageNotFound("/unfollow/" . $user); }