diff --git a/src/adapters/class-jw-player.php b/src/adapters/class-jw-player.php index 219718c..56e55a5 100644 --- a/src/adapters/class-jw-player.php +++ b/src/adapters/class-jw-player.php @@ -12,7 +12,6 @@ use Alley\WP\WP_Video_Sync\Interfaces\Adapter; use Alley\WP\WP_Video_Sync\Last_Modified_Date; use DateTimeImmutable; -use stdClass; /** * JW Player Adapter. @@ -41,7 +40,7 @@ public function __construct( JW_Player_API $api ) { * @param DateTimeImmutable $updated_after Return videos modified after this date. * @param int $batch_size The number of videos to fetch in each batch. * - * @return stdClass[] An array of video data. + * @return array An array of video data objects. */ public function get_videos( DateTimeImmutable $updated_after, int $batch_size ): array { // Set the request URL based on the arguments. @@ -58,9 +57,14 @@ public function get_videos( DateTimeImmutable $updated_after, int $batch_size ): return []; } + // Validate the media property. + if ( ! is_array( $videos['media'] ) ) { + return []; + } + // Attempt to set the last modified date. if ( - ! empty( $videos['media'] ) + ! empty( $videos['media'][ count( $videos['media'] ) - 1 ] ) && isset( $videos['media'][ count( $videos['media'] ) - 1 ]->last_modified ) ) { $this->set_last_modified_date( $videos['media'][ count( $videos ) - 1 ]->last_modified ); diff --git a/src/api/class-jw-player-api.php b/src/api/class-jw-player-api.php index aa03512..0697e3a 100644 --- a/src/api/class-jw-player-api.php +++ b/src/api/class-jw-player-api.php @@ -59,7 +59,7 @@ public function __construct( string $api_key, string $api_secret ) { * @param string $last_modified_date The date of the last modification to the last batch of videos. * @param int $batch_size The number of videos to fetch in each batch. */ - public function set_request_url( string $last_modified_date, int $batch_size ) { + public function set_request_url( string $last_modified_date, int $batch_size ): void { $request_url = $this->api_url . '/' . $this->api_key . '/media/'; $this->request_url = add_query_arg( @@ -85,7 +85,7 @@ public function get_request_url(): string { /** * Get the request arguments. * - * @return array + * @return array> */ public function get_request_args(): array { return [ @@ -99,12 +99,14 @@ public function get_request_args(): array { /** * Parse the API error response. * - * @param array $response_object The API response object. + * @param array $response_object The API response object. * - * @return array + * @return array */ public function parse_error( array $response_object ): array { - return isset( $response_object['errors'][0]->description ) + return ! empty( $response_object['errors'] ) + && is_array($response_object['errors'] ) + && isset( $response_object['errors'][0]->description ) ? [ 'error' => $response_object['errors'][0]->description ] : []; } @@ -112,12 +114,13 @@ public function parse_error( array $response_object ): array { /** * Parse the API successful response. * - * @param array $response_object The API response object. + * @param array $response_object The API response object. * - * @return array + * @return array */ public function parse_success( array $response_object ): array { - return is_array( $response_object['media'] ) && ! empty( $response_object['media'] ) + return ! empty( $response_object['media'] ) + && is_array( $response_object['media'] ) ? [ 'media' => $response_object['media'] ] : []; } diff --git a/src/api/class-request.php b/src/api/class-request.php index ba2fadc..92c1433 100644 --- a/src/api/class-request.php +++ b/src/api/class-request.php @@ -44,7 +44,7 @@ public function user_agent(): string { /** * Get the request arguments. * - * @return array + * @return array */ public function get_request_args(): array { $requester_args = $this->api_requester->get_request_args(); @@ -53,6 +53,11 @@ public function get_request_args(): array { $requester_args['user-agent'] = $this->user_agent(); } + /** + * Allow the request arguments to be filtered before the request is made. + * + * @param array $requester_args The request arguments. + */ return apply_filters( 'wp_video_sync_request_args', $requester_args ); } @@ -60,11 +65,12 @@ public function get_request_args(): array { * Parse the API response. * * @param mixed $response The API response. - * @return array + * + * @return array */ private function parse_response( mixed $response ): array { // Failed request expressed as a WP_Error. - if ( is_wp_error( $response ) || empty( $response ) ) { + if ( is_wp_error( $response ) || empty( $response ) || ! is_array( $response ) ) { return []; } @@ -87,7 +93,7 @@ private function parse_response( mixed $response ): array { /** * Perform a GET request. * - * @return array + * @return array */ public function get(): array { if ( function_exists( 'vip_safe_wp_remote_get' ) ) { diff --git a/src/interfaces/adapter.php b/src/interfaces/adapter.php index 18ffeb4..2039b5c 100644 --- a/src/interfaces/adapter.php +++ b/src/interfaces/adapter.php @@ -8,7 +8,6 @@ namespace Alley\WP\WP_Video_Sync\Interfaces; use DateTimeImmutable; -use stdClass; /** * Defines an interface that all adapters must implement. @@ -21,7 +20,7 @@ interface Adapter { * @param DateTimeImmutable $updated_after Return videos modified after this date. * @param int $batch_size The number of videos to fetch in each batch. * - * @return stdClass[] An array of video data. Specific shape will be determined by the adapter. + * @return array An array of video data. Specific shape will be determined by the adapter. */ public function get_videos( DateTimeImmutable $updated_after, int $batch_size ): array; } diff --git a/src/interfaces/api-requester.php b/src/interfaces/api-requester.php index a08635a..22ff701 100644 --- a/src/interfaces/api-requester.php +++ b/src/interfaces/api-requester.php @@ -23,23 +23,26 @@ public function get_request_url(): string; /** * Get the arguments for the API request. * - * @return array + * @return array> */ public function get_request_args(): array; /** - * Parse a successful API response. + * Parse an error API response. + * + * @param array $response_object The API response object. * - * @param array $response_object The response object. - * @return array + * @return array */ - public function parse_success( array $response_object ): array; + public function parse_error( array $response_object ): array; /** - * Parse an error API response. + * Parse a successful API response. + * + * @param array $response_object The API response object. * - * @param array $response_object The response object. - * @return array + * @return array */ - public function parse_error( array $response_object ): array; + public function parse_success( array $response_object ): array; + }