Laravel: Passing a class name from a Controller to a Job - laravel

I'm trying to pass a certain class name from the controller to a job file through
Job::dispatch($className);
I've tried passing it as a string and passing it as a ClassName::class but none of these methods work
The code in the Job file looks something along the lines of this:
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class BreakUpArraysJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $products;
protected $jobName;
public function __construct($products, $jobName)
{
$this->products = $products;
$this->jobName = $jobName;
}
/**
* Execute the jobName.
*
* #return void
*/
public function handle()
{
$productArrays = array_chunk($this->products, 5000);
foreach($productArrays as $productArray){
$this->jobName::dispatch($productArray);
}
}
}
the $jobname variable is the className I'm trying to pass.
I also need to note that I'm passing the classname of a different Job from the Controller, by which I mean the job handler is supposed to call another Job through the variable.

I do not think your syntax is working, an alternative approach would be to do it like this.
dispatch(new $this->jobName($productArray));

in your job construct add class Name that your var is instance of before $product like this:
public function __construct(Product $products, $jobName)
{
$this->products = $products;
$this->jobName = $jobName;
}

Related

Laravel 8 jobs not dispatching

I have this Job
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Http\Traits\CreateTrait;
class CreateJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, CreateTrait;
protected $contactForm;
public function __construct($contactForm)
{
$this->contactForm = $contactForm;
}
public function handle()
{
//This connects to SSH and takes 5-10s
$this->CreateOnetime($this->contactForm->room_id, $this->contactForm->company_id, $this->contactForm->id);
}
}
Then I try in controller
use App\Jobs\CreateJob;
class ContactFormController extends Controller
{
public function save(StoreContactFormRequest $request, $cid, $rid){
$validated = $request->validated();
$validated['room_id'] = $rid;
$validated['company_id'] = $cid;
$contactForm = ContactForm::create($validated);
CreateJob::dispatch($contactForm);
return back();
}
}
Nothing written in the DB, though I have QUEUE_DRIVER=database in the .env file.
The function runs synchronously, so it does not create the Job somehow, just runs it.
Problem was I had QUEUE_CONNECTION=sync somewhere after QUEUE_DRIVER=database

Laravel - how to pass variable from controller to job

Sorry if this is a noob question.
I used to send emails directly through the controller, like so:
Mail::to($user)->send(new RegisterConfirmation($user));
As you can see I also pass the $user into the email where I can use that user's information, like "Hello {{$user->username}}!"
But now instead I want to queue emails, so in my controller I now have this:
RegisterConfirmationJob::dispatch($user);
In my job, I have the following:
public function __construct(User $user)
{
$this->user = $user;
}
public function handle()
{
$user = $this->user;
Mail::to($user)->send(new RegisterConfirmation($user));
}
When I run this, it returns the following error:
[2021-07-09 20:33:57] local.ERROR: Undefined property:
App\Jobs\RegisterConfirmationJob::$user {"exception":"[object]
(ErrorException(code: 0): Undefined property:
App\Jobs\RegisterConfirmationJob::$user at
/home/vagrant/projects/app/Jobs/RegisterConfirmationJob.php:37)
[stacktrace]
Why is this happening?
EDIT:
As requested, here is my entire RegisterConfirmationJob class:
<?php
namespace App\Jobs;
use App\Mail\RegisterConfirmation;
use App\User;
use Illuminate\Support\Facades\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class RegisterConfirmationJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function __construct(User $user)
{
$this->user = $user;
}
public function handle()
{
$user = $this->user;
Mail::to($user)->send(new RegisterConfirmation($user));
}
}
It's simple, you hadn't declared $user as a class property, that is the error meaning.
I recommend you declare it as public property to use it at blade template
Yeah bro, it's not a Laravel problem, it's a PHP class definition problem, you can solve this by declaring the $user property, like that:
<?php
namespace App\Jobs;
use App\Mail\RegisterConfirmation;
use App\User;
use Illuminate\Support\Facades\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class RegisterConfirmationJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $user; // you forgot of put this line
public function __construct(User $user)
{
$this->user = $user;
}
public function handle()
{
$user = $this->user;
Mail::to($user)->send(new RegisterConfirmation($user));
}
}

Laravel 8 - class not using mock version of class in test

