<commit_before><?php

namespace SimpleDAV\Model;

use PicoDb\Database;
use PicoFarad\Session;

class User {

    public static function loggedIn() {
        return empty($_SESSION['loggedin']) ? null : $_SESSION['loggedin'];
    }

    public static function isAdmin() {
        $username = self::loggedIn();
        if (!isset($username)) {
            return false;
        }

        return Database::get('db')->table('users')->equals('username', $username)->findOneColumn('role') == Role::ADMIN;
    }

    public static function logout() {
        Session\close();
    }

    public static function exists($username) {
        return Database::get('db')->table('users')->equals('username', $username)->count() === 1;
    }

    public static function getHash($username) {
        return Database::get('db')->table('users')->equals('username', $username)->findOneColumn('digesta1');
    }

    public static function updateHash($username, $hash) {
        Database::get('db')->table('users')->equals('username', $username)->save(['digesta1' => $hash]);
    }

    public static function create($username, $hash) {
        if (self::exists($username)) {
            return false;
        }

        $db = Database::get('db');
        $db->table('users')->insert(['username' => $username, 'digesta1' => $hash]);
        $db->table('principals')->insert(['uri' => "principals/$username/calendar-proxy-read"]);
        $db->table('principals')->insert(['uri' => "principals/$username/calendar-proxy-write"]);
        $db->table('addressbooks')->insert([
            'principaluri' => "principals/$username",
            'displayname' => 'Contacts',
            'uri' => 'default',
            'description' => '',
            'synctoken' => '1'
        ]);
        $db->table('calendars')->insert([
            'principaluri' => "principals/$username",
            'displayname' => 'Contacts',
            'uri' => 'default',
            'components' => 'VEVENT,VTODO',
            'synctoken' => '1',
            'transparent' => '0'
        ]);
        return true;
    }

    public static function getUsers() {<commit_msg>refactor user creation into separate functions<commit_after><?php

namespace SimpleDAV\Model;

use PicoDb\Database;
use PicoFarad\Session;

class User {

    public static function loggedIn() {
        return empty($_SESSION['loggedin']) ? null : $_SESSION['loggedin'];
    }

    public static function isAdmin() {
        $username = self::loggedIn();
        if (!isset($username)) {
            return false;
        }

        return Database::get('db')->table('users')->equals('username', $username)->findOneColumn('role') == Role::ADMIN;
    }

    public static function logout() {
        Session\close();
    }

    public static function exists($username) {
        return Database::get('db')->table('users')->equals('username', $username)->count() === 1;
    }

    public static function getHash($username) {
        return Database::get('db')->table('users')->equals('username', $username)->findOneColumn('digesta1');
    }

    public static function updateHash($username, $hash) {
        Database::get('db')->table('users')->equals('username', $username)->save(['digesta1' => $hash]);
    }

    private static function createPrincipal($username) {
        $db = Database::get('db');
        $db->table('principals')->insert(['uri' => "principals/$username/calendar-proxy-read"]);
        $db->table('principals')->insert(['uri' => "principals/$username/calendar-proxy-write"]);
    }

    private static function createAddressBook($username, $addressBook = 'Contacts') {
        $db = Database::get('db');
        $db->table('addressbooks')->insert([
            'principaluri' => "principals/$username",
            'displayname' => $addressBook,
            'uri' => 'default',
            'description' => '',
            'synctoken' => '1'
        ]);
    }

    private static function createCalendar($username, $calendarName = 'Calendar') {
        $db = Database::get('db');
        $db->table('calendars')->insert([
            'principaluri' => "principals/$username",
            'displayname' => $calendarName,
            'uri' => 'default',
            'components' => 'VEVENT,VTODO',
            'synctoken' => '1',
            'transparent' => '0'
        ]);
    }

    public static function create($username, $hash) {
        if (self::exists($username)) {
            return false;
        }

        $db = Database::get('db');
        $db->table('users')->insert(['username' => $username, 'digesta1' => $hash]);
        self::createPrincipal($username);
        self::createAddressBook($username);
        self::createCalendar($username);
        return true;
    }

    public static function getUsers() {
{'fraction_non_alphanumeric': 0.18490566037735848, 'fraction_numerical': 0.0029350104821802936, 'mean_word_length': 2.7596532702915684, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '66881', 'n_tokens_mistral': 1335, 'n_tokens_neox': 1313, 'n_words': 244}