-
-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor handlers #182
Refactor handlers #182
Changes from all commits
3f704ba
ca8a20f
101e251
4a31aac
dc2cd05
8297902
e3a598d
fe95f62
2a65982
da8f918
725ef84
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -68,7 +68,7 @@ $data = [ | |||||
'url' => $url, | ||||||
'destinationFile' => $filename, | ||||||
]; | ||||||
$message = new \Yiisoft\Queue\Message\Message('file-download', $data); | ||||||
$message = new \Yiisoft\Queue\Message\Message(FileDownloader::class, $data); | ||||||
``` | ||||||
|
||||||
Then you should push it to the queue: | ||||||
|
@@ -80,7 +80,10 @@ $queue->push($message); | |||||
Its handler may look like the following: | ||||||
|
||||||
```php | ||||||
class FileDownloader | ||||||
use Yiisoft\Queue\Message\MessageInterface; | ||||||
use Yiisoft\Queue\Message\MessageHandlerInterface; | ||||||
|
||||||
class FileDownloader implements MessageHandlerInterface | ||||||
{ | ||||||
private string $absolutePath; | ||||||
|
||||||
|
@@ -89,21 +92,21 @@ class FileDownloader | |||||
$this->absolutePath = $absolutePath; | ||||||
} | ||||||
|
||||||
public function handle(\Yiisoft\Queue\Message\MessageInterface $downloadMessage): void | ||||||
public function handle(\Yiisoft\Queue\Message\MessageInterface $downloadMessage): MessageInterface | ||||||
{ | ||||||
$fileName = $downloadMessage->getData()['destinationFile']; | ||||||
$fileName = $message->getData()['destinationFile']; | ||||||
$path = "$this->absolutePath/$fileName"; | ||||||
file_put_contents($path, file_get_contents($downloadMessage->getData()['url'])); | ||||||
file_put_contents($path, file_get_contents($message->getData()['url'])); | ||||||
|
||||||
return $message; | ||||||
Comment on lines
+100
to
+101
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
} | ||||||
``` | ||||||
|
||||||
The last thing we should do is to create a configuration for the `Yiisoft\Queue\Worker\Worker`: | ||||||
|
||||||
```php | ||||||
$handlers = ['file-download' => [new FileDownloader('/path/to/save/files'), 'handle']]; | ||||||
$worker = new \Yiisoft\Queue\Worker\Worker( | ||||||
$handlers, // Here it is | ||||||
$logger, | ||||||
$injector, | ||||||
$container | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,9 +21,12 @@ public function withMessage(MessageInterface $message): self | |
return $instance; | ||
} | ||
|
||
public function getHandlerName(): string | ||
/** | ||
* @return class-string<MessageHandlerInterface> | ||
*/ | ||
public function getHandler(): string | ||
{ | ||
return $this->message->getHandlerName(); | ||
return $this->message->getHandler(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There may be a problem here, when handler support only original message and don't support envelope. |
||
} | ||
|
||
public function getData(): mixed | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Queue\Message; | ||
|
||
interface MessageHandlerInterface | ||
{ | ||
public function handle(MessageInterface $message): void; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,8 +10,9 @@ interface MessageInterface | |
* Returns handler name. | ||
* | ||
* @return string | ||
* @psalm-return class-string<MessageHandlerInterface> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any string can be here (for example, ID for container), but class string only. |
||
*/ | ||
public function getHandlerName(): string; | ||
public function getHandler(): string; | ||
|
||
/** | ||
* Returns payload data. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.