Laravel separate functions to service class - laravel

I am refactoring my application according to this article:
https://laravel-news.com/controller-refactor
I had all logic in my controllers so it seems like a good idea to do this. But now I have some struggles with the update function.
class CategoryController extends Controller
{
/**
* Display a listing of the resource.
*
* #param Request $request
* #return JsonResponse
*/
public function index(Request $request): JsonResponse
{
$categories = Category::where('created_by', $request->company->id)->orderBy('order')->get();
return response()->json($categories);
}
/**
* Store a newly created category
*
* #param StoreCategoryRequest $request
* #param CategoryService $categoryService
* #return JsonResponse
*/
public function create(StoreCategoryRequest $request, CategoryService $categoryService): JsonResponse
{
$category = $categoryService->createCategory($request);
if ($category) {
return response()->json(['success' => true, 'message' => 'api.category.save.success']);
}
return response()->json(['success' => false, 'message' => 'api.category.save.failed']);
}
/**
* Update the specified resource in storage.
*
* #param StoreCategoryRequest $request
* #param Category $category
* #param CategoryService $categoryService
* #return JsonResponse
*/
public function update(StoreCategoryRequest $request, Category $category, CategoryService $categoryService): JsonResponse
{
try {
$result = $categoryService->updateCategory($request, $category);
if ($result) {
return response()->json(['success' => true, 'message' => 'api.category.update.success']);
}
return response()->json(['success' => false, 'message' => 'api.category.update.failed']);
} catch (\Exception $e) {
return response()->json(['success' => false, 'message' => 'api.category.update.failed']);
}
}
}
And the route:
Route::put('category/{category}', [CategoryController::class, 'update']);
Laravel is getting the category based on the id, but I don't know how to handle this correctly in my controller. I autoload the CategoryService there, so that I can use the update function. After that I give the actual category as a property to that service, I also don't know if handling the exceptions like this is the 'best way'.
class CategoryService
{
public function createCategory(Request $request): bool {
$category = new Category();
$category->fill($request->all());
$category->created_by = $request->company->id;
return $category->save();
}
/**
* #throws \Exception
*/
public function updateCategory(Request $request, Category $category): bool {
if($this->isOwnerOfCategory($category, $request->company)) {
$category->fill($request->all());
$category->created_by = $request->company->id;
return $category->save();
}
throw new \Exception('Not the owner of the category');
}
private function isOwnerOfCategory(Category $category, Company $company): bool
{
return $category->created_by === $company->id;
}
}
The create function/ flow feels good. But the update function feels like properties are coming from everywhere and the code is a lot less readable. Are there any suggestions to improve this?

Related

Laravel Unit Testing - Controllers

