If you ever need to add a new type of user, your controller will violate the open/closed principle. I'd Consider creating a general repository interface that reflects the most common CRUD operations or at least a user interface and pass an implementation to a function that does the saving.
SOLID Principle In Laravel with Repository Pattern
Hi, Recently I've seen SOLID principle and Repository Pattern Videos in Laracast and I want to start implementing these approaches. I've some confession about SRP.. I'll try to explain all bellow.
My system has two types of User Consider.. Normal User and Admin User. So I created an Interface
interface UserRepositoryInterface
{
public function save(array $array);
}
and created two Classes
class UserRepository implements UserRepositoryInterface
{
public function save(array $array)
{
// Save User here
}
}
and
class AdminRepository implements UserRepositoryInterface
{
public function save(array $array)
{
// Save Admin here
}
}
User and Admin has different roles and different property to store in Database which are implemented in each class. Now in my UserController if I code like
if($inputs['type']=='user'){
$repo = new UserRepository;
$repo->save($inputs);
} elseif($inputs['type']=='admin'){
$repo = new AdminRepository;
$repo->save($inputs);
}
Does this break any principle of SOLID principle or Whats the best practice to deal with such situation? Help, Collaboration and Discussion are welcome.
TIA Ariful
Please or to participate in this conversation.