Extracted an abstract class to make other objects easier to handle

This commit is contained in:
Christian Wolf 2019-06-19 16:14:52 +02:00
parent 55b44d9b6f
commit 9dbe6112dd

View File

@ -10,7 +10,42 @@ defined('_JEXEC') or die;
abstract class UserException extends Exception {} abstract class UserException extends Exception {}
class InvalidUserDataException extends UserException {} class InvalidUserDataException extends UserException {}
class ClubsHelperControllerUser abstract class AbstractClubsHelperController
{
private $fcnNames;
public function __construct()
{
$this->fcnNames = array();
$this->registerAll();
}
abstract protected function registerAll();
protected function registerFcn($fcnName, $methodName)
{
$this->fcnNames[$fcnName] = $methodName;
}
public function handle($fcnName, $data, $post)
{
if(empty($this->fcnNames[$fcnName]))
throw new Exception();
$method = $this->fcnNames[$fcnName];
$this->$method($data, $post);
}
protected function checkLen($value, $len, $msg)
{
if(strlen($value) < 5)
throw new InvalidUserDataException($msg);
}
}
class ClubsHelperControllerUser extends AbstractClubsHelperController
{ {
/** /**
* @param string $fcnName * @param string $fcnName
@ -52,13 +87,19 @@ class ClubsHelperControllerUser
return "index.php?option=com_clubs&view=part&type=user.$fcnName"; return "index.php?option=com_clubs&view=part&type=user.$fcnName";
} }
private function checkLen($value, $len, $msg)
protected function registerAll()
{ {
if(strlen($value) < 5) $this->registerFcn('name', 'editName');
throw new InvalidUserDataException($msg); $this->registerFcn('username', 'editUsername');
$this->registerFcn('address', 'editAddress');
$this->registerFcn('city', 'editCity');
$this->registerFcn('mail', 'editMail');
$this->registerFcn('phone', 'editPhone');
$this->registerFcn('mobile', 'editMobile');
} }
private function editName($user, $post) protected function editName($user, $post)
{ {
$name = trim($post->getString('value')); $name = trim($post->getString('value'));
$this->checkLen($name, 5, "Der Name muss mindestens 5 Zeichen lang sein."); $this->checkLen($name, 5, "Der Name muss mindestens 5 Zeichen lang sein.");
@ -67,11 +108,12 @@ class ClubsHelperControllerUser
$user->save(); $user->save();
} }
private function editUsername($user, $post) protected function editUsername($user, $post)
{ {
$username = $post->getString('value'); $username = $post->getString('value');
$validator = new ClubsHelperValidator(); $validator = new ClubsHelperValidator();
$res = $validator->validateUsername($user, $username); $res = $validator->validateUsername($user, $username);
$this->checkLen($username, 5, 'Der Benutzername muss mindestens 5 Zeichen lang sein.');
switch($res) switch($res)
{ {
@ -91,7 +133,7 @@ class ClubsHelperControllerUser
} }
} }
private function editAddress($user, $post) protected function editAddress($user, $post)
{ {
$address = trim($post->getString('value')); $address = trim($post->getString('value'));
$this->checkLen($address, 10, 'Die Adresse muss mindestens 10 Zeichen lang sein.'); $this->checkLen($address, 10, 'Die Adresse muss mindestens 10 Zeichen lang sein.');
@ -105,7 +147,7 @@ class ClubsHelperControllerUser
$user->save(); $user->save();
} }
private function editCity($user, $post) protected function editCity($user, $post)
{ {
$city = trim($post->getString('value')); $city = trim($post->getString('value'));
$this->checkLen($city, 4, 'Die Stadt muss aus mindestens 4 Zeichen bestehen.'); $this->checkLen($city, 4, 'Die Stadt muss aus mindestens 4 Zeichen bestehen.');
@ -114,7 +156,7 @@ class ClubsHelperControllerUser
$user->save(); $user->save();
} }
private function editMail($user, $post) protected function editMail($user, $post)
{ {
$mail = trim($post->getString('value')); $mail = trim($post->getString('value'));
$this->checkLen($mail, 8, "Die Mail-Adresse muss aus mindestens 8 Zeichen bestehen."); $this->checkLen($mail, 8, "Die Mail-Adresse muss aus mindestens 8 Zeichen bestehen.");
@ -125,7 +167,7 @@ class ClubsHelperControllerUser
$user->save(); $user->save();
} }
private function editPhone($user, $post) protected function editPhone($user, $post)
{ {
$phone = trim($post->getString('value')); $phone = trim($post->getString('value'));
@ -149,7 +191,7 @@ class ClubsHelperControllerUser
$user->save(); $user->save();
} }
private function editMobile($user, $post) protected function editMobile($user, $post)
{ {
$mobile = trim($post->getString('value')); $mobile = trim($post->getString('value'));
@ -172,6 +214,7 @@ class ClubsHelperControllerUser
$user->setMobile($mobilePlain); $user->setMobile($mobilePlain);
$user->save(); $user->save();
} }
} }
class ClubsControllerParts extends BaseController class ClubsControllerParts extends BaseController
@ -212,11 +255,13 @@ class ClubsControllerParts extends BaseController
{ {
case 'user': case 'user':
$obj = new ClubsHelperControllerUser(); $obj = new ClubsHelperControllerUser();
return $obj->parse($fcnName, $user, $post); $obj->handle($fcnName, $user, $post);
break;
default: default:
throw new Exception(); throw new Exception();
} }
return "index.php?option=com_clubs&view=part&type=$partname";
} }
} }