<?php
namespace App\Controller;
use App\WhatsApp\WhatsAppLinkGenerator;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
#[Route('/wa', name: 'whatsapp_')]
class WhatsAppController extends AbstractController
{
private WhatsAppLinkGenerator $linkGenerator;
public function __construct(WhatsAppLinkGenerator $linkGenerator)
{
$this->linkGenerator = $linkGenerator;
}
#[Route(name: 'link')]
public function link()
{
$companyName = get_bloginfo('name');
$text = <<<EOD
Hola *{$companyName}*, me gustaría recibir más información sobre:
EOD;
return $this->redirect($this->linkGenerator->generateWhatsAppUrl($text));
}
#[Route('/product/{slug}', name: 'product')]
public function product(string $slug)
{
$productID = get_page_by_path($slug, OBJECT, 'product')?->ID;
$product = wc_get_product($productID);
if (!$product) {
throw new NotFoundHttpException();
}
$link = $product->get_permalink();
$name = $product->get_name();
$companyName = get_bloginfo('name');
$text = <<<EOD
Hola *{$companyName}*, me gustaría recibir más información sobre {$name} que he encontrado en $link.
EOD;
return $this->redirect($this->linkGenerator->generateWhatsAppUrl($text));
}
#[Route('/share/product/{slug}', name: 'share_product')]
public function shareProduct(string $slug)
{
$productID = get_page_by_path($slug, OBJECT, 'product')?->ID;
$product = wc_get_product($productID);
if (!$product) {
throw new NotFoundHttpException();
}
$link = $product->get_permalink();
$name = $product->get_name();
$companyName = get_bloginfo('name');
$sale = $product->is_type('variable') ? $product->get_variation_sale_price('min', true) + 0 : $product->get_sale_price();
$price = $product->is_type('variable') ? $product->get_variation_regular_price('min', true) + 0 : $product->get_regular_price();
$productPrice = $price ? ' por ' . str_replace(',00', '', $sale ?? $price) . '€' : '';
$text = <<<EOD
Mira qué he encontrado en *{$companyName}*, ¡un {$name}{$productPrice}! $link.
EOD;
return $this->redirect($this->linkGenerator->generateWhatsAppShare($text));
}
}