Permalink
Switch branches/tags
Nothing to show
Find file Copy path
7f9ca25 Mar 17, 2017
1 contributor

Users who have contributed to this file

224 lines (186 sloc) 6.32 KB
<?php
namespace App\Http\Controllers;
use App\Models\User;
use App\Repositories\Contracts\UserRepository;
use Illuminate\Http\Request;
use App\Transformers\UserTransformer;
class UserController extends Controller
{
/**
* Instance of UserRepository
*
* @var UserRepository
*/
private $userRepository;
/**
* Instanceof UserTransformer
*
* @var UserTransformer
*/
private $userTransformer;
/**
* Constructor
*
* @param UserRepository $userRepository
* @param UserTransformer $userTransformer
*/
public function __construct(UserRepository $userRepository, UserTransformer $userTransformer)
{
$this->userRepository = $userRepository;
$this->userTransformer = $userTransformer;
parent::__construct();
}
/**
* Display a listing of the resource.
*
* @param Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function index(Request $request)
{
$users = $this->userRepository->findBy($request->all());
return $this->respondWithCollection($users, $this->userTransformer);
}
/**
* Display the specified resource.
*
* @param $id
* @return \Illuminate\Http\JsonResponse|string
*/
public function show($id)
{
$user = $this->userRepository->findOne($id);
if (!$user instanceof User) {
return $this->sendNotFoundResponse("The user with id {$id} doesn't exist");
}
// Authorization
$this->authorize('show', $user);
return $this->respondWithItem($user, $this->userTransformer);
}
/**
* Store a newly created resource in storage.
*
* @param Request $request
* @return \Illuminate\Http\JsonResponse|string
*/
public function store(Request $request)
{
// Validation
$validatorResponse = $this->validateRequest($request, $this->storeRequestValidationRules($request));
// Send failed response if validation fails
if ($validatorResponse !== true) {
return $this->sendInvalidFieldResponse($validatorResponse);
}
$user = $this->userRepository->save($request->all());
if (!$user instanceof User) {
return $this->sendCustomResponse(500, 'Error occurred on creating User');
}
return $this->setStatusCode(201)->respondWithItem($user, $this->userTransformer);
}
/**
* Update the specified resource in storage.
*
* @param Request $request
* @param $id
* @return \Illuminate\Http\JsonResponse
*/
public function update(Request $request, $id)
{
// Validation
$validatorResponse = $this->validateRequest($request, $this->updateRequestValidationRules($request));
// Send failed response if validation fails
if ($validatorResponse !== true) {
return $this->sendInvalidFieldResponse($validatorResponse);
}
$user = $this->userRepository->findOne($id);
if (!$user instanceof User) {
return $this->sendNotFoundResponse("The user with id {$id} doesn't exist");
}
// Authorization
$this->authorize('update', $user);
$user = $this->userRepository->update($user, $request->all());
return $this->respondWithItem($user, $this->userTransformer);
}
/**
* Remove the specified resource from storage.
*
* @param $id
* @return \Illuminate\Http\JsonResponse|string
*/
public function destroy($id)
{
$user = $this->userRepository->findOne($id);
if (!$user instanceof User) {
return $this->sendNotFoundResponse("The user with id {$id} doesn't exist");
}
// Authorization
$this->authorize('destroy', $user);
$this->userRepository->delete($user);
return response()->json(null, 204);
}
/**
* Store Request Validation Rules
*
* @param Request $request
* @return array
*/
private function storeRequestValidationRules(Request $request)
{
$rules = [
'email' => 'email|required|unique:users',
'firstName' => 'required|max:100',
'middleName' => 'max:50',
'lastName' => 'required|max:100',
'username' => 'max:50',
'address' => 'max:255',
'zipCode' => 'max:10',
'phone' => 'max:20',
'mobile' => 'max:20',
'city' => 'max:100',
'state' => 'max:100',
'country' => 'max:100',
'password' => 'min:5'
];
$requestUser = $request->user();
// Only admin user can set admin role.
if ($requestUser instanceof User && $requestUser->role === User::ADMIN_ROLE) {
$rules['role'] = 'in:BASIC_USER,ADMIN_USER';
} else {
$rules['role'] = 'in:BASIC_USER';
}
return $rules;
}
/**
* Update Request validation Rules
*
* @param Request $request
* @return array
*/
private function updateRequestValidationRules(Request $request)
{
$userId = $request->segment(2);
$rules = [
'email' => 'email|unique:users,email,'. $userId,
'firstName' => 'max:100',
'middleName' => 'max:50',
'lastName' => 'max:100',
'username' => 'max:50',
'address' => 'max:255',
'zipCode' => 'max:10',
'phone' => 'max:20',
'mobile' => 'max:20',
'city' => 'max:100',
'state' => 'max:100',
'country' => 'max:100',
'password' => 'min:5'
];
$requestUser = $request->user();
// Only admin user can update admin role.
if ($requestUser instanceof User && $requestUser->role === User::ADMIN_ROLE) {
$rules['role'] = 'in:BASIC_USER,ADMIN_USER';
} else {
$rules['role'] = 'in:BASIC_USER';
}
return $rules;
}
}