We help IT Professionals succeed at work.
Get Started

Help refactoring PHP function

Eddie Shipman
on
74 Views
Last Modified: 2015-07-14
I have the following PHP function from an ExpressionEngine plugin and having a little difficulty figuring out how to refactor this to be more efficient, and shorter.

    public function checkuser() {
        $first_name = $this->EE->input->post("FirstName");
        $last_name  = $this->EE->input->post("LastName");
        $DOB        = $this->EE->input->post("DOB");
        $email      = $this->EE->input->post("Email");
        $this->return_data['error'] = '';
        
        $senddata = array("SearchBy"       =>  "FIND",
                          "UserID"         =>  null,
                          "Username"       =>  null,
                          "Sha1Password"   =>  null,
                          "FirstName"      =>  $first_name,
                          "LastName"       =>  $last_name,
                          "DOB"            =>  $DOB,
                          "Address"        =>  null,
                          "Zip"            =>  null,
                          "Phone"          =>  null,
                          "Email"          =>  $email 
        );

        $url = $this->scic_db_url.'SC2/FindUser';
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
        curl_setopt($ch, CURLOPT_USERAGENT, 'HAC');
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_TIMEOUT, 120);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($senddata));
        $re = curl_exec($ch);
        $curl_err = curl_error($ch);
        curl_close($ch);
        $part = json_decode($re, true);

        if($curl_err) return $curl_err;
        if(!array_key_exists("Exception", $part)) {
            // User exists. check email for the $part to see if it matches one passed?
            if($part["Primary_Email_Address"] !== $email) {
                $this->return_data['error'] = "* Your account information is in our database, however, it contains a different email address.";
            } else {
                $this->return_data['error'] = "* An Account with this email has already been created";
            }
        } elseif(strpos($part["Message"],"No records found") > -1) {
            // Person with that information doesn't exist but let's check for the email addy once again, for safeties sake
            $senddata = array("SearchBy"       =>  "EMAIL",
                              "UserID"         =>  null,
                              "Username"       =>  null,
                              "Sha1Password"   =>  null,
                              "FirstName"      =>  null,
                              "LastName"       =>  null,
                              "DOB"            =>  null,
                              "Address"        =>  null,
                              "Zip"            =>  null,
                              "Phone"          =>  null,
                              "Email"          =>  $email // ONLY REQUIRED parameter for EMAIL searches
            );

            $url = $this->scic_db_url.'SC2/FindUser';
            $ch = curl_init();

            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
            curl_setopt($ch, CURLOPT_USERAGENT, 'HAC');
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
            curl_setopt($ch, CURLOPT_TIMEOUT, 120);
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($senddata));
            $re = curl_exec($ch);
            $curl_err = curl_error($ch);
            curl_close($ch);
            $part = json_decode($re, true);
            if($part["Primary_Email_Address"] === $email) {
                $this->return_data['error'] = "* An Account with this email has already been created";
            }
        } else {
            $this->return_data['success'] = true;
        }
        return json_encode($this->return_data);
    }

Open in new window


I'd preferably like to have a function that I could pass the method, "FIND" or "EMAIL" and the arguments and have it return either the error message or success.
Comment
Watch Question
CERTIFIED EXPERT
Expert of the Year 2008
Top Expert 2008
Commented:
This problem has been solved!
Unlock 1 Answer and 1 Comment.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE