I want (at this moment tyring) to follow the SOLID principles, but my mind is going to blows up.
I read a lot of posts about Repository Pattern in Laravel to follow the SOLID principles. My question is very similar to this question. But I don't understand how can I don't violate the Open/Closed Principal in the Factory Pattern
I'm developing a Two Factor Authentication system and I have multiple method to use as tfa.
Right now:
- Authenticator App
- SMS
Let's jump to the code:
Controller: (Without factory)
public function index(Request $request)
{
// Violate the OCP. I'm modyfing the class.
switch ($request->method) {
case 'authenticator':
$tfaMethod = new Authenticator;
break;
case 'sms':
$tfaMethod = new SMS;
break;
}
return (new TwoFactorMethod)->process($this->currentUser, $tfaMethod);
}
TwoFactorMethod class:
public function process($user, MethodInterface $method)
{
return $method->process($user);
}
Each method have their own class. It's ok. But if I want to add a new method, such as: E-mail i'll broken the OCP in the class using the switch case.
How can I "fix"? Or is just a misunderstanding from my side?
Thank you!