support for credit was updated

This commit is contained in:
Benjamin Beganović 2024-10-17 17:59:21 +02:00
parent b40cdff451
commit caf36a1594
1 changed files with 35 additions and 1 deletions

View File

@ -14,10 +14,15 @@ namespace App\Events\Credit;
use App\Models\Company; use App\Models\Company;
use App\Models\Credit; use App\Models\Credit;
use Illuminate\Broadcasting\InteractsWithSockets; use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable; use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use League\Fractal\Manager;
use App\Transformers\ArraySerializer;
use League\Fractal\Resource\Item;
class CreditWasUpdated class CreditWasUpdated implements ShouldBroadcast
{ {
use Dispatchable; use Dispatchable;
use InteractsWithSockets; use InteractsWithSockets;
@ -41,5 +46,34 @@ class CreditWasUpdated
$this->credit = $credit; $this->credit = $credit;
$this->company = $company; $this->company = $company;
$this->event_vars = $event_vars; $this->event_vars = $event_vars;
$this->dontBroadcastToCurrentUser();
}
/**
* @inheritDoc
*/
public function broadcastOn()
{
return [
new PrivateChannel("company-{$this->company->company_key}"),
];
}
/**
* @inheritDoc
*/
public function broadcastWith(): array
{
$manager = new Manager();
$manager->setSerializer(new ArraySerializer());
$class = sprintf('App\\Transformers\\%sTransformer', class_basename($this->credit));
$transformer = new $class();
$resource = new Item($this->credit, $transformer, $this->credit->getEntityType());
$data = $manager->createData($resource)->toArray();
return [...$data, 'x-socket-id' => $this->socket];
} }
} }