|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Services\Matchmaking; |
| 4 | + |
| 5 | +use App\MatchmakingProfile; |
| 6 | +use Illuminate\Support\Str; |
| 7 | + |
| 8 | +class ProfileAvatarResolver |
| 9 | +{ |
| 10 | + private const ORG_DIR = 'images/matchmaking-tool/matchmaking-logos'; |
| 11 | + private const VOLUNTEER_DIR = 'images/matchmaking-tool/matchmaking-volunteers'; |
| 12 | + |
| 13 | + private const ALLOWED_EXTENSIONS = ['png', 'jpg', 'jpeg', 'webp', 'svg']; |
| 14 | + |
| 15 | + /** |
| 16 | + * @var array<string, string>|null |
| 17 | + */ |
| 18 | + private ?array $organisationIndex = null; |
| 19 | + |
| 20 | + /** |
| 21 | + * @var array<string, string>|null |
| 22 | + */ |
| 23 | + private ?array $volunteerIndex = null; |
| 24 | + |
| 25 | + public function resolveForProfile(MatchmakingProfile $profile): ?string |
| 26 | + { |
| 27 | + return $this->resolve( |
| 28 | + $profile->type, |
| 29 | + $profile->organisation_name, |
| 30 | + $profile->first_name, |
| 31 | + $profile->last_name, |
| 32 | + $profile->slug |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + public function resolve( |
| 37 | + ?string $type, |
| 38 | + ?string $organisationName, |
| 39 | + ?string $firstName, |
| 40 | + ?string $lastName, |
| 41 | + ?string $slug = null |
| 42 | + ): ?string { |
| 43 | + $isOrganisation = $type === MatchmakingProfile::TYPE_ORGANISATION; |
| 44 | + $index = $isOrganisation ? $this->getOrganisationIndex() : $this->getVolunteerIndex(); |
| 45 | + |
| 46 | + if (empty($index)) { |
| 47 | + return null; |
| 48 | + } |
| 49 | + |
| 50 | + $candidates = []; |
| 51 | + if (!empty($slug)) { |
| 52 | + $candidates[] = $slug; |
| 53 | + } |
| 54 | + if (!empty($organisationName)) { |
| 55 | + $candidates[] = $organisationName; |
| 56 | + } |
| 57 | + |
| 58 | + $fullName = trim(implode(' ', array_filter([(string) $firstName, (string) $lastName]))); |
| 59 | + if ($fullName !== '') { |
| 60 | + $candidates[] = $fullName; |
| 61 | + } |
| 62 | + |
| 63 | + foreach ($candidates as $candidate) { |
| 64 | + $normalized = $this->normalize($candidate); |
| 65 | + if (isset($index[$normalized])) { |
| 66 | + return '/' . ltrim($index[$normalized], '/'); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return null; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @return array<string, string> |
| 75 | + */ |
| 76 | + public function getOrganisationIndex(): array |
| 77 | + { |
| 78 | + if ($this->organisationIndex === null) { |
| 79 | + $this->organisationIndex = $this->buildIndex(self::ORG_DIR); |
| 80 | + } |
| 81 | + |
| 82 | + return $this->organisationIndex; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @return array<string, string> |
| 87 | + */ |
| 88 | + public function getVolunteerIndex(): array |
| 89 | + { |
| 90 | + if ($this->volunteerIndex === null) { |
| 91 | + $this->volunteerIndex = $this->buildIndex(self::VOLUNTEER_DIR); |
| 92 | + } |
| 93 | + |
| 94 | + return $this->volunteerIndex; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * @return array<string, string> |
| 99 | + */ |
| 100 | + private function buildIndex(string $relativeDirectory): array |
| 101 | + { |
| 102 | + $absoluteDirectory = public_path($relativeDirectory); |
| 103 | + if (!is_dir($absoluteDirectory)) { |
| 104 | + return []; |
| 105 | + } |
| 106 | + |
| 107 | + $index = []; |
| 108 | + $files = scandir($absoluteDirectory) ?: []; |
| 109 | + |
| 110 | + foreach ($files as $file) { |
| 111 | + if ($file === '.' || $file === '..') { |
| 112 | + continue; |
| 113 | + } |
| 114 | + |
| 115 | + $extension = strtolower(pathinfo($file, PATHINFO_EXTENSION)); |
| 116 | + if (!in_array($extension, self::ALLOWED_EXTENSIONS, true)) { |
| 117 | + continue; |
| 118 | + } |
| 119 | + |
| 120 | + $basename = pathinfo($file, PATHINFO_FILENAME); |
| 121 | + $normalized = $this->normalize($basename); |
| 122 | + if ($normalized === '') { |
| 123 | + continue; |
| 124 | + } |
| 125 | + |
| 126 | + $index[$normalized] = trim($relativeDirectory . '/' . $file, '/'); |
| 127 | + } |
| 128 | + |
| 129 | + return $index; |
| 130 | + } |
| 131 | + |
| 132 | + private function normalize(string $value): string |
| 133 | + { |
| 134 | + return Str::slug(trim($value)); |
| 135 | + } |
| 136 | +} |
| 137 | + |
0 commit comments