, "username": "", "domain": "", * "passwords": [{ "hash": "", "algorithm": ""}], * "alias": { "alias": "", "domain": ""} * }, * {"type": "range", "id": "%range%", "username": "user_%range%", "domain": "", * "range": {"from": 5, "to": 10}, * "passwords": [{ "hash": "", "algorithm": ""}] * } * ] */ class LiblinphoneTesterAccoutSeeder extends Seeder { public function run($json) { $accounts = []; $passwords = []; $aliases = []; foreach ($json as $element) { if ($element->type == 'account') { array_push( $accounts, $this->generateAccountArray( $element->id, $element->username, $element->domain, $element->activated ?? true ) ); if (isset($element->passwords)) { foreach ($element->passwords as $password) { array_push( $passwords, $this->generatePasswordArray( $element->id, $password->hash, $password->algorithm ) ); } } if (isset($element->alias)) { array_push( $aliases, $this->generateAliasArray( $element->id, $element->alias->alias, $element->alias->domain ) ); } } if ($element->type == 'range') { for ($i = $element->range->from; $i <= $element->range->to; $i++) { array_push( $accounts, $this->generateAccountArray( str_replace('%range%', $i, $element->id), str_replace('%range%', $i, $element->username), str_replace('%range%', $i, $element->domain), $element->activated ?? true ) ); array_push( $passwords, $this->generatePasswordArray($i, 'secret', 'CLRTXT') ); } } } // Ensure that we clear previous ones $ids = array_map(function($account) { return (int)$account['id']; }, $accounts); Account::withoutGlobalScopes()->whereIn('id', $ids)->delete(); // And seed the fresh ones DB::table('accounts')->insert($accounts); DB::table('passwords')->insert($passwords); DB::table('aliases')->insert($aliases); } private function generateAccountArray( int $id, string $username, string $domain, bool $activated = true, string $confirmationKey = null ): array { return [ 'id' => $id, 'username' => $username, 'domain' => $domain, 'email' => rawurlencode($username) . '@' . $domain, 'activated' => $activated, 'ip_address' => '', 'confirmation_key' => $confirmationKey, 'user_agent' => 'FlexiAPI Seeder', 'creation_time' => '2010-01-03 04:30:43' ]; } private function generatePasswordArray( int $accountId, string $password, string $algorythm ): array { return [ 'account_id' => $accountId, 'password' => $password, 'algorithm' => $algorythm ]; } private function generateAliasArray( int $accountId, string $alias, string $domain ): array { return [ 'account_id' => $accountId, 'alias' => $alias, 'domain' => $domain ]; } }