I am quite new to Laravel and have been reading the documentation on testing, however I am not sure how I would go about Unit Testing the Controller I have posted below. Any advice on how I would go about this is much appreciated.
The controller below is the CRUD controller I created for Booking Forms.
class BookingFormsController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$bookingforms = BookingForm::orderBy('surgeryDate', 'asc')->paginate(5);
return view('bookingforms.index')->with('bookingforms', $bookingforms);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('bookingforms.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $booking)
{
$this->validate($booking, [
'requestID' => 'required',
'patientID' => 'required',
'patientForename' => 'required',
'patientSurname'=> 'required',
'patientSex' => 'required',
'patientDOB' => 'required',
'surgeryType' => 'required',
'surgeryDate' => 'required',
'performingSurgeon' => 'required',
'TheatreRoomID' => 'required',
'patientUrgency' => 'required',
'patientNotes' => 'required',
'bloodGroup' => 'required'
]);
// Create new Booking Form
$bookingform = new Bookingform;
$bookingform->requestID = $booking->input('requestID');
$bookingform->bookingID = $booking->input('bookingID');
$bookingform->patientID = $booking->input('patientID');
$bookingform->patientForename = $booking->input('patientForename');
$bookingform->patientSurname = $booking->input('patientSurname');
$bookingform->patientSex = $booking->input('patientSex');
$bookingform->patientDOB = $booking->input('patientDOB');
$bookingform->surgeryType = $booking->input('surgeryType');
$bookingform->surgeryDate = $booking->input('surgeryDate');
$bookingform->performingSurgeon = $booking->input('performingSurgeon');
$bookingform->TheatreRoomID = $booking->input('TheatreRoomID');
$bookingform->patientUrgency = $booking->input('patientUrgency');
$bookingform->patientNotes = $booking->input('patientNotes');
$bookingform->bloodGroup = $booking->input('bloodGroup');
$bookingform->user_id = auth()->user()->id;
//Save Booking form
$bookingform->save();
//redirect
return redirect('/bookingforms')->with('success', 'Booking Submitted');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($bookingID)
{
$bookingform = BookingForm::find($bookingID);
return view('bookingforms.show')->with('bookingform', $bookingform);
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($bookingID)
{
$bookingform = BookingForm::find($bookingID);
//check for correct user_id
if(auth()->user()->id !==$bookingform->user_id){
return redirect('/bookingforms')->with('danger', 'This is not your booking, please contact the Booker.');
}
return view('bookingforms.edit')->with('bookingform', $bookingform);
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $booking, $bookingID)
{
$this->validate($booking, [
'patientID' => 'required',
'patientForename' => 'required',
'patientSurname'=> 'required',
'patientSex' => 'required',
'patientDOB' => 'required',
'surgeryType' => 'required',
'surgeryDate' => 'required',
'performingSurgeon' => 'required',
'TheatreRoomID' => 'required',
'patientUrgency' => 'required',
'patientNotes' => 'required',
'bloodGroup' => 'required'
]);
// Create new Booking Form
$bookingform = Bookingform::find($bookingID);
$bookingform->bookingID = $booking->input('bookingID');
$bookingform->patientID = $booking->input('patientID');
$bookingform->patientForename = $booking->input('patientForename');
$bookingform->patientSurname = $booking->input('patientSurname');
$bookingform->patientSex = $booking->input('patientSex');
$bookingform->patientDOB = $booking->input('patientDOB');
$bookingform->surgeryType = $booking->input('surgeryType');
$bookingform->surgeryDate = $booking->input('surgeryDate');
$bookingform->performingSurgeon = $booking->input('performingSurgeon');
$bookingform->TheatreRoomID = $booking->input('TheatreRoomID');
$bookingform->patientUrgency = $booking->input('patientUrgency');
$bookingform->patientNotes = $booking->input('patientNotes');
$bookingform->bloodGroup = $booking->input('bloodGroup');
$bookingform->user_id = auth()->user()->id;
//Save Booking form
$bookingform->save();
//redirect
return redirect('/bookingforms')->with('success', 'Booking Updated');
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($bookingID)
{
$bookingform = Bookingform::find($bookingID);
if(auth()->user()->id !==$bookingform->user_id){
return redirect('/bookingforms')->with('danger', 'This is not your booking, please contact the Booker.');
}
$bookingform->delete();
return redirect('/bookingforms')->with('success', 'Booking Removed');
}
A simple example:
class ExampleTest extends TestCase
{
public function testBookingFormsIndex()
{
$response = $this->get('index');
$response->assertStatus('200');
}
public function testBookingFormsCreate()
{
$response = $this->get('create');
$response->assertStatus('200');
}
}
Like i said, the above is a basic example that is based on the example from the HTTP Test documentation from laravel.
More information can be found here:
https://laravel.com/docs/7.x/http-tests
I would also recommend using the laravel Requests to validate your form inputs, this keeps your controller clean and places the code where is belongs.
More information on this topic can be found here: https://laravel.com/docs/7.x/validation#creating-form-requests
The controller you wrote is a bit hard to test, as it is tightly coupled to the Eloquent model. You would better decouple it by adding a repository layer and inject it into your controller.
BTW: you can use fillable attributes to avoid writing a lot of code just to fill the attributes of your BookingForm
Now for example you may do the following:
Create A BookingFormRepository interface:
interface BookingFormRepository
{
public function all();
public function create(array $attributes);
// etc ....
}
Create an implementation of the BookingFormRepository:
class BookingFormRepositoryImpl implements BookingRepository
{
public function all()
{
return BookingForm::all();
}
public function create(array $attributes)
{
// Use fillable attributes for better readability
$record = BookingForm::create($attributes);
return $record;
}
// Implement other methods ....
}
In the AppServiceProvider in the register method bind your implementation:
App::bind(BookingFormRepository::class, BookingFormRepositoryImpl::class);
Then in your controller, inject the BookingRepository interface:
class BookingFormController extends Controller {
private $bookingFormRepository;
public function __construct(BookingFormRepository $bookingRepo)
{
$this->bookingFormRepository = $bookingRepo;
}
public function index()
{
$bookings = $this->bookingFormRepository->all();
return view('bookingform.index', $bookings);
}
// .. other methods ... like `store`
}
Now the controller will be easy to test, just mock the BookingRepository and make call assertions on it:
class BookingFormControllerTest extends TestCase
{
public function testIndexBookingForm()
{
// Arrange
$repository = Mockery::mock('BookingRepository');
$repository->shouldReceive('all')->once()
->andReturn(['foobar']);
App::instance('BookingRepository', $repository);
// Act, Replace with your right url ...
$this->get('bookings');
// Assert
$this->assertResponseOk();
$this->assertViewHas('bookingforms.index', ['foobar']);
}
}
I recommand reading the Taylor Otwell book "Laravel from aprentice to Artisan".

How to use 2 attributes in Validation Rule Class Laravel

How can I use two attributes in the Validation Rule of Laravel. This is my function where I have 2 variables groupid and grouppassword. Currently only groupid is taken.
public function groupPasswordValidationReal(Request $request){
$groupid = $request->input('groupid');
$grouppassword = $request->input('grouppassword');
$validator = \Validator::make($request->all(), [
'groupid' => [new ValidRequest] //How to pass grouppassword in this function???
]);
return redirect('home')->with('success', 'Request is send!');
}
How can I pass both to my Validation Class? Here you can see the functions in the Validation Class.
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
use App\Group;
class ValidTest implements Rule
{
public function passes($attribute, $value)
{
//$value is groupid
$validPassword = Group::where([['idgroups', $value],['group_password', /*Here I need grouppassword*/]])->first();
if($validPassword){
return true;
}else{
return false;
}
}
public function message()
{
return 'Wrong Password!';
}
}
Add property and constructor to your ValidTest class. Pass the required value as an argument to the new object.
ValidTest.php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
use App\Group;
class ValidTest implements Rule
{
/**
* The group password.
*
* #var string
*/
public $groupPassword;
/**
* Create a new rule instance.
*
* #param \App\Source $source
* #param string $branch
* #return void
*/
public function __construct($groupPassword)
{
$this->groupPassword = $groupPassword;
}
public function passes($attribute, $value)
{
//$value is groupid
//$this->groupPassword is group_password
$validPassword = Group::where([['idgroups', $value],['group_password', $this->groupPassword]])->first();
if($validPassword){
return true;
}else{
return false;
}
}
public function message()
{
return 'Wrong Password!';
}
}
Controller
public function groupPasswordValidationReal(Request $request){
$groupid = $request->input('groupid');
$grouppassword = $request->input('grouppassword');
$validator = \Validator::make($request->all(), [
'groupid' => new ValidTest($grouppassword)
]);
return redirect('home')->with('success', 'Request is send!');
}
Source : custom-validation-rules-in-laravel-5-5
Note : This is not a tested solution.
Use the Rule constructor like
class ValidUser implements Rule
{
private $grouppassword;
/**
* Create a new rule instance.
*
* #return void
*/
public function __construct($grouppassword)
{
$this->grouppassword = $grouppassword;
}
/**
* Determine if the validation rule passes.
*
* #param string $attribute
* #param mixed $value
* #return bool
*/
public function passes($attribute, $value)
{
// $this->grouppassword
// $groupid = $value
// Do your logic...
return true;
}
/**
* Get the validation error message.
*
* #return string
*/
public function message()
{
return 'The :attribute must something...';
}
}
Usage
Route::post('/user', function (Request $request) {
// Parameter validation
$validator = Validator::make($request->all(), [
'groupid' => 'required',
'grouppassword' => ['required', new ValidUser($request->groupid), ]
]);
if ($validator->fails())
return response()->json(['error' => $validator->errors()], 422);
});
I think you could use a closure there. So something like this:
$validator = \Validator::make($request->all(), function() use ($groupid, $grouppassword) {
$validPassword = Group::where([['idgroups', $value],['group_password', $grouppassword]])->first();
if($validPassword){
return true;
}else{
return false;
}
}
I haven't run it though.
If you are calling the validation from a Request class, you can access the extra value as e.g.
request()->validate([
'groupid' => [new ValidTest(request()->input('grouppassword'))]
]);

Unable to overwrite authorized method in policy

I want to respond with a custom message when authorization fails.
I've overwritten the method in the Policy class but it does not return the custom message.
Policy:
class PostPolicy
{
use HandlesAuthorization;
/**
* Determine if user can view post
* #param User $user
* #param Post $post
* #return bool
*/
public function view(User $user, Post $post)
{
return $user
->posts()
->where('post_id', $post->id)
->exists();
}
/**
* [deny description]
* #return [type] [description]
*/
protected function deny()
{
return response()->json([
'message' => 'My custom unauthorized message'
], 401);
}
}
Implementing in PostController:
...
public function show(Post $post)
{
$this->authorize('view', $post);
...
}
The response still returns whats defined in the HandlesAuthorization trait, i.e.:
protected function deny($message = 'This action is unauthorized.')
{
throw new AuthorizationException($message);
}
You can simply add this code inside the AuthorizationException.php
/**
* Render the exception into an HTTP response.
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function render(Request $request)
{
if ($request->is('api/*')) {
$response = [
'message' => $this->message,
'status' => 403,
];
return response()->json($response, 403);
}
}

Trying to get property of non-object. Error in laravel

I'm kinda new at laravel and need your help.
I have this IR model :
class IR extends Model
{
//
protected $fillable=['irnum','date','subject','cause','facts','as','at','rec','user_id'];
protected $casts=['user_id'=>'int'];
public function user()
{
return $this->belongsTo(user::class);
}
public static $rules =array (
'date'=>'required',
'status'=>'required|min:10',
'cause' => 'required|min:10',
'facts' => 'required|min:10',
'ir-as' => 'required|min:10',
'rec' => 'required|min:10',
'ir-at' => 'required|min:10',
);
}
and route:
Route::group(['middleware' => ['web']], function () {
Route::get('/', function () {
return view('welcome');
})->middleware('guest');
Route::resource('tasks','TaskController');
Route::get('ir',function ()
{
return View::make('tasks/ir');
});
Route::resource('irs','IRController');
Route::auth();
});
and this is my controller :
class IRController extends Controller
{
/**
* The task repository instance.
*
* #var TaskRepository
*/
protected $irs;
/**
* Create a new controller instance.
*
* #param TaskRepository $tasks
* #return void
*/
public function __construct(IRRepository $irs)
{
$this->middleware('auth');
$this->irs = $irs;
}
/**
* Display a list of all of the user's task.
*
* #param Request $request
* #return Response
*/
public function index(Request $request)
{
return view('tasks.ir',[
'irs' => $this->irs->forUser($request->user()),
]);
}
/**
* Create a new task.
*
* #param Request $request
* #return Response
*/
public function create()
{
return View::make('irs.create');
}
public function store(Request $request)
{
$request->user_id=Auth::user()->id;
$input =$request->all();
$validation=Validator::make($input, IR::$rules);
if($validation->passes())
{
IR::create($input);
return Redirect::route('tasks.ir');
}
return Redirect::route('tasks.ir')
->withInput()
->withErrors($validation)
->with('message','There were validation errors.');
}
/**
* Destroy the given task.
*
* #param Request $request
* #param Task $task
* #return Response
*/
public function destroy(Request $request, IR $irs)
{
}
}
I really dont know what causes to throw this error.
Error throws when i add Incident report.
Pls help.
New at laravel
You're saying you get an error when you're trying to add an incident report or IR, so I assume problem is in a store() action.
I can see only one potential candidate for this error in a store() action:
Auth::user()->id;
Add dd(Auth::user()); before this clause and if it will output null, use check() method, which checks if any user is authenticated:
if (Auth::check()) {
Auth::user->id;
}

Laravel 5 FormRequest validator with multiple scenarios

I would like to ask how should I handle validation on multiple scenarios using FormRequest in L5? I know and I was told that I can create saparate FormRequest files to handle different validations but it is very redundant and also noted that I would need to inject it into the controller manually using the use FormRequest; keyword. What did previously in L4.2 is that I can define a new function inside my customValidator.php which then being called during controller validation via trycatch and then the data is being validated by service using the below implementation.
class somethingFormValidator extends \Core\Validators\LaravelValidator
{
protected $rules = array(
'title' => 'required',
'fullname' => 'required',
// and many more
);
public function scenario($scene)
{
switch ($scene) {
case 'update':
$this->rules = array(
'title' => 'required',
'fullname' => 'required',
// and other update validated inputs
break;
}
return $this;
}
}
Which then in my LaravelValidator.php
<?php namespace Core\Validators;
use Validator;
abstract class LaravelValidator {
/**
* Validator
*
* #var \Illuminate\Validation\Factory
*/
protected $validator;
/**
* Validation data key => value array
*
* #var Array
*/
protected $data = array();
/**
* Validation errors
*
* #var Array
*/
protected $errors = array();
/**
* Validation rules
*
* #var Array
*/
protected $rules = array();
/**
* Custom validation messages
*
* #var Array
*/
protected $messages = array();
public function __construct(Validator $validator)
{
$this->validator = $validator;
}
/**
* Set data to validate
*
* #return \Services\Validations\AbstractLaravelValidator
*/
public function with(array $data)
{
$this->data = $data;
return $this;
}
/**
* Validation passes or fails
*
* #return Boolean
*/
public function passes()
{
$validator = Validator::make(
$this->data,
$this->rules,
$this->messages
);
if ($validator->fails())
{
$this->errors = $validator->messages();
return false;
}
return true;
}
/**
* Return errors, if any
*
* #return array
*/
public function errors()
{
return $this->errors;
}
}
and then finally this is how i call the scenarios inside services like this
public function __construct(somethingFormValidator $v)
{
$this->v = $v;
}
public function updateSomething($array)
{
if($this->v->scenario('update')->with($array)->passes())
{
//do something
else
{
throw new ValidationFailedException(
'Validation Fail',
null,
$this->v->errors()
);
}
}
So the problem is now since i have migrated to L5 and L5 uses FormRequest, how should I use scenario validation in my codes?
<?php namespace App\Http\Requests;
use App\Http\Requests\Request;
class ResetpasswordRequest extends Request {
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'login_email' => 'required',
'g-recaptcha-response' => 'required|captcha',
];
}
public function messages()
{
return [
'login_email.required' => 'Email cannot be blank',
'g-recaptcha-response.required' => 'Are you a robot?',
'g-recaptcha-response.captcha' => 'Captcha session timeout'
];
}
public function scenario($scene)
{
switch ($scene) {
case 'scene1':
$this->rules = array(
//scenario rules
);
break;
}
return $this;
}
}
also how should I call it in the controller?
public function postReset(ResetpasswordRequest $request)
{
$profile = ProfileService::getProfileByEmail(Request::input('login_email'));
if($profile == null)
{
$e = array('login_email' => 'This email address is not registered');
return redirect()->route('reset')->withInput()->withErrors($e);
}
else
{
//$hash = ProfileService::createResetHash($profile->profile_id);
$time = strtotime('now');
$ip = Determinator::getClientIP();
MailProcessor::sendResetEmail(array('email' => $profile->email,
'ip' => $ip, 'time' => $time,));
}
}
I believe the real issue at hand is everything is validated through the form request object before it reaches your controller and you were unable to set the appropriate validation rules.
The best solution I can come up with for that is to set the validation rules in the form request object's constructor. Unfortunately, I am not sure how or where you are able to come up with the $scene var as it seems to be hard-coded in your example as 'update'.
I did come up with this though. Hopefully reading my comments in the constructor will help further.
namespace App\Http\Requests;
use App\Http\Requests\Request;
class TestFormRequest extends Request
{
protected $rules = [
'title' => 'required',
'fullname' => 'required',
// and many more
];
public function __construct()
{
call_user_func_array(array($this, 'parent::__construct'), func_get_args());
// Not sure how to come up with the scenario. It would be easiest to add/set a hidden form field
// and set it to 'scene1' etc...
$this->scenario($this->get('scenario'));
// Could also inspect the route to set the correct scenario if that would be helpful?
// $this->route()->getUri();
}
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return $this->rules;
}
public function scenario($scene)
{
switch ($scene) {
case 'scene1':
$this->rules = [
//scenario rules
];
break;
}
}
}
You can use laratalks/validator package for validation with multiple scenarios in laravel. see this repo

Resources