2
2
3
3
namespace ApiClients \Client \Twitter ;
4
4
5
+ use ApiClients \Client \Twitter \Resource \Async \Profile ;
6
+ use ApiClients \Client \Twitter \Resource \ProfileInterface ;
5
7
use ApiClients \Client \Twitter \Resource \TweetInterface ;
6
8
use ApiClients \Client \Twitter \Resource \UserInterface ;
9
+ use ApiClients \Foundation \Client as FoundationClient ;
10
+ use ApiClients \Foundation \Factory ;
11
+ use ApiClients \Foundation \Hydrator \CommandBus \Command \BuildSyncFromAsyncCommand ;
12
+ use ApiClients \Foundation \Oauth1 \Middleware \Oauth1Middleware ;
13
+ use ApiClients \Foundation \Oauth1 \Options as Oauth1Options ;
14
+ use ApiClients \Foundation \Options ;
15
+ use ApiClients \Foundation \Transport \Options as TransportOptions ;
16
+ use ApiClients \Tools \Psr7 \Oauth1 \Definition ;
7
17
use React \EventLoop \Factory as LoopFactory ;
8
18
use React \EventLoop \LoopInterface ;
9
19
use function Clue \React \Block \await ;
10
20
11
21
final class Client implements ClientInterface
12
22
{
23
+ /**
24
+ * @var string
25
+ */
26
+ private $ consumerKey ;
27
+
28
+ /**
29
+ * @var string
30
+ */
31
+ private $ consumerSecret ;
32
+
13
33
/**
14
34
* @var LoopInterface
15
35
*/
@@ -18,32 +38,83 @@ final class Client implements ClientInterface
18
38
/**
19
39
* @var AsyncClient
20
40
*/
41
+ protected $ asyncClient ;
42
+
43
+ /**
44
+ * @var FoundationClient
45
+ */
21
46
protected $ client ;
22
47
23
48
/**
24
49
* @var StreamingClient
25
50
*/
26
51
protected $ streamingClient ;
27
52
53
+ /**
54
+ * @var array
55
+ */
56
+ protected $ options ;
57
+
28
58
public function __construct (
29
59
string $ consumerKey ,
30
60
string $ consumerSecret
31
61
) {
62
+ $ this ->consumerKey = $ consumerKey ;
63
+ $ this ->consumerSecret = $ consumerSecret ;
32
64
$ this ->loop = LoopFactory::create ();
33
- $ this ->client = new AsyncClient ($ consumerKey , $ consumerSecret , $ this ->loop );
65
+
66
+ $ this ->options = ApiSettings::getOptions (
67
+ $ consumerKey ,
68
+ $ consumerSecret ,
69
+ 'Sync '
70
+ );
71
+
72
+ $ this ->client = Factory::create ($ this ->loop , $ this ->options );
73
+
74
+ $ this ->asyncClient = new AsyncClient ($ consumerKey , $ consumerSecret , $ this ->loop , [], $ this ->client );
34
75
}
35
76
36
77
public function withAccessToken (string $ accessToken , string $ accessTokenSecret ): Client
37
78
{
79
+ $ options = $ this ->options ;
80
+ // @codingStandardsIgnoreStart
81
+ $ options [Options::TRANSPORT_OPTIONS ][TransportOptions::DEFAULT_REQUEST_OPTIONS ][Oauth1Middleware::class][Oauth1Options::ACCESS_TOKEN ] = new Definition \AccessToken ($ accessToken );
82
+ $ options [Options::TRANSPORT_OPTIONS ][TransportOptions::DEFAULT_REQUEST_OPTIONS ][Oauth1Middleware::class][Oauth1Options::TOKEN_SECRET ] = new Definition \TokenSecret ($ accessTokenSecret );
83
+ // @codingStandardsIgnoreEnd
84
+
38
85
$ clone = clone $ this ;
39
- $ clone ->client = $ this ->client ->withAccessToken ($ accessToken , $ accessTokenSecret );
86
+ $ clone ->client = Factory::create ($ this ->loop , $ options );
87
+ $ clone ->asyncClient = (new AsyncClient (
88
+ $ this ->consumerKey ,
89
+ $ this ->consumerSecret ,
90
+ $ this ->loop ,
91
+ [],
92
+ $ this ->client
93
+ ))->withAccessToken ($ accessToken , $ accessTokenSecret );
40
94
return $ clone ;
41
95
}
42
96
43
97
public function withOutAccessToken (): Client
44
98
{
99
+ $ options = $ this ->options ;
100
+ // @codingStandardsIgnoreStart
101
+ if (isset ($ options [Options::TRANSPORT_OPTIONS ][TransportOptions::DEFAULT_REQUEST_OPTIONS ][Oauth1Middleware::class][Oauth1Options::ACCESS_TOKEN ])) {
102
+ unset($ options [Options::TRANSPORT_OPTIONS ][TransportOptions::DEFAULT_REQUEST_OPTIONS ][Oauth1Middleware::class][Oauth1Options::ACCESS_TOKEN ]);
103
+ }
104
+ if (isset ($ options [Options::TRANSPORT_OPTIONS ][TransportOptions::DEFAULT_REQUEST_OPTIONS ][Oauth1Middleware::class][Oauth1Options::TOKEN_SECRET ])) {
105
+ unset($ options [Options::TRANSPORT_OPTIONS ][TransportOptions::DEFAULT_REQUEST_OPTIONS ][Oauth1Middleware::class][Oauth1Options::TOKEN_SECRET ]);
106
+ }
107
+ // @codingStandardsIgnoreEnd
108
+
45
109
$ clone = clone $ this ;
46
- $ clone ->client = $ this ->client ->withOutAccessToken ();
110
+ $ clone ->client = Factory::create ($ this ->loop , $ options );
111
+ $ clone ->asyncClient = (new AsyncClient (
112
+ $ this ->consumerKey ,
113
+ $ this ->consumerSecret ,
114
+ $ this ->loop ,
115
+ [],
116
+ $ this ->client
117
+ ));
47
118
return $ clone ;
48
119
}
49
120
@@ -52,8 +123,8 @@ public function stream(): StreamingClient
52
123
if (!($ this ->streamingClient instanceof StreamingClient)) {
53
124
$ this ->streamingClient = new StreamingClient (
54
125
$ this ->loop ,
55
- $ this ->client ->getCommandBus (),
56
- $ this ->client ->stream ()
126
+ $ this ->asyncClient ->getCommandBus (),
127
+ $ this ->asyncClient ->stream ()
57
128
);
58
129
}
59
130
@@ -63,15 +134,25 @@ public function stream(): StreamingClient
63
134
public function tweet (string $ tweet ): TweetInterface
64
135
{
65
136
return await (
66
- $ this ->client ->tweet ($ tweet ),
137
+ $ this ->asyncClient ->tweet ($ tweet ),
138
+ $ this ->loop
139
+ );
140
+ }
141
+
142
+ public function profile (): ProfileInterface
143
+ {
144
+ return await (
145
+ $ this ->asyncClient ->profile ()->then (function (Profile $ profile ) {
146
+ return $ this ->client ->handle (new BuildSyncFromAsyncCommand (ProfileInterface::HYDRATE_CLASS , $ profile ));
147
+ }),
67
148
$ this ->loop
68
149
);
69
150
}
70
151
71
152
public function user (string $ tweet ): UserInterface
72
153
{
73
154
return await (
74
- $ this ->client ->user ($ tweet ),
155
+ $ this ->asyncClient ->user ($ tweet ),
75
156
$ this ->loop
76
157
);
77
158
}
0 commit comments