aboutsummaryrefslogtreecommitdiff
path: root/src/Tools/PopulateDevDb.php
blob: 8f1b2b93aa4f08feee8bbe205ed7e33260758ad2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php


namespace Micropoly\Tools;


use Micropoly\Entrypoint;
use Micropoly\Env;
use Micropoly\Models\Note;
use SQLite3;

class PopulateDevDb implements Entrypoint
{
    private const NUM_NOTES = 1000;
    private const WORDS_FILE = "/usr/share/dict/cracklib-small";
    private const TAGS_MIN_RAND = 0;
    private const TAGS_MAX_RAND = 6;
    private const CHANCE_TRASH = 0.1;
    private const CHANCE_INBOX = 0.4;
    private const CONTENT_MIN_WORDS = 3;
    private const CONTENT_MAX_WORDS = 200;

    private array $words = [];

    private function readWords()
    {
        $words = file_get_contents(self::WORDS_FILE);
        $words = explode("\n", $words);
        $words = array_map("trim", $words);
        $words = array_filter($words);

        $this->words = $words;
    }

    public function run(Env $env)
    {
        $this->readWords();

        $db = $env->db();
        for ($i = 0; $i < self::NUM_NOTES; $i++)
            $this->createTestNote($db);
    }

    private function randomWords(int $min, int $max): array
    {
        $words = [];
        $num = mt_rand($min, $max);
        for ($i = 0; $i < $num; $i++)
            $words[] = $this->words[mt_rand(0, count($this->words)-1)];

        return $words;
    }

    private static function byChance(float $chance): bool
    {
        return mt_rand() / mt_getrandmax() <= $chance;
    }

    private function createTestNote(SQLite3 $db): void
    {
        $note = new Note();
        $tags = $this->randomWords(self::TAGS_MIN_RAND, self::TAGS_MAX_RAND);
        if (self::byChance(self::CHANCE_INBOX))
            $tags[] = "inbox";
        $note->setTags($tags);
        $note->setContent(implode(" ", $this->randomWords(self::CONTENT_MIN_WORDS, self::CONTENT_MAX_WORDS)));
        $note->setTrash(self::byChance(self::CHANCE_TRASH));

        $note->save($db);
    }
}