Advertisement
Guest User

Untitled

a guest
Feb 24th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. <?php
  2.  
  3. namespace AppHttpRequestsOrganisation;
  4.  
  5. use AppContractsOrganisationInterface;
  6. use IlluminateFoundationHttpFormRequest;
  7. use AppHttpControllersApiBaseApiController;
  8. use SymfonyComponentHttpKernelExceptionHttpException;
  9.  
  10. class CreateOrganisationRequest extends FormRequest
  11. {
  12. private $organisationRepository;
  13.  
  14. public function __construct(
  15. array $query = array(),
  16. array $request = array(),
  17. array $attributes = array(),
  18. array $cookies = array(),
  19. array $files = array(),
  20. array $server = array(),
  21. $content = null,
  22. OrganisationInterface $organisationRepository
  23. ) {
  24. parent::__construct($query, $request, $attributes, $cookies, $files, $server, $content);
  25. $this->organisationRepository = $organisationRepository;
  26. }
  27.  
  28. public function authorize()
  29. {
  30. return true;
  31. }
  32.  
  33. public function rules()
  34. {
  35. return [
  36. 'name' => 'required|min:3|max:60|unique:organisations,name'
  37. ];
  38. }
  39.  
  40. public function withValidator($validator)
  41. {
  42. $validator->after(function() {
  43. $this->checkIfMaxOrganisationLimitReached();
  44. });
  45. }
  46.  
  47. private function checkIfMaxOrganisationLimitReached()
  48. {
  49. $currentOrganisationsCount = $this->organisationRepository->myOrganisationCount();
  50. $maxOrganisationsLimit = auth()->user()->max_organisations;
  51.  
  52. if (($currentOrganisationsCount + 1) > $maxOrganisationsLimit) {
  53. throw new HttpException(BaseApiController::Bad_Request, "You have reached maximum organisations that can be created, {$maxOrganisationsLimit}.");
  54. }
  55. }
  56. }
  57.  
  58. public function store(CreateOrganisationRequest $request)
  59. {
  60. $validatedData = array_merge(
  61. $request->validated(),
  62. ['owner_id' => auth()->user()->id]
  63. );
  64.  
  65. $organisation = $this->organisationRepository->create($validatedData);
  66.  
  67. return $this->success(
  68. fractal($organisation, new OrganisationTransformer()),
  69. $this::Created
  70. );
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement