I am working on one project which redirects user to custom page after login, registration etc. FYI, the custom page can be configured as per customer from admin settings.
I am trying to follow some design-pattern guidelines to achieve this. So far I have designed my classes as below
File: RedirectInterface.php
interface RedirectInterface
{
public function getUrl($customerId = null);
}
File: LoginRedirect.php
class LoginRedirect implements RedirectInterface
{
public function getUrl($customerId = null)
{
// do some business logic here to get url
$url = '/account/some-customer';
return $url;
}
}
File: RegisterRedirect.php
class RegisterRedirect implements RedirectInterface
{
public function getUrl($customerId = null)
{
// do some business logic here to get url
$url = '/welcome/some-customer';
return $url;
}
}
File: RedirectFactory - Creational design pattern (static factory)
class RedirectFactory
{
public static function build($type, $customerId)
{
if ($type == 'login') {
return new LoginRedirect($customerId);
} else if ($type == 'register') {
return new RegisterRedirect($customerId);
}
throw new InvalidArgumentException ('Invalid redirect type.');
}
}
Usage:
// 1. Usage: Somewhere in customer registration code
$redirectUrl = RedirectFactory::build('register', 102)->getUrl();
header('Location: ' . $redirectUrl);
// 2. Usage: Somewhere in customer login code
$redirectUrl = RedirectFactory::build('login', 102)->getUrl();
header('Location: ' . $redirectUrl);
So my question here is:
- What is the appropriate design pattern used in this case? like Adapter, Static Factory etc.
- How would you refactor this sample with more clincial finish?
Thanks