Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 19 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| ImageAnalysisJob | |
0.00% |
0 / 19 |
|
0.00% |
0 / 3 |
30 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getJobType | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| execute | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | |
| 4 | namespace App\Job; |
| 5 | |
| 6 | use App\Service\Api\AiService; |
| 7 | use Cake\Queue\Job\Message; |
| 8 | use Interop\Queue\Processor; |
| 9 | |
| 10 | /** |
| 11 | * ImageAnalysisJob |
| 12 | * |
| 13 | * This job is responsible for analyzing images using AI. |
| 14 | * It processes messages from the queue to analyze images and update their metadata. |
| 15 | */ |
| 16 | class ImageAnalysisJob extends AbstractJob |
| 17 | { |
| 18 | /** |
| 19 | * Instance of the AI service. |
| 20 | * |
| 21 | * @var \App\Service\Api\AiService |
| 22 | */ |
| 23 | private AiService $aiService; |
| 24 | |
| 25 | /** |
| 26 | * Constructor to allow dependency injection for testing |
| 27 | * |
| 28 | * @param \App\Service\Api\AiService|null $aiService |
| 29 | */ |
| 30 | public function __construct(?AiService $aiService = null) |
| 31 | { |
| 32 | $this->aiService = $aiService ?? new AiService(); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Get the human-readable job type name for logging |
| 37 | * |
| 38 | * @return string The job type description |
| 39 | */ |
| 40 | protected static function getJobType(): string |
| 41 | { |
| 42 | return 'image analysis'; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Executes the job to analyze an image and update its metadata. |
| 47 | * |
| 48 | * @param \Cake\Queue\Job\Message $message The message containing image data |
| 49 | * @return string|null Returns Processor::ACK on success, Processor::REJECT on failure |
| 50 | */ |
| 51 | public function execute(Message $message): ?string |
| 52 | { |
| 53 | $folderPath = $message->getArgument('folder_path'); |
| 54 | $file = $message->getArgument('file'); |
| 55 | $id = $message->getArgument('id'); |
| 56 | $model = $message->getArgument('model'); |
| 57 | |
| 58 | if (!$this->validateArguments($message, ['folder_path', 'file', 'id', 'model'])) { |
| 59 | return Processor::REJECT; |
| 60 | } |
| 61 | |
| 62 | return $this->executeWithErrorHandling($id, function () use ($folderPath, $file, $id, $model) { |
| 63 | $modelTable = $this->getTable($model); |
| 64 | $image = $modelTable->get($id); |
| 65 | |
| 66 | $analysisResult = $this->aiService->analyzeImage($folderPath . $file); |
| 67 | |
| 68 | if ($analysisResult) { |
| 69 | $image->name = $analysisResult['name']; |
| 70 | $image->alt_text = $analysisResult['alt_text']; |
| 71 | $image->keywords = $analysisResult['keywords']; |
| 72 | |
| 73 | return $modelTable->save($image); |
| 74 | } |
| 75 | |
| 76 | return false; |
| 77 | }, "{$model}:{$file}"); |
| 78 | } |
| 79 | } |