Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace AppHttpRequestsOrganisation;
- use AppContractsOrganisationInterface;
- use IlluminateFoundationHttpFormRequest;
- use AppHttpControllersApiBaseApiController;
- use SymfonyComponentHttpKernelExceptionHttpException;
- class CreateOrganisationRequest extends FormRequest
- {
- private $organisationRepository;
- public function __construct(
- array $query = array(),
- array $request = array(),
- array $attributes = array(),
- array $cookies = array(),
- array $files = array(),
- array $server = array(),
- $content = null,
- OrganisationInterface $organisationRepository
- ) {
- parent::__construct($query, $request, $attributes, $cookies, $files, $server, $content);
- $this->organisationRepository = $organisationRepository;
- }
- public function authorize()
- {
- return true;
- }
- public function rules()
- {
- return [
- 'name' => 'required|min:3|max:60|unique:organisations,name'
- ];
- }
- public function withValidator($validator)
- {
- $validator->after(function() {
- $this->checkIfMaxOrganisationLimitReached();
- });
- }
- private function checkIfMaxOrganisationLimitReached()
- {
- $currentOrganisationsCount = $this->organisationRepository->myOrganisationCount();
- $maxOrganisationsLimit = auth()->user()->max_organisations;
- if (($currentOrganisationsCount + 1) > $maxOrganisationsLimit) {
- throw new HttpException(BaseApiController::Bad_Request, "You have reached maximum organisations that can be created, {$maxOrganisationsLimit}.");
- }
- }
- }
- public function store(CreateOrganisationRequest $request)
- {
- $validatedData = array_merge(
- $request->validated(),
- ['owner_id' => auth()->user()->id]
- );
- $organisation = $this->organisationRepository->create($validatedData);
- return $this->success(
- fractal($organisation, new OrganisationTransformer()),
- $this::Created
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement