Compare commits

..

7 Commits

5 changed files with 5 additions and 282 deletions

View File

@ -1,20 +1,13 @@
<?php
use Faker\Factory;
use MapGuesser\PersistentData\Model\User;
use MapGuesser\Repository\UserRepository;
use MapGuesser\Util\UsernameGenerator;
use SokoWeb\Database\Query\Select;
$select = new Select(Container::$dbConnection);
$users = Container::$persistentDataManager->selectMultipleFromDb($select, User::class);
$userRepository = new UserRepository();
$usernameGenerator = new UsernameGenerator();
foreach ($users as $user) {
do {
$username = $usernameGenerator->generate();
} while ($userRepository->getByUsername($username));
$user->setUsername($username);
$user->setUsername(Factory::create()->userName);
Container::$persistentDataManager->saveToDb($user);
}

View File

@ -2,6 +2,7 @@
use DateInterval;
use DateTime;
use Faker\Factory;
use SokoWeb\Http\Request;
use SokoWeb\Interfaces\Response\IContent;
use SokoWeb\Interfaces\Response\IRedirect;
@ -14,7 +15,6 @@ use MapGuesser\Repository\UserConfirmationRepository;
use MapGuesser\Repository\UserPasswordResetterRepository;
use MapGuesser\Repository\UserPlayedPlaceRepository;
use MapGuesser\Repository\UserRepository;
use MapGuesser\Util\UsernameGenerator;
use SokoWeb\Response\HtmlContent;
use SokoWeb\Response\JsonContent;
use SokoWeb\Response\Redirect;
@ -368,7 +368,7 @@ class LoginController
return new JsonContent(['error' => ['errorText' => 'Username can contain only english letters, digits, - (hyphen), . (dot), _ (underscore).']]);
}
$newUser->setUsername(strlen(\Container::$request->post('username')) > 0 ? \Container::$request->post('username') : (new UsernameGenerator())->generate());
$newUser->setUsername(strlen(\Container::$request->post('username')) > 0 ? \Container::$request->post('username') : Factory::create()->userName);
$newUser->setCreatedDate(new DateTime());
\Container::$persistentDataManager->saveToDb($newUser);

View File

@ -132,7 +132,7 @@ class User extends Model implements IUser
public function getDisplayName(): string
{
return $this->username;
return $this->email;
}
public function checkPassword(string $password): bool

View File

@ -1,247 +0,0 @@
<?php namespace MapGuesser\Util;
class UsernameGenerator
{
const ADJECTIVES = [
'abundant',
'agile',
'alluring',
'ample',
'adorable',
'angry',
'anxious',
'astonishing',
'beautiful',
'big',
'bitter',
'blissful',
'blue',
'brave',
'bright',
'brilliant',
'busy',
'calm',
'captivating',
'careful',
'charming',
'cheerful',
'clumsy',
'colorful',
'confused',
'cooperative',
'courageous',
'cozy',
'crispy',
'curious',
'dazzling',
'delightful',
'determined',
'eager',
'elegant',
'enchanting',
'enthusiastic',
'exciting',
'exquisite',
'faithful',
'fancy',
'fearless',
'fierce',
'fluffy',
'fresh',
'friendly',
'frigid',
'funny',
'gentle',
'glorious',
'graceful',
'grateful',
'happy',
'harmonious',
'healthy',
'helpful',
'honest',
'hopeful',
'hot',
'humble',
'hungry',
'impressive',
'infamous',
'innocent',
'intense',
'jolly',
'joyful',
'kind',
'lively',
'lonely',
'lovely',
'lucky',
'mysterious',
'naughty',
'nervous',
'nutritious',
'obedient',
'peaceful',
'playful',
'polite',
'powerful',
'precious',
'proud',
'radiant',
'reckless',
'reliable',
'rich',
'romantic',
'rough',
'sad',
'scary',
'sensitive',
'shiny',
'silky',
'sincere',
'sleepy',
'smart',
'sneaky',
'soft',
'sparkling',
'splendid',
'strong',
'stubborn',
'sweet',
'tender',
'thoughtful',
'thrilling',
'timid',
'tranquil',
'trustworthy',
'unique',
'vibrant',
'victorious',
'warm',
'wise',
'witty',
'wonderful',
'worried',
'zealous'
];
const NOUNS = [
'airplane',
'ant',
'apple',
'aquarium',
'backpack',
'banana',
'bear',
'bee',
'camera',
'car',
'cat',
'chocolate',
'desk',
'diamond',
'dog',
'dolphin',
'duck',
'egg',
'eiffeltower',
'elephant',
'fire',
'flower',
'forest',
'fork',
'fox',
'galaxy',
'giraffe',
'globe',
'guitar',
'hammer',
'hamster',
'hat',
'house',
'icecream',
'iguana',
'island',
'jacket',
'jaguar',
'jellyfish',
'jigsaw',
'kangaroo',
'key',
'kite',
'koala',
'lamp',
'lighthouse',
'lightning',
'lion',
'llama',
'moon',
'mountain',
'mouse',
'necklace',
'nest',
'newt',
'notebook',
'ocean',
'octopus',
'orchid',
'owl',
'panda',
'pencil',
'penguin',
'piano',
'queen',
'quilt',
'quokka',
'rabbit',
'rainbow',
'robot',
'ship',
'snake',
'statue',
'sun',
'sunflower',
'table',
'telescope',
'tiger',
'tree',
'turtle',
'uakari',
'umbrella',
'unicorn',
'universe',
'vase',
'violin',
'volcano',
'vulture',
'wallaby',
'waterfall',
'whale',
'xray',
'xylophone',
'yacht',
'yak',
'yarn',
'yeti',
'zebra',
'zeppelin',
'zucchini',
];
function generate(): string
{
$numberOfAdjectives = count(self::ADJECTIVES);
$numberOfNouns = count(self::NOUNS);
$firstAdjective = self::ADJECTIVES[mt_rand(0, $numberOfAdjectives - 1)];
do {
$secondAdjective = self::ADJECTIVES[mt_rand(0, $numberOfAdjectives - 1)];
} while ($firstAdjective === $secondAdjective);
$noun = self::NOUNS[mt_rand(0, $numberOfNouns - 1)];
$firstAdjective = ucfirst($firstAdjective);
$secondAdjective = ucfirst($secondAdjective);
$noun = ucfirst($noun);
return $firstAdjective . $secondAdjective . $noun;
}
}

View File

@ -1,23 +0,0 @@
<?php namespace MapGuesser\Tests\Util;
use MapGuesser\Util\UsernameGenerator;
use PHPUnit\Framework\TestCase;
final class UsernameGeneratorTest extends TestCase
{
public function testCanGenerateRandomUsernameFromComponents(): void
{
$generator = new UsernameGenerator();
$parts = $this->getUsernameParts($generator->generate());
$this->assertEquals(3, count($parts));
$this->assertContains($parts[0], UsernameGenerator::ADJECTIVES);
$this->assertContains($parts[1], UsernameGenerator::ADJECTIVES);
$this->assertContains($parts[2], UsernameGenerator::NOUNS);
}
private function getUsernameParts(string $username): array
{
return explode('-', strtolower(preg_replace('/([a-z])([A-Z])/', '$1-$2', $username)));
}
}