@@ -71,8 +71,14 @@ private function action(Context $context, array $opts = []): Response
7171 $ callback = ag ($ opts , Options::RAW_RESPONSE_CALLBACK , null );
7272 $ this ->logRequests = $ callback && ag ($ opts , Options::RAW_RESPONSE , false );
7373
74+ $ opts [Options::LOG_TO_WRITER ] = ag ($ opts , Options::LOG_TO_WRITER , static fn () => static function (string $ log ) {});
75+
7476 if (true === (bool ) ag ($ opts , Options::PLEX_EXTERNAL_USER , false )) {
7577 $ cls = fn () => $ this ->getExternalUsers ($ context , $ opts );
78+ $ opts [Options::LOG_TO_WRITER ](r ('Reading external user from cache? {state} ' , [
79+ 'state ' => true === (bool ) ag ($ opts , Options::NO_CACHE ) ? 'no ' : 'yes ' ,
80+ ]));
81+
7682 return true === (bool ) ag ($ opts , Options::NO_CACHE )
7783 ? $ cls ()
7884 : $ this ->tryCache (
@@ -89,6 +95,10 @@ private function action(Context $context, array $opts = []): Response
8995
9096 $ cls = fn () => $ this ->getHomeUsers ($ this ->getExternalUsers ($ context , $ opts ), $ context , $ opts );
9197
98+ $ opts [Options::LOG_TO_WRITER ](r ('Reading data from cache? {state} ' , [
99+ 'state ' => true === (bool ) ag ($ opts , Options::NO_CACHE ) ? 'no ' : 'yes ' ,
100+ ]));
101+
92102 $ data = true === (bool ) ag ($ opts , Options::NO_CACHE )
93103 ? $ cls ()
94104 : $ this ->tryCache (
@@ -192,7 +202,7 @@ private function getExternalUsers(Context $context, array $opts = []): Response
192202 'headers ' => [
193203 'Accept ' => 'application/xml ' ,
194204 ],
195- ]), $ context , $ url );
205+ ]), $ context , $ url, $ opts );
196206
197207 if (true !== (bool ) ag ($ opts , Options::GET_TOKENS ) || count ($ users ) < 1 ) {
198208 return new Response (status: true , response: $ users );
@@ -249,7 +259,7 @@ private function getExternalUsers(Context $context, array $opts = []): Response
249259 );
250260 }
251261
252- return $ this ->externalUsersTokens ($ users , $ context , $ url , $ response );
262+ return $ this ->externalUsersTokens ($ users , $ context , $ url , $ response, $ opts );
253263 }
254264
255265 /**
@@ -258,18 +268,18 @@ private function getExternalUsers(Context $context, array $opts = []): Response
258268 * @param iResponse $response
259269 * @param Context $context
260270 * @param iUri $url
271+ * @param array $opts The options.
261272 *
262273 * @return array Return processed response.
263274 * @throws iException if an error occurs during the request.
264275 */
265- private function processExternalUsers (iResponse $ response , Context $ context , iUri $ url ): array
276+ private function processExternalUsers (iResponse $ response , Context $ context , iUri $ url, array $ opts = [] ): array
266277 {
267278 $ content = simplexml_load_string ($ response ->getContent (false ));
268279 $ data = [];
269280 foreach ($ content ->User ?? [] as $ _user ) {
270281 $ user = [];
271282 // @INFO: This workaround is needed, for some reason array_map() doesn't work correctly on xml objects.
272- /** @noinspection PhpLoopCanBeConvertedToArrayMapInspection */
273283 foreach ($ _user ->attributes () as $ k => $ v ) {
274284 $ user [$ k ] = (string ) $ v ;
275285 }
@@ -296,7 +306,7 @@ private function processExternalUsers(iResponse $response, Context $context, iUr
296306 $ list = [];
297307 foreach ($ data as $ user ) {
298308 $ uuidStatus = preg_match ('/\/users\/(?<uuid>.+?)\/avatar/ ' , ag ($ user , 'thumb ' , '' ), $ matches );
299- $ list [] = [
309+ $ _user = [
300310 'id ' => (int ) ag ($ user , 'id ' ),
301311 'uuid ' => 1 === $ uuidStatus ? ag ($ matches , 'uuid ' ) : ag ($ user , 'invited_user ' ),
302312 'name ' => ag ($ user , ['username ' , 'title ' , 'email ' , 'id ' ], '?? ' ),
@@ -306,6 +316,17 @@ private function processExternalUsers(iResponse $response, Context $context, iUr
306316 'protected ' => 1 === (int ) ag ($ user , 'protected ' ),
307317 'updatedAt ' => 'external_user ' ,
308318 ];
319+
320+ $ list [] = $ _user ;
321+
322+ $ opts [Options::LOG_TO_WRITER ](r ("Processed external user '{name}' with id '{id}': {data}. " , [
323+ 'name ' => $ _user ['name ' ],
324+ 'id ' => $ _user ['id ' ],
325+ 'data ' => [
326+ 'local ' => array_to_json ($ _user ),
327+ 'remote ' => array_to_json ($ user ),
328+ ],
329+ ]));
309330 }
310331
311332 return $ list ;
@@ -318,12 +339,13 @@ private function processExternalUsers(iResponse $response, Context $context, iUr
318339 * @param Context $context The context.
319340 * @param iUri $url The URL.
320341 * @param iResponse $response The response.
342+ * @param array $opts The options.
321343 *
322344 * @return Response Return processed response.
323345 * @throws iException if an error occurs during the request.
324346 * @throws JsonException if an error occurs during the JSON parsing.
325347 */
326- private function externalUsersTokens (array $ users , Context $ context , iUri $ url , iResponse $ response ): Response
348+ private function externalUsersTokens (array $ users , Context $ context , iUri $ url , iResponse $ response, array $ opts = [] ): Response
327349 {
328350 if (count ($ users ) < 1 ) {
329351 return new Response (status: true , response: $ users );
@@ -356,6 +378,11 @@ private function externalUsersTokens(array $users, Context $context, iUri $url,
356378
357379 foreach ($ users as &$ user ) {
358380 if ((int ) ag ($ user , 'id ' ) !== (int ) ag ($ data , 'userID ' )) {
381+ $ opts [Options::LOG_TO_WRITER ](r ("Skipping token for user '{name}' with id '{id}' because it doesn't match with userID '{userID}' in the response. " , [
382+ 'name ' => ag ($ user , 'name ' ),
383+ 'id ' => ag ($ user , 'id ' ),
384+ 'userID ' => ag ($ data , 'userID ' ),
385+ ]));
359386 continue ;
360387 }
361388 $ user ['token ' ] = ag ($ data , 'accessToken ' );
@@ -470,6 +497,12 @@ private function processHomeUsers(
470497 continue ;
471498 }
472499
500+ $ opts [Options::LOG_TO_WRITER ](r ("Skipping external user '{name}' with id '{id}' because match a home user with id '{userId}' and name '{userName}'. " , [
501+ 'name ' => ag ($ extUser , 'name ' ),
502+ 'id ' => ag ($ extUser , 'id ' ),
503+ 'userId ' => $ user ['id ' ],
504+ 'userName ' => $ user ['name ' ],
505+ ]));
473506 unset($ users [$ key ]);
474507 }
475508 }
0 commit comments