ldc-legaulois/app/Mail/Arcadia/HasOrderedAnItem.php
2026-09-25 16:25:31 +02:00

101 lines
3 KiB
PHP

<?php
namespace App\Mail\Arcadia;
use App\Enums\Arcadia\ArcadiaOrderType;
use App\Lib\Arcadia\HasOrderedAnItemData;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
class HasOrderedAnItem extends Mailable implements ShouldQueue
{
use Queueable;
use SerializesModels;
/**
* Email Data.
*
* @var \App\Lib\Arcadia\HasOrderedAnItemData
*/
private $data;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct(HasOrderedAnItemData $data)
{
$this->data = $data;
}
/**
* Build the message.
*
* @return $this
*
* @throws \RuntimeException If generating XML fails.
*/
public function build()
{
$xml = $this->makeXml();
// * Format xml.
$doc = new \DOMDocument('1.0', 'utf-8');
$doc->xmlStandalone = true;
$doc->preserveWhiteSpace = false;
$doc->formatOutput = true;
$doc->loadXML($xml);
$xml = $doc->saveXML();
if (! $xml) {
throw new \RuntimeException('Failed to make XML');
}
$fileName = Str::slug(sprintf(
'order_%s_%s_%s%s',
$this->data->order_num,
ArcadiaOrderType::make($this->data->state_order)->label(),
date('Ymd_His'),
\microtime(true)
)) . '.xml';
Storage::disk('arcadia')->put($fileName, $xml);
return $this->subject('Un object en vente à été commandé')
->markdown('emails.arcadiaOrders.default_markdown')
->attachData(
$xml,
$fileName,
['mime' => 'application/xml']
);
}
/**
* Render Arcadia xml for order.
*/
private function makeXml(): string
{
return view('emails.arcadiaOrders.default_xml', [
'operation' => $this->data->operation,
'support_code' => $this->data->support_code,
'label_type_support' => $this->data->operation,
'order_date' => $this->data->order_date,
'order_num' => $this->data->order_num,
'orderType' => ArcadiaOrderType::make($this->data->state_order),
'package_unit_weight' => $this->data->package_unit_weight,
'lastname' => $this->data->lastname,
'firstname' => $this->data->firstname,
'email' => $this->data->email,
'address' => $this->data->address,
'address2' => $this->data->address2,
'city' => $this->data->city,
'phone' => $this->data->phone,
'postcode' => $this->data->postcode,
])->render();
}
}