InvalidSyntax
InvalidSyntax

Reputation: 9509

Running method from Controller in Artisan Command?

In my custom Artisan command I fetch a list of items from my DB, I would like to iterate through them and run them through a method from my DownloaderController. How can I do this? and what is the best practise?

DownloadCommand.php

public function handle()
    {
        $files = File::all();

        foreach($files as $file)
        {
            // downloadFile method belongs to DownloadController
            downloadFile($file);
        }
    }

DownloadController.php

public function downloadFile($file)
{
    // some example logic to download file
    if(wget($file))
    {
        $file->status = 'Downloaded';
    }
    else
    {
        $file->status = 'Failed';
    }

    $file->save();
}

Upvotes: 0

Views: 197

Answers (1)

Mateusz Sip
Mateusz Sip

Reputation: 1280

Logic responsible for fetching data shouldn't be included in your controller. Create a repository (or a specialized service) for that, and inject it to your command.

Upvotes: 0

Related Questions