Skip to content

Instantly share code, notes, and snippets.

@trikitrok
Last active October 4, 2024 12:46
SecurityManager using better designed ports
using System;
using LegacySecurityManager.infrastructure;
namespace LegacySecurityManager;
public class SecurityManager
{
private readonly Notifier _notifier;
private readonly UserInputRequester _userInputRequester;
public SecurityManager(Notifier notifier, Input input)
{
_notifier = notifier;
_userInputRequester = new UserInputRequester(input);
}
public void CreateValidUser()
{
var userInputData = _userInputRequester.Request();
if (!userInputData.PasswordsMatch())
{
NotifyPasswordDoNotMatch();
return;
}
if (!userInputData.PasswordIsLongEnough())
{
NotifyPasswordIsToShort();
return;
}
NotifyUserCreation(userInputData);
}
private void NotifyPasswordIsToShort()
{
Print($"Password must be at least {UserInputData.Min_Length} characters in length");
}
private void NotifyPasswordDoNotMatch()
{
Print("The passwords don't match");
}
private void NotifyUserCreation(UserInputData userInputData)
{
Print(ComposeUserCreationMessage(userInputData));
}
private static string ComposeUserCreationMessage(UserInputData userInputData)
{
return $"Saving Details for User ({userInputData.UserName()}, {userInputData.FullName()}, {EncryptPassword(userInputData.Password())})\n";
}
private static string EncryptPassword(string password)
{
var array = password.ToCharArray();
Array.Reverse(array);
var encryptedPassword = new string(array);
return encryptedPassword;
}
private void Print(string message)
{
_notifier.Notify(message);
}
public static void CreateUser()
{
new SecurityManager(new ConsoleNotifier(), new ConsoleInput()).CreateValidUser();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment