diff --git a/src/adapters/class-jw-player-7-for-wp.php b/src/adapters/class-jw-player-7-for-wp.php index b3957e3..bb9c8df 100644 --- a/src/adapters/class-jw-player-7-for-wp.php +++ b/src/adapters/class-jw-player-7-for-wp.php @@ -8,28 +8,14 @@ namespace Alley\WP\WP_Video_Sync\Adapters; use Alley\WP\WP_Video_Sync\Interfaces\Adapter; +use Alley\WP\WP_Video_Sync\Last_Modified_Date; use DateTimeImmutable; use stdClass; /** * JW Player 7 for WP Adapter. Supports both the free and premium versions of the plugin. */ -class JW_Player_7_For_WP implements Adapter { - /** - * The date of the last modification to the last batch of videos. - * - * @var ?DateTimeImmutable - */ - private ?DateTimeImmutable $last_modified_date; - - /** - * Fetches the date of the last modification to the last batch of videos. - * - * @return ?DateTimeImmutable - */ - public function get_last_modified_date(): ?DateTimeImmutable { - return $this->last_modified_date; - } +class JW_Player_7_For_WP extends Last_Modified_Date implements Adapter { /** * Fetches videos from JW Player that were modified after the provided DateTime. @@ -54,10 +40,7 @@ public function get_videos( DateTimeImmutable $updated_after, int $batch_size ): // Attempt to set the last modified date. if ( isset( $videos[ count( $videos ) - 1 ]->last_modified ) ) { - $last_modified_date = DateTimeImmutable::createFromFormat( DATE_W3C, $videos[ count( $videos ) - 1 ]->last_modified ); - if ( $last_modified_date instanceof DateTimeImmutable ) { - $this->last_modified_date = $last_modified_date; - } + $this->set_last_modified_date( $videos[ count( $videos ) - 1 ]->last_modified ); } return $videos; diff --git a/src/adapters/class-jw-player.php b/src/adapters/class-jw-player.php index d30d54a..219718c 100644 --- a/src/adapters/class-jw-player.php +++ b/src/adapters/class-jw-player.php @@ -10,20 +10,14 @@ use Alley\WP\WP_Video_Sync\API\JW_Player_API; use Alley\WP\WP_Video_Sync\API\Request; use Alley\WP\WP_Video_Sync\Interfaces\Adapter; +use Alley\WP\WP_Video_Sync\Last_Modified_Date; use DateTimeImmutable; use stdClass; /** * JW Player Adapter. */ -class JW_Player implements Adapter { - - /** - * The date of the last modification to the last batch of videos. - * - * @var ?DateTimeImmutable - */ - private ?DateTimeImmutable $last_modified_date = null; +class JW_Player extends Last_Modified_Date implements Adapter { /** * The JW Player API. @@ -41,34 +35,6 @@ public function __construct( JW_Player_API $api ) { $this->jw_player_api = $api; } - /** - * Fetches the date of the last modification to the last batch of videos. - * - * @return ?DateTimeImmutable - */ - public function get_last_modified_date(): ?DateTimeImmutable { - return $this->last_modified_date; - } - - /** - * Sets the date of the last modification to the latest batch of videos. - * - * @param array $videos An array of videos and associated data. - * @return void - */ - public function set_last_modified_date( array $videos ): void { - if ( - ! empty( $videos ) - && isset( $videos[ count( $videos ) - 1 ]->last_modified ) - ) { - $last_modified_date = DateTimeImmutable::createFromFormat( DATE_W3C, $videos[ count( $videos ) - 1 ]->last_modified ); - - if ( $last_modified_date instanceof DateTimeImmutable ) { - $this->last_modified_date = $last_modified_date; - } - } - } - /** * Fetches videos from JW Player that were modified after the provided DateTime. * @@ -93,7 +59,12 @@ public function get_videos( DateTimeImmutable $updated_after, int $batch_size ): } // Attempt to set the last modified date. - $this->set_last_modified_date( $videos['media'] ); + if ( + ! empty( $videos['media'] ) + && isset( $videos['media'][ count( $videos['media'] ) - 1 ]->last_modified ) + ) { + $this->set_last_modified_date( $videos['media'][ count( $videos ) - 1 ]->last_modified ); + } // Return the videos. return ! empty( $videos['media'] ) ? $videos['media'] : []; diff --git a/src/class-last-modified-date.php b/src/class-last-modified-date.php new file mode 100644 index 0000000..5e19e41 --- /dev/null +++ b/src/class-last-modified-date.php @@ -0,0 +1,46 @@ +last_modified_date; + } + + /** + * Sets the date of the last modification based on the latest batch of videos. + * + * @param string $last_modified_date The date of the last modified video in the batch. + * @return void + */ + public function set_last_modified_date( string $last_modified_date = '' ): void { + $last_modified_date = DateTimeImmutable::createFromFormat( DATE_W3C, $last_modified_date ); + + if ( $last_modified_date instanceof \DateTimeImmutable ) { + $this->last_modified_date = $last_modified_date; + } + } +} diff --git a/src/interfaces/adapter.php b/src/interfaces/adapter.php index c660628..18ffeb4 100644 --- a/src/interfaces/adapter.php +++ b/src/interfaces/adapter.php @@ -14,12 +14,6 @@ * Defines an interface that all adapters must implement. */ interface Adapter { - /** - * Fetches the date of the last modification to the last batch of videos. - * - * @return ?DateTimeImmutable - */ - public function get_last_modified_date(): ?DateTimeImmutable; /** * Fetches videos from the provider that were modified after the provided DateTime.