Skip to content
Permalink
Browse files

Merge pull request #1 from terumichi1209/feature/user_list_and_create…

…_screen

ユーザ一覧とユーザ作成機能実装
  • Loading branch information
terumichi1209 committed Jun 30, 2019
2 parents 85a1d9f + 511df16 commit 701ed18a11eee570893d859f09fd7309acf7a333
@@ -0,0 +1,62 @@
<?php
namespace App\Entities;
use App\ValueObjects\User\Name;
use App\ValueObjects\User\Email;
use App\ValueObjects\User\Password;
class User
{
/**
* @var Name
*/
private $name;
/**
* @var Email
*/
private $email;
/**
* @var Password
*/
private $password;
/**
* UserEntity constructor.
* @param Name $name
* @param Email $email
* @param Password $password
*/
public function __construct(Name $name, Email $email, Password $password)
{
$this->name = $name;
$this->email = $email;
$this->password = $password;
}
/**
* @return string
*/
public function getName(): string
{
return $this->name->get();
}
/**
* @return string
*/
public function getEmail(): string
{
return $this->email->get();
}
/**
* @return string
*/
public function getPassword(): string
{
return $this->password->get();
}
}
@@ -0,0 +1,27 @@
<?php
namespace App\Factories;
use App\Entities\User;
use App\ValueObjects\User\Email;
use App\ValueObjects\User\Name;
use App\ValueObjects\User\Password;
class UserFactory
{
/**
* @param string $name
* @param string $email
* @param string $password
* @return User
* @throws \Exception
*/
public function make(string $name, string $email, string $password): User
{
return new User(
new Name($name),
new Email($email),
new Password($password)
);
}
}
@@ -0,0 +1,41 @@
<?php
namespace App\Http\Controllers;
use App\Http\Requests\StoreUserPost;
use App\Services\UserService;
use Illuminate\View\View;
use Illuminate\Http\RedirectResponse;
class UserController extends Controller
{
/**
* @return View
*/
public function index(): View
{
$users = (new UserService())->list();
return view('user.list', ['users' => $users]);
}
/**
* @return View
*/
public function create(): View
{
return view('user.create', ['user' => \Session::get('_old_input')]);
}
/**
* @param StoreUserPost $request
* @return RedirectResponse
*/
public function store(StoreUserPost $request): RedirectResponse
{
$user = $request->all();
if ((new UserService())->store($user)) {
return redirect('/user/')->with('success_message', trans('message.success_save'));
}
return redirect('/user/')->with('error_message', trans('message.failed_save'));
}
}
@@ -0,0 +1,58 @@
<?php
namespace App\Http\Requests;
use App\ValueObjects\User;
use Illuminate\Foundation\Http\FormRequest;
class StoreUserPost extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules(): array
{
$name_between = User\Name::MIN_LENGTH . ',' . User\Name::MAX_LENGTH;
$pass_between = User\Password::MIN_LENGTH . ',' . User\Password::MAX_LENGTH;
return [
'name' => 'required|string|between:' . $name_between,
'email' => 'required|unique:users|email',
'password' => 'required|string|between:' . $pass_between,
];
}
public function messages()
{
return [
'name.required' => trans('validation.required', ['attribute' => 'ユーザ名']),
'name.string' => trans('validation.string', ['attribute' => 'ユーザ名']),
'name.between' => trans('validation.between', [
'attribute' => 'ユーザ名',
'min' => User\Name::MIN_LENGTH,
'max' => User\Name::MAX_LENGTH
]),
'email.required' => trans('validation.required', ['attribute' => 'メールアドレス']),
'email.unique' => trans('validation.unique', ['attribute' => 'メールアドレス']),
'email.email' => trans('validation.email'),
'password.required' => trans('validation.required', ['attribute' => 'パスワード']),
'password.string' => trans('validation.string', ['attribute' => 'パスワード']),
'password.between' => trans('validation.between', [
'attribute' => 'パスワード',
'min' => User\Password::MIN_LENGTH,
'max' => User\Password::MAX_LENGTH
]),
];
}
}
@@ -0,0 +1,17 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
}
@@ -0,0 +1,47 @@
<?php
namespace App\Repositories;
use App\Entities;
use App\Factories\UserFactory;
use App\Models\User;
use \Illuminate\Database\Eloquent\Collection;
class UserRepository
{
/**
* @param array $request_params
* @return Entities\User
* @throws \Exception
*/
public function new(array $request_params): Entities\User
{
return (new UserFactory)->make(
$request_params['name'],
$request_params['email'],
$request_params['password']
);
}
/**
* @return Collection
*/
public function list(): Collection
{
return User::all();
}
/**
* @param Entities\User $user
* @return bool
*/
public function store(Entities\User $user): bool
{
return (new User([
'name' => $user->getName(),
'email' => $user->getEmail(),
'password' => $user->getPassword()
]))->save();
}
}
@@ -0,0 +1,40 @@
<?php
namespace App\Services;
use App\Repositories\UserRepository;
use Illuminate\Database\Eloquent\Collection;
class UserService
{
/** UserRepository */
private $user_repos;
public function __construct()
{
$this->user_repos = new UserRepository;
}
/**
* @return Collection
*/
public function list(): Collection
{
return $this->user_repos->list();
}
/**
* @param array $request_params
* @return bool
*/
public function store(array $request_params): bool
{
try {
$user = $this->user_repos->new($request_params);
return $this->user_repos->store($user);
} catch (\Exception $e) {
report($e);
return false;
}
}
}

This file was deleted.

@@ -0,0 +1,8 @@
<?php
namespace App\ValueObjects;
interface BaseValueObject
{
public function get();
}
@@ -0,0 +1,43 @@
<?php
namespace App\ValueObjects\User;
use App\ValueObjects\BaseValueObject;
class Email implements BaseValueObject
{
/**
* @var string
*/
private $email;
/**
* Email constructor.
* @param string $email
* @throws \Exception
*/
public function __construct(string $email)
{
if (!$this->isEmail($email)) {
throw new \Exception(trans('validation.email'));
}
$this->email = $email;
}
/**
* @return string
*/
public function get(): string
{
return $this->email;
}
/**
* @param string $email
* @return bool
*/
private function isEmail(string $email): bool
{
return \Validator::make([$email], ['email'])->passes();
}
}

0 comments on commit 701ed18

Please sign in to comment.
You can’t perform that action at this time.