You can generate a feature by following command.
php artisan mojura:feature [Feature] [Module] [Directory] [--force]
php artisan mojura:feature LoginUser Core
Generated Feature file will be at app/Modules/CoreModule/Features/LoginUserFeature.php
Parameters
-
[Feature]: The name of the feature class or file you want to create (required).
-
[Module]: The module where the feature class will be created (required).
-
[Directory]: The specific directory within the module where the feature class will be placed (optional).
-
[--force]: If set, it will overwrite any existing feature class with the same name (optional).
Feature Class Implementation - Running Jobs
The Feature must inherit the Mojura Feature InnoAya\Mojura\Core\Feature
to work with the run
or runInQueue
method.
Running jobs from a feature is straightforward using run
method.
<?php
namespace App\Modules\CoreModule\Features;
use InnoAya\Mojura\Core\Feature;
use App\Exceptions\UnauthorizedException;
use App\Helpers\JsonResponder;
use App\Modules\CoreModule\Http\Requests\LoginUserRequest;
use App\Modules\CoreModule\Jobs\LoginUserJob;
use Illuminate\Http\JsonResponse;
class LoginFeature extends Feature
{
/**
* Execute the feature.
*/
public function handle(LoginUserRequest $request): JsonResponse
{
try {
$data = $this->run(new LoginUserJob($request->validated()));
return JsonResponder::success('Logged in successfully', $data);
} catch (UnauthorizedException $ue) {
return JsonResponder::unauthorized($ue->getMessage());
} catch (Exception $e) {
return JsonResponder::internalServerError($e->getMessage());
}
}
}
Feature Class Implementation - Running Queue Jobs
Running queue jobs from a feature is straightforward using runInQueue
method. The Job that run inside runInQueue
method must inherit InnoAya\Mojura\Core\\QueueableJob
.
<?php
namespace App\Modules\CoreModule\Features;
use InnoAya\Mojura\Core\Feature;
use App\Exceptions\UnauthorizedException;
use App\Helpers\JsonResponder;
use App\Modules\CoreModule\Http\Requests\LoginUserRequest;
use App\Modules\CoreModule\Jobs\LoginUserJob;
use App\Modules\CoreModule\Jobs\NotifyUserLoginViaEmailJob;
use Illuminate\Http\JsonResponse;
class LoginFeature extends Feature
{
/**
* Execute the feature.
*/
public function handle(LoginUserRequest $request): JsonResponse
{
try {
$data = $this->run(new LoginUserJob($request->validated()));
$this->runInQueue(new NotifyUserLoginViaEmailJob($request->validated()));
return JsonResponder::success('Logged in successfully', $data);
} catch (UnauthorizedException $ue) {
return JsonResponder::unauthorized($ue->getMessage());
} catch (Exception $e) {
return JsonResponder::internalServerError($e->getMessage());
}
}
}