/** * register * * @param Data $user * * @return Data */ public function register(Data $user) { $password = new Password(); $user->password = $password->create($user->password); unset($user->password2); $user = User::save($user); return $user; }
/** * register * * @param array $user * * @throws \Exception * @return bool */ public function register($user) { $user = new Data($user); if ($user->password != $user->password2) { throw new \Exception('Password not match.'); } $password = new Password(); $user->password = $password->create($user->password); unset($user->password2); User::save($user); return true; }
/** * Execute the controller. * * @return mixed Return executed result. * * @throws \LogicException * @throws \RuntimeException */ public function execute() { $user = User::get(); try { $user->image = "0"; User::save($user); } catch (\Exception $e) { $response = new Response(); $response->setBody(json_encode(['error' => $e->getMessage()])); $response->setMimeType('text/json'); $response->respond(); exit; } $return = new Registry(); $return['success'] = true; $return['image'] = UserHelper::getAvatar($user->id, 650); $response = new Response(); $response->setBody((string) $return); $response->setMimeType('text/json'); $response->respond(); exit; }
/** * Execute the controller. * * @return mixed Return executed result. * * @throws \LogicException * @throws \RuntimeException */ public function execute() { $files = $this->input->files; $field = $this->input->get('field', 'file'); $user = User::get(); try { $src = $files->getByPath($field . '.tmp_name', null, InputFilter::STRING); $name = $files->getByPath($field . '.name', null, InputFilter::STRING); if (!$src) { throw new \Exception('File not upload'); } $ext = pathinfo($name, PATHINFO_EXTENSION); $src = Thumb::createThumb($src); $dest = sprintf('user/%s/%s.%s', sha1('user-profile-' . $user->id), md5('user-profile-' . $user->id), $ext); $result = S3Helper::put($src, $dest); File::delete($src); if (!$result) { throw new \Exception('Upload fail.'); } } catch (\Exception $e) { $response = new Response(); $response->setBody(json_encode(['error' => $e->getMessage()])); $response->setMimeType('text/json'); $response->respond(); exit; } $return = new Registry(); $return['filename'] = 'https://windspeaker.s3.amazonaws.com/' . $dest; $return['file'] = 'https://windspeaker.s3.amazonaws.com/' . $dest; $user->image = $return['filename']; User::save($user); $response = new Response(); $response->setBody((string) $return); $response->setMimeType('text/json'); $response->respond(); exit; }