Better structure for parts controller

This commit is contained in:
Christian Wolf 2019-06-18 17:09:52 +02:00
parent 7f019dd740
commit 20c5c9d147

View File

@ -10,6 +10,33 @@ defined('_JEXEC') or die;
abstract class UserException extends Exception {}
class InvalidUserDataException extends UserException {}
class ClubsHelperControllerUser
{
public function parse($fcnName, $user, $post)
{
switch($fcnName)
{
case 'name':
return $this->editName($user, $post);
default:
throw new Exception();
}
}
public function editName($user, $post)
{
$name = $post->getString('name');
if(strlen($name) < 5)
throw new InvalidUserDataException("Der Name muss mindestens 5 Zeichen lang sein.");
$user->setName($name);
$user->save();
return 'index.php?option=com_clubs&view=mypage&layout=parts&type=name';
}
}
class ClubsControllerParts extends BaseController
{
@ -29,7 +56,6 @@ class ClubsControllerParts extends BaseController
{
echo new JsonResponse($e);
}
}
/**
@ -38,22 +64,22 @@ class ClubsControllerParts extends BaseController
*/
private function callMethod($user, $post)
{
switch($post->getCmd('partname'))
{
case 'user.name':
return $this->editUserName($user, $post);
}
}
$partname = $post->getCmd('partname');
private function editUserName($user, $post)
{
$name = $post->getString('name');
if(strlen($name) < 5)
throw new InvalidUserDataException("Der Name muss mindestens 5 Zeichen lang sein.");
if(! preg_match('/.*\..*/', $partname))
throw new Exception();
$user->setName($name);
$user->save();
return 'index.php?option=com_clubs&view=mypage&layout=parts&type=name';
list($objname, $fcnName) = explode('.', $partname, 2);
switch($objname)
{
case 'user':
$obj = new ClubsHelperControllerUser();
return $obj->parse($fcnName, $user, $post);
default:
throw new Exception();
}
}
}