1
\$\begingroup\$

This is my app/Http/Requests/Organisation/CreateOrganisationRequest.php

<?php

namespace App\Http\Requests\Organisation;

use App\Contracts\OrganisationInterface;
use Illuminate\Foundation\Http\FormRequest;
use App\Http\Controllers\Api\BaseApiController;
use Symfony\Component\HttpKernel\Exception\HttpException;

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}.");
        }
    }
}

and it's used in the controller like this:

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
    );
}

This works and get's the job done, but I am unsure if this is a good approach in Laravel.

P.S. The HttpException that I throw is handled by app\Exceptions\Handler.php.


Another approach I've considered is to add a custom attribute to User model like this:

public function getOrganisationCountAttribute()
{
    if ($this->account_owner) {
        return $this->organisations()->count();
    }
    return 0;
}

and update my CreateOrganisationRequest (i.e. the FormRequest) by removing the repository and do this instead in my business rule validation:

private function checkIfMaxOrganisationLimitReached()
{
    $currentOrganisationsCount = auth()->user()->organisation_count;
    $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}.");
    }
}
\$\endgroup\$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.