fix tests
This commit is contained in:
@@ -8,9 +8,7 @@ use Illuminate\Support\Facades\Log;
|
||||
|
||||
class WhatsappInviteEmailService
|
||||
{
|
||||
public function __construct(private EmailService $emailService)
|
||||
{
|
||||
}
|
||||
public function __construct(private EmailService $emailService) {}
|
||||
|
||||
public function send(array $payload): array
|
||||
{
|
||||
@@ -18,12 +16,13 @@ class WhatsappInviteEmailService
|
||||
$cc = array_values(array_unique(array_filter((array) ($payload['cc_emails'] ?? []))));
|
||||
$cc = array_values(array_diff($cc, $to));
|
||||
|
||||
if (empty($to) && !empty($cc)) {
|
||||
if (empty($to) && ! empty($cc)) {
|
||||
$to = $cc;
|
||||
$cc = [];
|
||||
}
|
||||
if (empty($to)) {
|
||||
Log::warning('WhatsappInvite: no recipients after normalization.');
|
||||
|
||||
return ['ok' => false, 'message' => 'No recipients provided.'];
|
||||
}
|
||||
|
||||
@@ -34,6 +33,7 @@ class WhatsappInviteEmailService
|
||||
$items = $this->buildItems($sections, $links);
|
||||
if (empty($items)) {
|
||||
Log::warning('WhatsappInvite: no invite items built.');
|
||||
|
||||
return ['ok' => false, 'message' => 'No invite links found.'];
|
||||
}
|
||||
|
||||
@@ -77,12 +77,12 @@ class WhatsappInviteEmailService
|
||||
|
||||
foreach ($sections as $section) {
|
||||
$sid = isset($section['class_section_id']) ? (int) $section['class_section_id'] : 0;
|
||||
$name = trim((string) ($section['class_section_name'] ?? ($sid ? ('Section ' . $sid) : 'Class')));
|
||||
$name = trim((string) ($section['class_section_name'] ?? ($sid ? ('Section '.$sid) : 'Class')));
|
||||
$url = trim((string) ($section['invite_link'] ?? ''));
|
||||
if ($url === '') {
|
||||
continue;
|
||||
}
|
||||
$key = $sid . '|' . $url;
|
||||
$key = $sid.'|'.$url;
|
||||
if (isset($seen[$key])) {
|
||||
continue;
|
||||
}
|
||||
@@ -96,20 +96,20 @@ class WhatsappInviteEmailService
|
||||
];
|
||||
}
|
||||
|
||||
if (empty($items) && !empty($links)) {
|
||||
if (empty($items) && ! empty($links)) {
|
||||
foreach ($links as $i => $url) {
|
||||
$url = trim((string) $url);
|
||||
if ($url === '') {
|
||||
continue;
|
||||
}
|
||||
$key = '0|' . $url;
|
||||
$key = '0|'.$url;
|
||||
if (isset($seen[$key])) {
|
||||
continue;
|
||||
}
|
||||
$seen[$key] = true;
|
||||
$items[] = [
|
||||
'class_section_id' => 0,
|
||||
'class_section_name' => 'Class Link ' . ($i + 1),
|
||||
'class_section_name' => 'Class Link '.($i + 1),
|
||||
'invite_link' => $url,
|
||||
'qr_src' => $this->generateQrImage($url),
|
||||
];
|
||||
@@ -126,21 +126,23 @@ class WhatsappInviteEmailService
|
||||
}
|
||||
|
||||
$dir = storage_path('app/qrcodes');
|
||||
if (!is_dir($dir)) {
|
||||
if (!@mkdir($dir, 0775, true) && !is_dir($dir)) {
|
||||
if (! is_dir($dir)) {
|
||||
if (! @mkdir($dir, 0775, true) && ! is_dir($dir)) {
|
||||
Log::error('QR: cannot create dir', ['dir' => $dir]);
|
||||
|
||||
return $this->qrFallback($inviteLink);
|
||||
}
|
||||
}
|
||||
if (!is_writable($dir)) {
|
||||
if (! is_writable($dir)) {
|
||||
Log::error('QR: dir not writable', ['dir' => $dir]);
|
||||
|
||||
return $this->qrFallback($inviteLink);
|
||||
}
|
||||
|
||||
$filename = 'wa_' . md5($inviteLink) . '.png';
|
||||
$path = $dir . DIRECTORY_SEPARATOR . $filename;
|
||||
$filename = 'wa_'.md5($inviteLink).'.png';
|
||||
$path = $dir.DIRECTORY_SEPARATOR.$filename;
|
||||
|
||||
if (!is_file($path)) {
|
||||
if (! is_file($path)) {
|
||||
$v5BuilderClass = '\\Endroid\\QrCode\\Builder\\Builder';
|
||||
$v5WriterClass = '\\Endroid\\QrCode\\Writer\\PngWriter';
|
||||
$v5Encoding = '\\Endroid\\QrCode\\Encoding\\Encoding';
|
||||
@@ -157,7 +159,7 @@ class WhatsappInviteEmailService
|
||||
if (class_exists($v5BuilderClass) && class_exists($v5WriterClass)) {
|
||||
try {
|
||||
$builder = $v5BuilderClass::create()
|
||||
->writer(new $v5WriterClass())
|
||||
->writer(new $v5WriterClass)
|
||||
->data($inviteLink);
|
||||
if (class_exists($v5Encoding)) {
|
||||
$builder->encoding(new $v5Encoding('UTF-8'));
|
||||
@@ -165,11 +167,11 @@ class WhatsappInviteEmailService
|
||||
$result = $builder->size(500)->margin(2)->build();
|
||||
$result->saveToFile($path);
|
||||
} catch (\Throwable $e) {
|
||||
$lastErr = 'Endroid v5: ' . $e->getMessage();
|
||||
$lastErr = 'Endroid v5: '.$e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
if (!is_file($path) && class_exists($writerClass)) {
|
||||
if (! is_file($path) && class_exists($writerClass)) {
|
||||
try {
|
||||
if (class_exists($qrClass) && method_exists($qrClass, 'create')) {
|
||||
$qr = $qrClass::create($inviteLink);
|
||||
@@ -183,7 +185,7 @@ class WhatsappInviteEmailService
|
||||
if (method_exists($qr, 'setMargin')) {
|
||||
$qr->setMargin(2);
|
||||
}
|
||||
$writer = new $writerClass();
|
||||
$writer = new $writerClass;
|
||||
$result = $writer->write($qr);
|
||||
if (method_exists($result, 'saveToFile')) {
|
||||
$result->saveToFile($path);
|
||||
@@ -193,21 +195,21 @@ class WhatsappInviteEmailService
|
||||
}
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
$lastErr = 'Endroid v3/v4: ' . $e->getMessage();
|
||||
$lastErr = 'Endroid v3/v4: '.$e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
if (!is_file($path) && class_exists($chlQRCode) && class_exists($chlQROptions)) {
|
||||
if (! is_file($path) && class_exists($chlQRCode) && class_exists($chlQROptions)) {
|
||||
try {
|
||||
$optsArr = [
|
||||
'version' => 5,
|
||||
'outputType' => constant($chlQRCode . '::OUTPUT_IMAGE_PNG'),
|
||||
'outputType' => constant($chlQRCode.'::OUTPUT_IMAGE_PNG'),
|
||||
'scale' => 6,
|
||||
'imageBase64' => false,
|
||||
'quietzoneSize' => 2,
|
||||
];
|
||||
if (class_exists($chlQRMatrix) && defined($chlQRMatrix . '::ECC_H')) {
|
||||
$optsArr['eccLevel'] = constant($chlQRMatrix . '::ECC_H');
|
||||
if (class_exists($chlQRMatrix) && defined($chlQRMatrix.'::ECC_H')) {
|
||||
$optsArr['eccLevel'] = constant($chlQRMatrix.'::ECC_H');
|
||||
}
|
||||
|
||||
$opts = new $chlQROptions($optsArr);
|
||||
@@ -215,11 +217,11 @@ class WhatsappInviteEmailService
|
||||
$png = $qrcode->render($inviteLink);
|
||||
file_put_contents($path, $png);
|
||||
} catch (\Throwable $e) {
|
||||
$lastErr = 'chillerlan: ' . $e->getMessage();
|
||||
$lastErr = 'chillerlan: '.$e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
if (!is_file($path) && $lastErr) {
|
||||
if (! is_file($path) && $lastErr) {
|
||||
Log::error('QR generation failed', ['error' => $lastErr]);
|
||||
}
|
||||
}
|
||||
@@ -227,7 +229,7 @@ class WhatsappInviteEmailService
|
||||
if (is_file($path)) {
|
||||
$data = @file_get_contents($path);
|
||||
if ($data !== false) {
|
||||
return 'data:image/png;base64,' . base64_encode($data);
|
||||
return 'data:image/png;base64,'.base64_encode($data);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -237,6 +239,7 @@ class WhatsappInviteEmailService
|
||||
private function qrFallback(string $inviteLink): string
|
||||
{
|
||||
$link = rawurlencode($inviteLink);
|
||||
return 'https://api.qrserver.com/v1/create-qr-code/?size=260x260&qzone=2&data=' . $link;
|
||||
|
||||
return 'https://api.qrserver.com/v1/create-qr-code/?size=260x260&qzone=2&data='.$link;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user