1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-12 14:12:44 +01:00

default resource broadcast

This commit is contained in:
Benjamin Beganović 2024-10-18 18:32:42 +02:00
parent 95e6ed49de
commit e1da6c1ead

View File

@ -12,12 +12,13 @@
namespace App\Utils\Traits\Invoice\Broadcasting;
use App\Models\BaseModel;
use App\Transformers\ArraySerializer;
use Illuminate\Broadcasting\PrivateChannel;
use League\Fractal\Manager;
use League\Fractal\Resource\Item;
trait DefaultInvoiceBroadcast
trait DefaultResourceBroadcast
{
public function broadcastOn(): array
{
@ -26,17 +27,40 @@ trait DefaultInvoiceBroadcast
];
}
public function broadcastModel(): BaseModel
{
throw new \LogicException('Make sure to pass a model to the broadcastModel method.');
}
public function broadcastManager(Manager $manager): Manager
{
return $manager;
}
public function broadcastWith(): array
{
$entity = $this->broadcastModel();
$manager = new Manager();
$manager->setSerializer(new ArraySerializer());
$class = sprintf('App\\Transformers\\%sTransformer', class_basename($this->invoice));
$class = sprintf('App\\Transformers\\%sTransformer', class_basename($entity));
$transformer = new $class();
$resource = new Item($this->invoice, $transformer, $this->invoice->getEntityType());
$resource = new Item($entity, $transformer, $entity->getEntityType());
$manager = $this->broadcastManager($manager);
$data = $manager->createData($resource)->toArray();
return [...$data, 'x-socket-id' => $this->socket];
$data = [...$data];
if (\property_exists($this, 'socket')) {
$data['x-socket-id'] = $this->socket;
}
return $data;
}
}