I am building a search function.
Speed is priority to getting the result from query so, I use native SQL.
Result is depending on user/group permission, keyword, and the option of search (name, description, all, etc).
The option is extendable so, it might be the best to use Dependency Injection, but I just can't figure out how to use it with this requirement.
I know "if" statement will work just fine. However, I am trying to follow the "single responsibility principle" and come out with the code below.
It is currently working just fine but it just doesn't looks quite right. Is there a way to convert it to Dependency Injection? and maybe you can help to explain why the code below is already good or it is just bad and give me a solution.
Interface :
interface SearchQueryInterface
{
public function searchQuery($user_id,$user_group, $keyword);
}
Classes that implements Interface :
// search by All
class SearchAll implements SearchQueryInterface{
public function searchQuery($user_id,$user_group, $keyword)
{
// results = Select query based on $user_id, $user_group and $keyword
// return query results;
}
}
// search by Name
class SearchName implements SearchQueryInterface{
public function searchQuery($user_id,$user_group, $keyword)
{
// results = Select query based on $user_id, $user_group and $keyword
// return query results;
}
}
// search by Description
class SearchDescription implements SearchQueryInterface{
public function searchQuery($user_id,$user_group, $keyword)
{
// results = Select query based on $user_id, $user_group and $keyword
// return query results;
}
}
Controller :
class SearchController extends \BaseController {
public function postSearch()
{
$user_id = 2;
$user_group = 1;
$option = Input::get('search_option');
$keyword = '%'. Input::get('search_keyword') .'%';
// get the class based on option
$search_query = App::make('Search'. $option );
$results = $search_query->searchQuery($user_id,$user_group, $keyword);
var_dump($results);
}
}