|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace CSlant\GitHubProject\Jobs; |
| 4 | + |
| 5 | +use CSlant\GitHubProject\Services\GithubService; |
| 6 | +use Illuminate\Bus\Queueable; |
| 7 | +use Illuminate\Contracts\Queue\ShouldQueue; |
| 8 | +use Illuminate\Foundation\Bus\Dispatchable; |
| 9 | +use Illuminate\Queue\InteractsWithQueue; |
| 10 | +use Illuminate\Queue\SerializesModels; |
| 11 | +use Illuminate\Support\Facades\Cache; |
| 12 | + |
| 13 | +class ProcessAggregatedEvents implements ShouldQueue |
| 14 | +{ |
| 15 | + use Dispatchable; |
| 16 | + use InteractsWithQueue; |
| 17 | + use Queueable; |
| 18 | + use SerializesModels; |
| 19 | + |
| 20 | + protected string $nodeId; |
| 21 | + |
| 22 | + /** |
| 23 | + * Create a new job instance. |
| 24 | + */ |
| 25 | + public function __construct(string $nodeId) |
| 26 | + { |
| 27 | + $this->nodeId = $nodeId; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Execute the job. |
| 32 | + */ |
| 33 | + public function handle(): void |
| 34 | + { |
| 35 | + $commentAggregationCacheKey = config('github-project.comment_aggregation_cache_key')."_{$this->nodeId}"; |
| 36 | + |
| 37 | + /** @var array<string, mixed> $eventMessages */ |
| 38 | + $eventMessages = Cache::pull($commentAggregationCacheKey, []); |
| 39 | + |
| 40 | + $message = $this->aggregateMessages($eventMessages); |
| 41 | + $author = Cache::pull($commentAggregationCacheKey.'_author', []); |
| 42 | + |
| 43 | + Cache::forget($commentAggregationCacheKey); |
| 44 | + Cache::forget($commentAggregationCacheKey.'_author'); |
| 45 | + |
| 46 | + $message .= view( |
| 47 | + 'github-project::md.shared.author', |
| 48 | + ['name' => $author['name'], 'html_url' => $author['html_url']] |
| 49 | + )->render(); |
| 50 | + |
| 51 | + $githubService = new GithubService; |
| 52 | + $githubService->commentOnNode($this->nodeId, $message); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Aggregate messages from events. |
| 57 | + * |
| 58 | + * @param array<string, mixed> $eventMessages |
| 59 | + * |
| 60 | + * @return string |
| 61 | + */ |
| 62 | + protected function aggregateMessages(array $eventMessages): string |
| 63 | + { |
| 64 | + $messages = array_map(function ($message) { |
| 65 | + return $message; |
| 66 | + }, $eventMessages); |
| 67 | + |
| 68 | + return implode("\n", $messages); |
| 69 | + } |
| 70 | +} |
0 commit comments