/** * @param Request $request * @return array */ public function getUser(Request $request) { $return = []; try { $userModel = new User(); $user = $userModel->findByPhone("+1" . $request->route('phone')); // format data based on user role switch ($user->role) { case 'expert': // fetch expert's responses and associated submissions $responseModel = new Response(); $responses = $responseModel->where('user_id', $user->id)->with('submission.user')->get(); $data = $user->toArray(); $data['photos'] = $responses; $return = $this->formatExpertUser($data); break; case 'enthusiast': // fetch enthusiast's submissions and associated responses $submissionModel = new Submission(); $submissions = $submissionModel->where('user_id', $user->id)->with('response.user')->get(); $data = $user->toArray(); $data['photos'] = $submissions; $return = $this->formatEnthusiastUser($data); break; } } catch (\Exception $e) { // return some dummy data $return = $this->dummyResponse(); } return $this->respondOK($return); }
/** * Validate that an expert's text is a response to an enthusiast photo. * * @param IncomingTextObject $incomingText * @return array */ private function validateText(IncomingTextObject $incomingText) { // break text into parts $text = explode(' ', $incomingText->getBody()); // check if first string is an integer and an enthusiast user id if (!is_numeric($text[0])) { // not an integer return ['valid' => FALSE, 'message' => "Please respond in the format: \"12345 Yes\", where 12345 is the number you received with the photo."]; } elseif (!($submission = $this->submissionModel->find($text[0]))) { // submission not found from integer provided return ['valid' => FALSE, 'message' => "We couldn't find that photo reference number!"]; } // validate second part of text if (strtolower($text[1]) !== 'yes' and strtolower($text[1]) !== 'no') { return ['valid' => FALSE, 'message' => "Please respond in the format: \"12345 Yes\" or \"12345 No\", where 12345 is the number you received with the photo."]; } return ['valid' => TRUE, 'submission' => $submission, 'answer' => strtolower($text[1])]; }
/** * @param IncomingTextObject $incomingText * @param User $enthusiast * @return Submission */ public function saveSubmission(IncomingTextObject $incomingText, User $enthusiast) { // extract media type of photo $mediaTypeArray = explode("/", $incomingText->getMediaType(0)); $mediaType = $mediaTypeArray[count($mediaTypeArray) - 1]; // build path for photo $date = Carbon::now(); $path = '/user_submissions/' . $enthusiast->id . "/" . $date->toDateString() . '/' . $date->format('His') . '.' . $mediaType; // send photo to s3 $s3 = Storage::disk('s3'); $s3->put($path, file_get_contents($incomingText->getMediaUrl(0))); // build photo url $photo_url = 'https://s3.amazonaws.com/' . config('filesystems.disks.s3.bucket') . $path; // save submission $submission = $this->submission->create(['user_id' => $enthusiast->id, 'photo_url' => $photo_url]); // return saved submission return $submission; }