aboutsummaryrefslogtreecommitdiff
path: root/src/Tools/PopulateDevDb.php
diff options
context:
space:
mode:
authorLaria Carolin Chabowski <laria@laria.me>2020-02-07 09:44:59 +0100
committerLaria Carolin Chabowski <laria@laria.me>2020-02-07 09:44:59 +0100
commit2eb5a432d2229788ce2fdb09f36c6f4bebdea813 (patch)
treeab57978bdda34c82b025b897cfb6825b1fd1e654 /src/Tools/PopulateDevDb.php
downloadmicropoly-2eb5a432d2229788ce2fdb09f36c6f4bebdea813.tar.gz
micropoly-2eb5a432d2229788ce2fdb09f36c6f4bebdea813.tar.bz2
micropoly-2eb5a432d2229788ce2fdb09f36c6f4bebdea813.zip
Initial commit
Diffstat (limited to 'src/Tools/PopulateDevDb.php')
-rw-r--r--src/Tools/PopulateDevDb.php71
1 files changed, 71 insertions, 0 deletions
diff --git a/src/Tools/PopulateDevDb.php b/src/Tools/PopulateDevDb.php
new file mode 100644
index 0000000..8f1b2b9
--- /dev/null
+++ b/src/Tools/PopulateDevDb.php
@@ -0,0 +1,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);
+ }
+} \ No newline at end of file