<?php

/**
 *  Informatica Eindproject D4p
 *  6in3, Stedelijk Gymnasium Nijmegen
 *  Docent: Hans de Wolf
 *
 *  ==================
 *
 *  Daniel Boutros,
 *  Christiaan Goossens,
 *  Jelmer Hinssen
 */

namespace Inforbank\Application\Auth;

use Inforbank\Application;

class Authorization
{
    public function login($rekeningnr, $passcode)
    {
        $container = Application::getContainer();
        $db = $container->db;

        $rekeningen = $db->rekeningen->where('rekeningnr', $rekeningnr);

        if (count($rekeningen) !== 1) {
            throw new \Exception("Rekening niet bekend.");
        }

        $id = $rekeningen->max("id");
        $klantnr = $rekeningen[$id]['klantid'];

        $klant = $db->klanten->where('id', $klantnr)[$klantnr];

        if ($klant['code'] === $passcode) {
            $_SESSION['userid'] = $klant['id'];
            return true;
        }

        return false;
    }

    public function getUserID()
    {
        return $_SESSION['userid'];
    }

    public function isUserAuthenticated()
    {
        return $this->getUserID() !== null;
    }

    public function logout()
    {
        unset($_SESSION['userid']);
    }
}