Hi Latest question on unraveling code - I'm trying to create tests that mock certain classes but struggling to get my first attempt to work. It does not appear to be using the mock in the test but the real class.
There are three questions:
What do I need to change to get this to work and the mock to be used?
Does the mock work for the whole app under test or just the class under test?
If I wanted to return an array of values do I just create the array with the expected values to enable the class under test to use that returned data?
So here's the class I want to mock:
<?php
namespace App\Wedleague\Utility;
use App\Libraries\Utilities\FolderUtility;
use App\Wedleague\LeagueTable\LeagueTableInterface;
use PDF;
/**
* Class PDFGenerator
* Generates a PDF based on a league
* #package App\Libraries\Helpers
*/
class PDFGenerator
{
use FolderUtility;
/**
* set the folder location for the generated pdf
* #var string
*/
private string $storageFolder = 'pdf';
private string $path;
private LeagueTableInterface $leagueTable;
/**
* PDFGenerator constructor.
*/
public function __construct(LeagueTableInterface $leagueTable)
{
$this->path = storage_path('app/' . $this->storageFolder);
$this->setUp();
$this->leagueTable = $leagueTable;
}
/**
* #return \Barryvdh\DomPDF\PDF
*/
public function createLeaguePDF()
{
$this->leagueTable->getLeagueTable();
error_reporting(E_ALL ^ E_DEPRECATED);
return PDF::loadView($this->leagueTable->getViewPath(), $this->leagueTable->getViewData())
->setOptions(['fontSubsetting' => true])
->setPaper('a4', 'landscape')
->save(storage_path('app/pdf/' . $this->leagueTable->getLeagueType() . '_league_update.pdf'));
}
private function setUp()
{
$this->checkFolderExists($this->path);
}
}
Here's the class I'm trying to test that uses this class:
<?php
namespace App\Jobs;
use App\Wedleague\Utility\PDFGenerator;
use App\Mail\MailEclecticUpdate;
use App\Models\League;
use App\Wedleague\LeagueTable\EclecticLeagueTable;
use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
/**
* Class ProcessSendEclecticUpdate
* #package App\Jobs
*/
class ProcessSendEclecticUpdateEmail implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* The number of times the job may be attempted.
*
* #var int
*/
public $tries = 3;
/**
* The number of seconds the job can run before timing out.
*
* #var int
*/
public $timeout = 240;
private League $league;
public function __construct(League $league)
{
$this->league = $league;
}
public function handle()
{
if (!$this->league || $this->league->league_type !== 'eclectic') {
throw new Exception("Error Processing the job - league not found or invalid league", 1);
}
$PDFGenerator = new PDFGenerator(new EclecticLeagueTable($this->league));
$PDFGenerator->createLeaguePDF();
$this->league->player()->get()->each(function ($player) {
if ($player->contactEmail) {
Mail::to($player->contactEmail)
->queue(new MailEclecticUpdate(
$this->league
));
}
});
Log::info('Eclectic League update issued');
}
}
and here's the basics of the test:
<?php
namespace Tests\Jobs;
use App\Jobs\ProcessSendEclecticUpdateEmail;
use App\Mail\MailEclecticUpdate;
use App\Models\League;
use App\Models\User;
use App\Wedleague\Utility\PDFGenerator;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Mail;
use Tests\TestCase;
/**
* Class ProcessSendEclecticUpdateTest
* #package Tests\Jobs
*/
class ProcessSendEclecticUpdateTest extends TestCase
{
use RefreshDatabase;
use WithFaker;
private $user;
private $adminUser;
protected function setUp(): void
{
parent::setUp();
$this->seed();
$this->adminUser = User::factory()->create(['admin' => 1]);
}
/**
* #test
* #covers ProcessSendEclecticUpdateEmail::handle
* #description:
*/
public function testHandle()
{
$mock = $this->mock(\App\Wedleague\Utility\PDFGenerator::class);
$mock->shouldReceive('createLeaguePDF')->once();
$this->app->instance(PDFGenerator::class, $mock);
Mail::fake();
$this->withoutExceptionHandling();
$league = League::find(27);
$players = $league->player()
->get()
->filter(function ($player){
return $player->contactEmail;
});
ProcessSendEclecticUpdateEmail::dispatch($league);
Mail::assertQueued(MailEclecticUpdate::class, $players->count());
}
}
The response I get from the test is:
Mockery\Exception\InvalidCountException : Method createLeaguePDF(<Any Arguments>) from Mockery_2_App_Wedleague_Utility_PDFGenerator should be called
exactly 1 times but called 0 times.
I know this is not using the mock due to the length of time the test takes as it produces a PDF
Any ideas how I can get this to work - it's mocking me!!! :)

