| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- <?php
-
- namespace App\Mail;
-
- use App\Models\Order;
- use App\Models\Store;
- use App\StorePlan\PlanSchema;
- use App\Support\MalaysianPhone;
- use Illuminate\Support\Facades\Storage;
-
- /**
- * How a store looks in the emails its shoppers get: its name, colours, fonts and corners,
- * taken from the design shoppers see. Plus the links the emails need.
- *
- * Emails cannot load the store's web fonts reliably, so each font comes with a safe fallback
- * of the same kind (serif or sans-serif).
- */
- final readonly class StoreBrand
- {
- /** Used when a store has no design yet. Calm and neutral. */
- private const array DEFAULT_COLORS = [
- 'background' => '#F6F5F2',
- 'surface' => '#FFFFFF',
- 'foreground' => '#1F1F1F',
- 'muted' => '#6B6B6B',
- 'primary' => '#1F1F1F',
- 'primaryForeground' => '#FFFFFF',
- 'accent' => '#C9A227',
- 'border' => '#E4E1DA',
- ];
-
- private const array SERIF_FONTS = ['playfair-display', 'lora', 'fraunces', 'cormorant-garamond', 'dm-serif-display', 'libre-baskerville'];
-
- /**
- * @param array<string, string> $colors
- */
- private function __construct(
- public Store $store,
- public string $name,
- public array $colors,
- public string $headingFont,
- public string $bodyFont,
- /** Corner rounding for cards, in pixels. */
- public int $radius,
- /** Corner rounding for buttons, in pixels. */
- public int $buttonRadius,
- public bool $malay,
- ) {}
-
- public static function for(Store $store): self
- {
- // What shoppers see: the published design, or the draft while the store is not live yet.
- $plan = $store->published_plan ?? $store->plan ?? [];
- $theme = $plan['theme'] ?? [];
-
- $colors = self::DEFAULT_COLORS;
-
- foreach (PlanSchema::COLORS as $key) {
- $value = $theme['colors'][$key] ?? null;
-
- if (is_string($value) && preg_match('/^#[0-9A-Fa-f]{6}$/', $value) === 1) {
- $colors[$key] = $value;
- }
- }
-
- $radius = match ($theme['radius'] ?? 'medium') {
- 'none' => 0,
- 'small' => 4,
- 'large' => 16,
- 'full' => 20,
- default => 8,
- };
-
- return new self(
- store: $store,
- name: self::logoText($plan) ?? $store->name,
- colors: $colors,
- headingFont: self::fontStack($theme['fonts']['heading'] ?? null),
- bodyFont: self::fontStack($theme['fonts']['body'] ?? null),
- radius: $radius,
- buttonRadius: ($theme['radius'] ?? null) === 'full' ? 999 : $radius,
- malay: $store->language->value === 'ms',
- );
- }
-
- /**
- * The uploaded logo's full web address, for the top of the email. Designed and AI-drawn logos are not
- * pictures that email apps can show, so those emails show the store's name instead.
- *
- * @return array{url: string, width: int, height: int}|null
- */
- public function logo(): ?array
- {
- $logo = $this->store->logo;
-
- if (($logo['type'] ?? null) !== 'upload') {
- return null;
- }
-
- $url = Storage::disk('public')->url($logo['path']);
-
- // Shown at most 48 pixels high and 220 wide, keeping its shape.
- $scale = min(48 / max(1, $logo['height']), 220 / max(1, $logo['width']), 1);
-
- return [
- 'url' => str_starts_with($url, 'http') ? $url : url($url),
- 'width' => (int) round($logo['width'] * $scale),
- 'height' => (int) round($logo['height'] * $scale),
- ];
- }
-
- /**
- * The email address shoppers can write to, when the merchant set one.
- */
- public function contactEmail(): ?string
- {
- return $this->store->contact_email;
- }
-
- public function color(string $key): string
- {
- return $this->colors[$key];
- }
-
- /**
- * The shopper's order page. A live store's page is on the store's own address; a store that is not
- * published yet only has the merchant's preview, so the link opens that.
- */
- public function orderUrl(Order $order): string
- {
- if (! $this->store->isPublished()) {
- return route('stores.preview.order', [$this->store->slug, $order->public_id]);
- }
-
- return $this->storeUrl().'/orders/'.$order->public_id;
- }
-
- /**
- * The store's own home page.
- */
- public function storeUrl(): string
- {
- if (! $this->store->isPublished()) {
- return route('stores.preview', $this->store->slug);
- }
-
- // Built from APP_URL, because emails are also sent from the webhook, where there is no shopper's visit.
- $app = parse_url((string) config('app.url'));
- $scheme = $app['scheme'] ?? 'https';
- $port = isset($app['port']) ? ':'.$app['port'] : '';
-
- return $scheme.'://'.$this->store->slug.'.'.config('storefront.domain').$port;
- }
-
- /**
- * A WhatsApp chat with the store about this order, or null when the store has no WhatsApp number.
- */
- public function whatsappUrl(Order $order): ?string
- {
- if ($this->store->whatsapp_number === null || $this->store->whatsapp_number === '') {
- return null;
- }
-
- $text = $this->malay
- ? "Hai {$this->store->name}, saya ada soalan tentang pesanan saya #{$order->number}."
- : "Hi {$this->store->name}, I have a question about my order #{$order->number}.";
-
- return MalaysianPhone::whatsappLink($this->store->whatsapp_number, $text);
- }
-
- public function money(int $sen): string
- {
- return 'RM '.number_format($sen / 100, 2);
- }
-
- /**
- * The words shown in the header, from the store's own header section when it has one.
- *
- * @param array<string, mixed> $plan
- */
- private static function logoText(array $plan): ?string
- {
- // Version 2 plans keep one shared header; older ones have it among the sections.
- $header = $plan['header'] ?? collect($plan['sections'] ?? [])->firstWhere('type', 'header');
- $text = trim((string) ($header['content']['logoText'] ?? ''));
-
- return $text !== '' ? $text : null;
- }
-
- private static function fontStack(?string $key): string
- {
- $family = PlanSchema::FONTS[$key] ?? null;
- $fallback = in_array($key, self::SERIF_FONTS, true)
- ? "Georgia, 'Times New Roman', serif"
- : 'Helvetica, Arial, sans-serif';
-
- return $family !== null ? "'{$family}', {$fallback}" : $fallback;
- }
- }
|