haakym's avatar

Problems implementing a Command handler

Hey everyone. I'm implementing a small component for a HR system for the office I work in. I'm currently working on allowing users to request a holiday. I'm trying my best to use Command Handlers (and hopefully Events too) as my Controllers are always very bloated.

I want to perform the following in my application:

Firstly, a User hits submit on the form to "create new request" for a holiday for example. This should go to the store() method on the RequestController and fire off a self handling command, namely CreateRequestCommand. This Command will then store the request made by the user in the DB, then carry on with functionality I haven't got round to yet.

There's two issues I'm facing while implementing the aforementioned:

Firstly, my form has a file upload. As you know if I dd() the file in my controller I'll see the files properties which I can use to upload it. However if I dd() it in the handle() function of the CreateRequestCommand it shows null. So, I'm confused as to where I should be be performing the upload logic?

Secondly, my form has 9 fields which pretty much matches what I store in the database, with exception to the user_id. So usually what I'd do, and I know this is considered back practice hence why I'm trying to use Commands, is grab all the input from a form and then throw it all in a create() function of the model to store it in the DB. Now I've extracted that logic to the Command and from viewing Taylor Otwell's example here on laracasts and in the docs for the PurchasePodcastCommand (or whatever it was) I understand I need all the relevant data to be placed in the Command's constructor, this meaning I have 9 variables in the constructor. Now forgive my ignorance, but having 9 variables in the constructor just looks wrong, so I'm asking myself and now here - am I doing this right? What follows is some of the relevant code from the classes I've mentioned in the above.

app\Http\Controllers\RequestController.php

public function store(Requests\CreateRequestRequest $request)
{
    $this->dispatchFrom(CreateRequestCommand::class, $request);
}

app\Commands\CreateRequestCommand.php

class CreateRequestCommand extends Command implements SelfHandling {

    /**
     * Values from request
     * @var mixed
     */
    protected $requestType, $attachment, $manager, $noOfDays, $dateFrom, $dateTo, $location, $contactNumber, $cover;

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct($requestType, $attachment, $manager, $noOfDays, $dateFrom, $dateTo, $location, $contactNumber, $cover)
    {
        $this->requestType   = $requestType;
        $this->attachment    = $attachment;
        $this->manager       = $manager;
        $this->noOfDays      = $noOfDays;
        $this->dateFrom      = $dateFrom;
        $this->dateTo        = $dateTo;
        $this->location      = $location;
        $this->contactNumber = $contactNumber;
        $this->cover         = $cover;
    }

    /**
     * Execute the command.
     *
     * @return void
     */
    public function handle()
    {
                // store to db here
        dd([
            'requestType'   => $this->requestType,
            'attachment'    => $this->attachment,
            'manager'       => $this->manager,
            'noOfDays'      => $this->noOfDays,
            'dateFrom'      => $this->dateFrom,
            'dateTo'        => $this->dateTo,
            'location'      => $this->location,
            'contactNumber' => $this->contactNumber,
            'cover'         => $this->cover,
        ]);
    }

}

Thanks very much indeed to anyone who can offer any advice. If you need to see more code or anything I'd be happy to share it.

0 likes
1 reply

Please or to participate in this conversation.