How to call Facades method from boot method?

In laravel 6 app I created facade app/Facades/MyFuncsClass.php :
<?php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class MyFuncsClass extends Facade{
protected static function getFacadeAccessor() { return 'MyFuncsClass'; }
}
But I got error :
"message": "Non-static method App\\Helpers\\MyFuncsClass::debToFile() should not be called
statically",
calling it from boot method:
protected static function boot() {
parent::boot();
static::deleting(function($task) {
$hostel_image_image_path= Task::getTaskImagePath($task->id, $task->image);
MyFuncsClass::debToFile(print_r($hostel_image_image_path, true), '-9 $hostel_image_image_path::');
...
Is there is a way to escape this error and run MyFuncsClass::debToFile in boot method ?
MODIFIED :
Sure I registered my facade in config/app.php, 'providers' block :
...
App\Providers\MyFuncsClassProvider::class,
file app/Http/Helpers/MyFuncsClass.php has a lot of public methods, with heading:
<?php
namespace App\Helpers;
use Illuminate\Http\Request;
use Barryvdh\Debugbar\Facade as Debugbar;
use Carbon\Carbon;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
class MyFuncsClass {
public $concat_str_max_length = 50;
public $m_concat_str_add_chars = '...';
public function debToFile($contents, string $descr_text = '', bool $is_sql = false, string $file_name = '')
{
try {
...
and in app/Providers/MyFuncsClassProvider.php :
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\App;
class MyFuncsClassProvider extends ServiceProvider
{
/**
* Register services.
*
* #return void
*/
public function register()
{
App::bind('MyFuncsClass', function()
{
return new \App\Helpers\MyFuncsClass;
});
}
/**
* Bootstrap services.
*
* #return void
*/
public function boot()
{
//
}
}
Actually I can call \MyFuncsClass::debToFile( ok from not static methods, like control actions, but I have the error
calling from static boot method...
MODIFIED # 2 :
With real time facades https://laravel.com/docs/5.7/facades#real-time-facades defintions
I tried
<?php
namespace App;
use DB;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use App\Facades\MyFuncsClass;
use App\Http\Helpers\MyFuncsClassContainer; // Alias for my helper class
use Illuminate\Database\Eloquent\Model;
use Barryvdh\Debugbar\Facade as Debugbar;
class Task extends Model
{
use Sluggable;
protected $table = 'tasks';
protected $primaryKey = 'id';
}
protected static function boot() {
parent::boot();
static::deleting(function($task) {
$hostel_image_image_path= Task::getTaskImagePath($task->id, $task->image);
\Log::info( '-9 $hostel_image_image_path::' );
\Log::info( print_r($hostel_image_image_path, true) );
$myFuncsClassCore = factory(MyFuncsClassContainer::class)->create();
$myFuncsClassCore->debToFile(' debToFile string REDSA');
Bur anywat I got error :
Cannot declare class App\Helpers\MyFuncsClassContainer, because the name is already in use {"userId":1,"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalErrorException
I tried a way to rename my helper class to “MyFuncsClassContainer”, supposing
having the same name for helper and facade could raise error, but failed
If there is a way to fix this error ?
Thanks!

error in registering command in laravel 5.0

i am trying to make my own command in laravel 5.0, but it generates the following error. i have registerd my command in kernel. following are my codes
//folowing error
[ErrorException]
Argument 1 passed to Illuminate\Console\Application::add() must be an instance of Symfony\Component\Console\Command
\Command, instance of App\console\Commands\qwork given, called in C:\xampp\htdocs\queue_mail\vendor\laravel\framewo
rk\src\Illuminate\Console\Application.php on line 115 and defined
//my command qwork.php
<?php namespace App\console\Commands;
use DB;
use App\Commands\Command;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldBeQueued;
class qwork extends Command implements SelfHandling, ShouldBeQueued {
use InteractsWithQueue, SerializesModels;
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
//
}
/**
* Execute the command.
*
* #return void
*/
public function handle()
{
DB::insert('insert into users (name, email) values (?, ?)', ['faran', 'ran.rana#gmail.com']);
}
}
//then defining route by:
Route::get('/run', function()
{
Artisan::queue('command:qwork');
});
Replace
use App\Commands\Command;
with
use Illuminate\Console\Command;

Resources