Last active
May 11, 2022 20:42
-
-
Save DarkGhostHunter/7a2250c0901e0dfc77f1ba86e7b2ed72 to your computer and use it in GitHub Desktop.
Password helper for Factories
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Database\Factories; | |
use Illuminate\Support\Facades\Hash; | |
trait StaticPassword | |
{ | |
/** | |
* An 8-character password containing lower case, upper case, an special caracter and a number. | |
* | |
* @var string | |
*/ | |
public const SAFE_PASSWORD = 'P@s$w0rD'; | |
/** | |
* The array containing already hashed passwords | |
* | |
* @var array<string, string> | |
*/ | |
protected static array $passwords = []; | |
/** | |
* Generates a hash for a password only once. | |
* | |
* @param string $password Defaults as "password". | |
* @return string The new password hash. | |
*/ | |
protected static function password(string $password = 'password'): string | |
{ | |
return self::$passwords[$password] ??= Hash::make($password); | |
} | |
/** | |
* Generates a hash for a save password | |
* | |
* @return string The password hash for "P@s$w0rD" | |
*/ | |
protected static function safePassword(): string | |
{ | |
return self::password(self::SAFE_PASSWORD); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment