22
33namespace ApiClients \Client \Twitter ;
44
5+ use ApiClients \Client \Twitter \Resource \Async \Profile ;
6+ use ApiClients \Client \Twitter \Resource \ProfileInterface ;
57use ApiClients \Client \Twitter \Resource \TweetInterface ;
68use 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 ;
717use React \EventLoop \Factory as LoopFactory ;
818use React \EventLoop \LoopInterface ;
919use function Clue \React \Block \await ;
1020
1121final class Client implements ClientInterface
1222{
23+ /**
24+ * @var string
25+ */
26+ private $ consumerKey ;
27+
28+ /**
29+ * @var string
30+ */
31+ private $ consumerSecret ;
32+
1333 /**
1434 * @var LoopInterface
1535 */
@@ -18,32 +38,83 @@ final class Client implements ClientInterface
1838 /**
1939 * @var AsyncClient
2040 */
41+ protected $ asyncClient ;
42+
43+ /**
44+ * @var FoundationClient
45+ */
2146 protected $ client ;
2247
2348 /**
2449 * @var StreamingClient
2550 */
2651 protected $ streamingClient ;
2752
53+ /**
54+ * @var array
55+ */
56+ protected $ options ;
57+
2858 public function __construct (
2959 string $ consumerKey ,
3060 string $ consumerSecret
3161 ) {
62+ $ this ->consumerKey = $ consumerKey ;
63+ $ this ->consumerSecret = $ consumerSecret ;
3264 $ 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 );
3475 }
3576
3677 public function withAccessToken (string $ accessToken , string $ accessTokenSecret ): Client
3778 {
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+
3885 $ 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 );
4094 return $ clone ;
4195 }
4296
4397 public function withOutAccessToken (): Client
4498 {
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+
45109 $ 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+ ));
47118 return $ clone ;
48119 }
49120
@@ -52,8 +123,8 @@ public function stream(): StreamingClient
52123 if (!($ this ->streamingClient instanceof StreamingClient)) {
53124 $ this ->streamingClient = new StreamingClient (
54125 $ this ->loop ,
55- $ this ->client ->getCommandBus (),
56- $ this ->client ->stream ()
126+ $ this ->asyncClient ->getCommandBus (),
127+ $ this ->asyncClient ->stream ()
57128 );
58129 }
59130
@@ -63,15 +134,25 @@ public function stream(): StreamingClient
63134 public function tweet (string $ tweet ): TweetInterface
64135 {
65136 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+ }),
67148 $ this ->loop
68149 );
69150 }
70151
71152 public function user (string $ tweet ): UserInterface
72153 {
73154 return await (
74- $ this ->client ->user ($ tweet ),
155+ $ this ->asyncClient ->user ($ tweet ),
75156 $ this ->loop
76157 );
77158 }
0 commit comments