|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tempest\Icon; |
| 4 | + |
| 5 | +use Exception; |
| 6 | +use Tempest\EventBus\EventBus; |
| 7 | +use Tempest\Http\HttpRequestFailed; |
| 8 | +use Tempest\Http\Status; |
| 9 | +use Tempest\HttpClient\HttpClient; |
| 10 | +use Tempest\Icon\IconCache; |
| 11 | +use Tempest\Support\Str; |
| 12 | +use Tempest\Support\Str\ImmutableString; |
| 13 | + |
| 14 | +final class Icon |
| 15 | +{ |
| 16 | + public function __construct( |
| 17 | + private IconCache $iconCache, |
| 18 | + private IconConfig $iconConfig, |
| 19 | + private HttpClient $http, |
| 20 | + private ?EventBus $eventBus = null, |
| 21 | + ) {} |
| 22 | + |
| 23 | + /** |
| 24 | + * Renders an icon as an SVG snippet. If the icon is not cached, it will be |
| 25 | + * downloaded on the fly and cached it for future use. If the icon is |
| 26 | + * already in the cache, it will be served from there. |
| 27 | + * |
| 28 | + * This method may return `null` if an error occurred in the process. |
| 29 | + */ |
| 30 | + public function render(string $icon): ?string |
| 31 | + { |
| 32 | + [$collection, $iconName] = $this->parseIconIdentifier($icon); |
| 33 | + |
| 34 | + if ($this->iconCache->get("icon-failure-{$collection}-{$iconName}")) { |
| 35 | + return null; |
| 36 | + } |
| 37 | + |
| 38 | + $svg = $this->iconCache->resolve( |
| 39 | + key: "icon-{$collection}-{$iconName}", |
| 40 | + callback: fn () => $this->fetchSvg($collection, $iconName), |
| 41 | + expiresAt: $this->iconConfig->expiresAfter, |
| 42 | + ); |
| 43 | + |
| 44 | + if ($this->iconCache->get("icon-failure-{$collection}-{$iconName}")) { |
| 45 | + $this->iconCache->delete("icon-{$collection}-{$iconName}"); |
| 46 | + } |
| 47 | + |
| 48 | + return $svg; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Downloads the icon's SVG file from the Iconify API |
| 53 | + */ |
| 54 | + private function fetchSvg(string $collection, string $icon): ?string |
| 55 | + { |
| 56 | + try { |
| 57 | + $url = new ImmutableString($this->iconConfig->iconifyApiUrl) |
| 58 | + ->finish('/') |
| 59 | + ->append("{$collection}/{$icon}.svg") |
| 60 | + ->toString(); |
| 61 | + |
| 62 | + $response = $this->http->get($url); |
| 63 | + |
| 64 | + if ($response->status !== Status::OK) { |
| 65 | + throw new HttpRequestFailed($response->status, cause: $response); |
| 66 | + } |
| 67 | + |
| 68 | + $this->eventBus?->dispatch(new IconDownloaded( |
| 69 | + collection: $collection, |
| 70 | + name: $icon, |
| 71 | + icon: $response->body, |
| 72 | + )); |
| 73 | + |
| 74 | + return $response->body; |
| 75 | + } catch (Exception $exception) { |
| 76 | + $this->eventBus?->dispatch(new IconDownloadFailed( |
| 77 | + collection: $collection, |
| 78 | + name: $icon, |
| 79 | + exception: $exception, |
| 80 | + )); |
| 81 | + |
| 82 | + $this->iconCache->put( |
| 83 | + key: "icon-failure-{$collection}-{$icon}", |
| 84 | + value: true, |
| 85 | + expiresAt: $this->iconConfig->retryAfter, |
| 86 | + ); |
| 87 | + |
| 88 | + return null; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @return array{string,string} |
| 94 | + */ |
| 95 | + private function parseIconIdentifier(string $identifier): ?array |
| 96 | + { |
| 97 | + if (! Str\contains($identifier, ':')) { |
| 98 | + $identifier = Str\replace_first($identifier, '-', ':'); |
| 99 | + } |
| 100 | + |
| 101 | + $parts = explode(':', $identifier, limit: 2); |
| 102 | + |
| 103 | + if (count($parts) !== 2) { |
| 104 | + return null; |
| 105 | + } |
| 106 | + |
| 107 | + return $parts; |
| 108 | + } |
| 109 | +} |
0 commit comments