Skip to content

Commit 8d5f752

Browse files
committed
Move event handling to a dedicated Dispatchable trait
1 parent 0aeac98 commit 8d5f752

21 files changed

+80
-28
lines changed

src/Client.php

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ public function __construct(
4242
*/
4343
public function completions(): Completions
4444
{
45-
return new Completions($this->transporter, $this->events);
45+
return (new Completions($this->transporter))
46+
->setEventDispatcher($this->events);
4647
}
4748

4849
/**
@@ -52,7 +53,8 @@ public function completions(): Completions
5253
*/
5354
public function chat(): Chat
5455
{
55-
return new Chat($this->transporter, $this->events);
56+
return (new Chat($this->transporter))
57+
->setEventDispatcher($this->events);
5658
}
5759

5860
/**
@@ -62,7 +64,8 @@ public function chat(): Chat
6264
*/
6365
public function embeddings(): Embeddings
6466
{
65-
return new Embeddings($this->transporter, $this->events);
67+
return (new Embeddings($this->transporter))
68+
->setEventDispatcher($this->events);
6669
}
6770

6871
/**
@@ -72,7 +75,8 @@ public function embeddings(): Embeddings
7275
*/
7376
public function audio(): Audio
7477
{
75-
return new Audio($this->transporter, $this->events);
78+
return (new Audio($this->transporter))
79+
->setEventDispatcher($this->events);
7680
}
7781

7882
/**
@@ -82,7 +86,8 @@ public function audio(): Audio
8286
*/
8387
public function edits(): Edits
8488
{
85-
return new Edits($this->transporter, $this->events);
89+
return (new Edits($this->transporter))
90+
->setEventDispatcher($this->events);
8691
}
8792

8893
/**
@@ -92,7 +97,8 @@ public function edits(): Edits
9297
*/
9398
public function files(): Files
9499
{
95-
return new Files($this->transporter, $this->events);
100+
return (new Files($this->transporter))
101+
->setEventDispatcher($this->events);
96102
}
97103

98104
/**
@@ -102,7 +108,8 @@ public function files(): Files
102108
*/
103109
public function models(): Models
104110
{
105-
return new Models($this->transporter, $this->events);
111+
return (new Models($this->transporter))
112+
->setEventDispatcher($this->events);
106113
}
107114

108115
/**
@@ -112,7 +119,8 @@ public function models(): Models
112119
*/
113120
public function fineTuning(): FineTuning
114121
{
115-
return new FineTuning($this->transporter, $this->events);
122+
return (new FineTuning($this->transporter))
123+
->setEventDispatcher($this->events);
116124
}
117125

118126
/**
@@ -124,7 +132,8 @@ public function fineTuning(): FineTuning
124132
*/
125133
public function fineTunes(): FineTunes
126134
{
127-
return new FineTunes($this->transporter, $this->events);
135+
return (new FineTunes($this->transporter))
136+
->setEventDispatcher($this->events);
128137
}
129138

130139
/**
@@ -134,7 +143,8 @@ public function fineTunes(): FineTunes
134143
*/
135144
public function moderations(): Moderations
136145
{
137-
return new Moderations($this->transporter, $this->events);
146+
return (new Moderations($this->transporter))
147+
->setEventDispatcher($this->events);
138148
}
139149

140150
/**
@@ -144,7 +154,8 @@ public function moderations(): Moderations
144154
*/
145155
public function images(): Images
146156
{
147-
return new Images($this->transporter, $this->events);
157+
return (new Images($this->transporter))
158+
->setEventDispatcher($this->events);
148159
}
149160

150161
/**
@@ -154,7 +165,8 @@ public function images(): Images
154165
*/
155166
public function assistants(): Assistants
156167
{
157-
return new Assistants($this->transporter, $this->events);
168+
return (new Assistants($this->transporter))
169+
->setEventDispatcher($this->events);
158170
}
159171

160172
/**
@@ -164,6 +176,7 @@ public function assistants(): Assistants
164176
*/
165177
public function threads(): ThreadsContract
166178
{
167-
return new Threads($this->transporter, $this->events);
179+
return (new Threads($this->transporter))
180+
->setEventDispatcher($this->events);
168181
}
169182
}

src/Resources/Assistants.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
final class Assistants implements AssistantsContract
1717
{
18+
use Concerns\Dispatchable;
1819
use Concerns\Transportable;
1920

2021
/**
@@ -125,6 +126,7 @@ public function list(array $parameters = []): AssistantListResponse
125126
*/
126127
public function files(): AssistantsFilesContract
127128
{
128-
return new AssistantsFiles($this->transporter, $this->events);
129+
return (new AssistantsFiles($this->transporter))
130+
->setEventDispatcher($this->events);
129131
}
130132
}

src/Resources/AssistantsFiles.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
final class AssistantsFiles implements AssistantsFilesContract
1616
{
17+
use Concerns\Dispatchable;
1718
use Concerns\Transportable;
1819

1920
/**

src/Resources/Audio.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
final class Audio implements AudioContract
1616
{
17+
use Concerns\Dispatchable;
1718
use Concerns\Transportable;
1819

1920
/**

src/Resources/Chat.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
final class Chat implements ChatContract
1616
{
17+
use Concerns\Dispatchable;
1718
use Concerns\Streamable;
1819
use Concerns\Transportable;
1920

src/Resources/Completions.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
final class Completions implements CompletionsContract
1616
{
17+
use Concerns\Dispatchable;
1718
use Concerns\Streamable;
1819
use Concerns\Transportable;
1920

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenAI\Resources\Concerns;
6+
7+
use OpenAI\Contracts\DispatcherContract;
8+
9+
trait Dispatchable
10+
{
11+
private DispatcherContract $events;
12+
13+
public function setEventDispatcher(DispatcherContract $events): static
14+
{
15+
$this->events = $events;
16+
17+
return $this;
18+
}
19+
20+
public function event(object $event): void
21+
{
22+
$this->events->dispatch($event);
23+
}
24+
}

src/Resources/Concerns/Transportable.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,15 @@
44

55
namespace OpenAI\Resources\Concerns;
66

7-
use OpenAI\Contracts\DispatcherContract;
87
use OpenAI\Contracts\TransporterContract;
98

109
trait Transportable
1110
{
1211
/**
1312
* Creates a Client instance with the given API token.
1413
*/
15-
public function __construct(
16-
private readonly TransporterContract $transporter,
17-
private readonly DispatcherContract $events,
18-
) {
19-
// ..
20-
}
21-
22-
public function event(object $event): void
14+
public function __construct(private readonly TransporterContract $transporter)
2315
{
24-
$this->events->dispatch($event);
16+
// ..
2517
}
2618
}

src/Resources/Edits.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
final class Edits implements EditsContract
1414
{
15+
use Concerns\Dispatchable;
1516
use Concerns\Transportable;
1617

1718
/**

src/Resources/Embeddings.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
final class Embeddings implements EmbeddingsContract
1414
{
15+
use Concerns\Dispatchable;
1516
use Concerns\Transportable;
1617

1718
/**

0 commit comments

Comments
 (0)