customerModel = new CustomerModel(); $this->requestMethod = $this->getServerRequestMethod(); $this->arrQueryStringParams = $this->getQueryStringParams(); } public function processAction() { $this->strErrorDesc = ''; try { switch($this->action) { case "select": $response = $this->selectAction(); break; case "insert": $response = $this->insertCustomer(); break; case "update": $response = $this->updateCustomer(); break; case "delete": $response = $this->deleteCustomer(); break; default: $strErrorDesc = 'Controller Method not supported for processAction: ' . $this->action; $strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity'; break; } $responseData = json_encode($response); } catch (Error $e) { $this->strErrorDesc = $e->getMessage().' Something went wrong in processAction! Please contact support.'; $this->strErrorHeader = 'HTTP/1.1 500 Internal Server Error'; } // send output if (!$strErrorDesc) { $this->sendOutput( $responseData, array('Content-Type: application/json', 'HTTP/1.1 200 OK') ); } else { $this->sendOutput(json_encode(array('error' => $strErrorDesc)), array('Content-Type: application/json', $strErrorHeader) ); } } private function selectAction(){ if ($this->checkRequestType('GET') == 'false') { $this->strErrorDesc = 'Request Method not supported for processAction'; $this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Request'; return; } if (isset($this->arrQueryStringParams['customer_id'])) { $response = $this->selectByIdAction(); } else { $this->customerModel->limit = 10; if (isset($this->arrQueryStringParams['limit'])) { $this->customerModel->limit = $this->arrQueryStringParams['limit']; } $response = $this->customerModel->findAllCustomers(); unset($this->customerModel->limit); } return $response; } private function selectByIdAction(){ if (isset($this->arrQueryStringParams['customer_id'])) { $this->customerModel->customerId = $this->arrQueryStringParams['customer_id']; $response = $this->customerModel->findByCustomerId(); unset($this->customerModel->customerId); } else { return $this->notFoundResponse(); } return $response; } private function insertCustomer() { if ($this->checkRequestType('POST') == 'false') { $this->strErrorDesc = 'Request Method not supported for processAction'; $this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Request'; return; } // reads the raw POST data and returns it as a string. $jsonPayload = (array) json_decode(file_get_contents('php://input'), TRUE); if (! $this->validatePerson($jsonPayload)) { return $this->unprocessableEntityResponse(); } $response = $this->customerModel->insertCustomer($jsonPayload); return $response; } private function updateCustomer() { if ($this->checkRequestType('PUT') == 'false') { $this->strErrorDesc = 'Request Method not supported for processAction'; $this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Request'; return; } $jsonPayload = file_get_contents('php://input'); $input = json_decode($jsonPayload); if (! $this->validatePerson($input)) { return $this->unprocessableEntityResponse(); } if ($input->customer_id != null) { $this->customerModel->customerId = $input->customer_id; $result = $this->customerModel->findByCustomerId(); if (! $result) { return $this->notFoundResponse(); } $response = $this->customerModel->updateCustomer($input); unset($this->customerModel->customerId); } else { return $this->notFoundResponse(); } return $response; } private function deleteCustomer() { if ($this->checkRequestType('DELETE') == 'false') { $this->strErrorDesc = 'Request Method not supported for processAction'; $this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Request'; return; } if (isset($this->arrQueryStringParams['customer_id'])) { $this->customerModel->customerId = $this->arrQueryStringParams['customer_id']; $result = $this->customerModel->findByCustomerId(); if (! $result) { return $this->notFoundResponse(); } $response = $this->customerModel->deleteCustomer(); unset($this->customerModel->customerId); } else { return $this->notFoundResponse(); } return $response; } private function checkRequestType($request) { $response = 'false'; if (strtoupper($this->requestMethod) == $request) { $response = 'true'; } return $response; } private function validatePerson($input) { $validtion = false; if($input->customer_name_first != null){ $validtion = true; if($input->customer_name_last == null) { $validtion = false; } } return $validtion; } private function unprocessableEntityResponse() { $response['status_code_header'] = 'HTTP/1.1 422 Unprocessable Payload'; $response['body'] = json_encode([ 'error' => 'Invalid input' ]); $this->strErrorDesc = 'Unprocessable Payload'; $this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Payload'; return $response; } private function notFoundResponse() { $response['status_code_header'] = 'HTTP/1.1 404 Entity Not Found'; $response['body'] = null; $this->strErrorDesc = 'Request Entity Not Found'; $this->strErrorHeader = 'HTTP/1.1 422 Entity Not Found'; return $response; } }