The better ways to make a helper

62 Views Asked by At

I got a method to be use frequently. Here is my code:

function dashboard_notification($arr_notification_data='')
{
    if($arr_notification_data == ''):
        return false;
    endif;

    $this->db->insert('message_notification', array(

    'dst_group'     => MESSAGE_DESTINATION_STUDENT_SPECIFIC,
    'dst_user_type' => USER_TYPE_STUDENT,
    'dst_user_id'   => $student_id,
    'alert_type'    => MESSAGE_ALERT_TYPE_EXCLAIMATION,
    'dt_expiry'     => date('Y-m-d H:i:s', time() + 14*24*60*60),
    'src_user_type' => USER_TYPE_ADMIN,
    'src_user_id'   => $this->curuser['admin_id'] ? $this->curuser['admin_id'] : 0,
    'msg_subject'   => 'New school name',
    'msg_content'   => 'Your school name was rejected, please update your school name again',
    'link_label'    => 'Update now',
    'link_url'      => 'http://url/profile/update/school/',
    'dt_added'      => timenow(),
    'dt_updated'    => timenow()
));
}

Is there any better ways to make this function as a helper? Since this function got to much variables...

2

There are 2 best solutions below

0
On

I would create a database adapter class, then create another class that extends it. In this case it could be called something like StudentDatabase and it would contain your dashboard_notification method.

If you really wanted a helper, it would look similar to this:

class StudentHelper {
    public static function dashboard_notification($curuser, $arr_notification_data='') {
        if($arr_notification_data == '') {
            return false;
        }

        // Your database object
        $db = new StudentDatabase();
        $db->insert('message_notification', array(
            'dst_group'     => MESSAGE_DESTINATION_STUDENT_SPECIFIC,
            'dst_user_type' => USER_TYPE_STUDENT,
            'dst_user_id'   => $curuser['student_id'],
            'alert_type'    => MESSAGE_ALERT_TYPE_EXCLAIMATION,
            'dt_expiry'     => date('Y-m-d H:i:s', time() + 14*24*60*60),
            'src_user_type' => USER_TYPE_ADMIN,
            'src_user_id'   => $curuser['admin_id'] ? $curuser['admin_id'] : 0,
            'msg_subject'   => 'New school name',
            'msg_content'   => 'Your school name was rejected, please update your school name again',
            'link_label'    => 'Update now',
            'link_url'      => 'http://url/profile/update/school/',
            'dt_added'      => timenow(),
            'dt_updated'    => timenow()
        ));
    }
}

Then include the class and call it like this:

StudentHelper::dashboard_notification($this->curuser, <params>);
0
On

Not much you could change you know, one thing I would though, I wouldn't hardcode the values inside of the function

$params = array(
'dst_group'     => MESSAGE_DESTINATION_STUDENT_SPECIFIC,
'dst_user_type' => USER_TYPE_STUDENT,
'dst_user_id'   => $student_id,
'alert_type'    => MESSAGE_ALERT_TYPE_EXCLAIMATION,
'dt_expiry'     => date('Y-m-d H:i:s', time() + 14*24*60*60),
'src_user_type' => USER_TYPE_ADMIN,
'src_user_id'   => $this->curuser['admin_id'] ? $this->curuser['admin_id'] : 0,
'msg_subject'   => 'New school name',
'msg_content'   => 'Your school name was rejected, please update your school name again',
'link_label'    => 'Update now',
'link_url'      => 'http://url/profile/update/school/',
'dt_added'      => timenow(),
'dt_updated'    => timenow()
);

Then I would pass that data as an argument to the function this way its reusable:

function dashboard_notification($arr_notification_data='', $params = array())
{
    if($arr_notification_data == ''):
        return false;
    endif;

    $this->db->insert('message_notification',$params);
}