<?php namespace FaZeBook; use FaZeBook\Data; use FaZeBook\Data\User; class Session { private static $instance = null; public static function getInstance() { if (is_null(self::$instance)) { self::$instance = new Session(Data::getInstance()); } return self::$instance; } private $data; private function __construct(Data $data) { $this->data = $data; // start new or resume old session session_start(); } public function isLoggedIn() : bool { return !is_null($this->currentUser()); } public function login($email, $password) : bool { if ($this->isLoggedIn()) return false; $user_by_email = $this->data->getUserByEmail($email); if ($user_by_email != null) { if ($user_by_email->verifyPassword($password)) { $_SESSION['userId'] = $user_by_email->getId(); return true; } else { return false; } } return false; } public function logout() { unset($_SESSION['userId']); session_destroy(); } public function currentUser() { if (isset($_SESSION['userId'])) { return $this->data->getUser($_SESSION['userId']); } return null; } }