1. Go to this page and download the library: Download phpnomad/tasks library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
phpnomad / tasks example snippets
use PHPNomad\Tasks\Exceptions\TaskCreateFailedException;
use PHPNomad\Tasks\Interfaces\Task;
class SendWelcomeEmail implements Task
{
public function __construct(
public readonly int $userId
) {}
public static function getId(): string
{
return 'user.send_welcome_email';
}
public function toPayload(): array
{
return ['userId' => $this->userId];
}
/**
* @throws TaskCreateFailedException
*/
public static function fromPayload(array $payload): static
{
return new static($payload['userId']);
}
}
use PHPNomad\Tasks\Interfaces\CanHandleTask;
use PHPNomad\Tasks\Interfaces\Task;
/**
* @implements CanHandleTask<SendWelcomeEmail>
*/
class SendWelcomeEmailHandler implements CanHandleTask
{
public function __construct(
protected UserDatastore $users,
protected EmailService $email
) {}
public function handle(Task $task): void
{
$user = $this->users->find($task->userId);
$this->email->send($user->getEmail(), 'Welcome!');
}
}
use PHPNomad\Tasks\Interfaces\HasTaskHandlers;
class MyAppInitializer implements HasTaskHandlers
{
public function getTaskHandlers(): array
{
return [
SendWelcomeEmail::class => SendWelcomeEmailHandler::class,
];
}
}
use PHPNomad\Tasks\Interfaces\TaskStrategy;
class UserService
{
public function __construct(
protected UserDatastore $users,
protected TaskStrategy $tasks
) {}
public function createUser(string $email): User
{
$user = $this->users->create(['email' => $email]);
$this->tasks->dispatch(new SendWelcomeEmail($user->getId()));
return $user;
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.